Beispiel #1
0
        public MainViewModel(TvDbSeriesRepository repository, ConnectivityService connectivityService, ReminderService reminderService, AgentScheduler agentScheduler)
        {
            this.connectivityService          = connectivityService;
            this.reminderService              = reminderService;
            this.agentScheduler               = agentScheduler;
            connectivityService.InternetDown += connectivityService_InternetDown;
            connectivityService.InternetUp   += connectivityService_InternetUp;

            Series = new SelfSortingObservableCollection <TvDbSeries, DateTime?>(s => s.NextEpisodeAirDateTime, new SoonestFirstComparer());
            repository.Subscribed   += (sender, args) => DispatcherHelper.UIDispatcher.BeginInvoke(() => Series.Add(args.Series));
            repository.Unsubscribed += (sender, args) => DispatcherHelper.UIDispatcher.BeginInvoke(() => Series.Remove(args.Series));

            AlphabeticalSortingEnabled = AppSettings.Instance.AlphabeticalSortingEnabled;

            if (!IsInDesignMode)
            {
                this.repository    = repository;
                IsSearchBoxEnabled = true;
                searchResults      = new SelfSortingObservableCollection <TvDbSeries, float>(s => s.Rating, order: SortOrder.Desc);
                ltUpdater          = new LiveTileUpdater(this);

                MessengerInstance.Register <AppSettings>(this, s => AlphabeticalSortingEnabled = s.AlphabeticalSortingEnabled);
                //series = new SelfSortingObservableCollection<TvDbSeries, string>(s => s.Title);
            }
            else if (IsInDesignMode)
            {
                searchResults = new ObservableCollection <TvDbSeries>();

                Search = "Simpsons";
                Series.Add(new TvDbSeries()
                {
                    Title    = "Futurama",
                    Image    = "http://thetvdb.com/banners/posters/73871-2.jpg",
                    Rating   = 5,
                    AirsTime = "11 PM",
                    Episodes = new ObservableCollection <TvDbSeriesEpisode>()
                    {
                        new TvDbSeriesEpisode()
                        {
                            Name          = "Episode 1",
                            FirstAired    = DateTime.Now.AddDays(14).AddHours(5),
                            SeriesNumber  = "1",
                            EpisodeNumber = "1",
                            Description   = "bla bla bla bla bla lba",
                            Image         = "http://thetvdb.com/banners/episodes/121361/3254641.jpg"
                        },
                        new TvDbSeriesEpisode()
                        {
                            Name          = "Episode 2",
                            FirstAired    = DateTime.Now.AddDays(7),
                            SeriesNumber  = "1",
                            EpisodeNumber = "2",
                            Description   = "bla bla bla bla bla lba",
                            Image         = "http://thetvdb.com/banners/episodes/121361/3254641.jpg"
                        },
                        new TvDbSeriesEpisode()
                        {
                            Name          = "Episode 1",
                            FirstAired    = DateTime.Now,
                            SeriesNumber  = "2",
                            EpisodeNumber = "1",
                            Description   = "bla bla bla bla bla lba",
                            Image         = "http://thetvdb.com/banners/episodes/121361/3254641.jpg"
                        }
                    }
                });

                Series.Add(new TvDbSeries()
                {
                    Title  = "Simpsons",
                    Image  = "http://thetvdb.com/banners/posters/71663-10.jpg",
                    Rating = 10
                });

                searchResults = series;

                AlphabeticalSortingEnabled = true;
            }
        }
Beispiel #2
0
        private IEnumerable <Action <TvDbSeries> > GetSeriesUpdates(string response)
        {
            var doc    = XDocument.Parse(response);
            var poster = doc.Descendants("poster").FirstOrDefault();

            if (poster != null)
            {
                if (!string.IsNullOrEmpty(poster.Value))
                {
                    var image = string.Format("{0}/banners/{1}", mirror, poster.Value);
                    yield return(s => s.Image = image);
                }
            }

            yield return
                (doc.ParseAndGetUpdateAction("Rating",
                                             value => float.Parse(value, CultureInfo.InvariantCulture.NumberFormat), (s, r) => s.Rating = r));

            yield return(doc.ParseAndGetUpdateAction("Airs_Time", (s, v) => s.AirsTime = v));

            yield return
                (doc.ParseAndGetUpdateAction("Airs_DayOfWeek",
                                             value => DaysOfWeek.IndexOf(value.Trim().ToLowerInvariant()), (s, v) => s.AirsDayOfWeek = v));

            yield return
                (doc.ParseAndGetUpdateAction("Runtime", int.Parse, (s, i) => s.Runtime = i));

            var episodeUpdates = from newData in doc.Descendants("Episode")
                                 select new
            {
                Id = TvDbSeriesEpisode.GetEpisodeId(
                    newData.Descendants("SeasonNumber").Select(n => n.Value).FirstOrDefault(),
                    newData.Descendants("EpisodeNumber").Select(n => n.Value).FirstOrDefault()),
                Data = newData
            };

            var newEpisodes = (from update in episodeUpdates
                               let name = update.Data.Descendants("EpisodeName").Select(e => e.Value).FirstOrDefault()
                                          let seasonNumber = update.Data.Descendants("SeasonNumber").Select(n => n.Value).FirstOrDefault()
                                                             let episodeNumber = update.Data.Descendants("EpisodeNumber").Select(n => n.Value).FirstOrDefault()
                                                                                 let overview = update.Data.Descendants("Overview").Select(n => n.Value).FirstOrDefault()
                                                                                                let firstAired = update.Data.Descendants("FirstAired").Select(n =>
            {
                DateTime date;
                if (DateTime.TryParse(n.Value, out date))
                {
                    return((DateTime?)date);
                }

                return(null);
            }).FirstOrDefault()
                                                                                                                 let image = update.Data.Descendants("filename").Select(n => string.Format("{0}/banners/{1}", mirror, n.Value)).FirstOrDefault()
                                                                                                                             select(Func <IList <TvDbSeriesEpisode>, TvDbSeriesEpisode>)(episodes =>
            {
                var episode = episodes.FirstOrDefault(e => e.Id == update.Id);
                if (episode == null)
                {
                    episode = new TvDbSeriesEpisode();
                }
                else
                {
                    episodes.Remove(episode);
                }

                episode.Name = name;
                episode.SeriesNumber = seasonNumber;
                episode.EpisodeNumber = episodeNumber;
                episode.Description = overview;
                episode.FirstAired = firstAired;
                episode.Image = image;

                return(episode);
            })).ToList();

            yield return(s =>
            {
                var episodes = new SelfSortingObservableCollection <TvDbSeriesEpisode, string>(e => e.Id);
                var current = s.Episodes.ToList();
                foreach (var episode in newEpisodes.Select(e => e(current)))
                {
                    episodes.Add(episode);
                }
                s.Episodes = episodes;
            });

            yield return(s => s.Updated = DateTime.Now);
        }