Ejemplo n.º 1
0
        /// <summary>
        /// Search shows asynchronously
        /// </summary>
        public override async Task LoadShowsAsync(bool reset = false)
        {
            await LoadingSemaphore.WaitAsync(CancellationLoadingShows.Token);

            StopLoadingShows();
            if (reset)
            {
                Shows.Clear();
                Page           = 0;
                VerticalScroll = 0d;
            }

            var watch = Stopwatch.StartNew();

            Page++;
            if (Page > 1 && Shows.Count == MaxNumberOfShows)
            {
                Page--;
                LoadingSemaphore.Release();
                return;
            }

            Logger.Info(
                $"Loading search page {Page} with criteria: {SearchFilter}");
            HasLoadingFailed = false;
            try
            {
                IsLoadingShows = true;
                var result =
                    await ShowService.SearchShowsAsync(SearchFilter,
                                                       Page,
                                                       MaxNumberOfShows,
                                                       Genre,
                                                       Rating * 10,
                                                       CancellationLoadingShows.Token);

                Shows.AddRange(result.shows.Except(Shows, new ShowLightComparer()));
                IsLoadingShows       = false;
                IsShowFound          = Shows.Any();
                CurrentNumberOfShows = Shows.Count;
                MaxNumberOfShows     = result.nbShows;
                UserService.SyncShowHistory(Shows);
            }
            catch (Exception exception)
            {
                Page--;
                Logger.Error(
                    $"Error while loading search page {Page} with criteria {SearchFilter}: {exception.Message}");
                HasLoadingFailed = true;
                Messenger.Default.Send(new ManageExceptionMessage(exception));
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Info(
                    $"Loaded search page {Page} with criteria {SearchFilter} in {elapsedMs} milliseconds.");
                LoadingSemaphore.Release();
            }
        }
        /// <summary>
        /// Load movies asynchronously
        /// </summary>
        public override async Task LoadShowsAsync()
        {
            var watch = Stopwatch.StartNew();

            Logger.Info(
                $"Loading shows favorite page {Page}...");
            HasLoadingFailed = false;
            try
            {
                IsLoadingShows = true;
                var imdbIds =
                    await UserService.GetFavoritesShows().ConfigureAwait(false);

                var shows = new List <ShowJson>();
                await imdbIds.ParallelForEachAsync(async imdbId =>
                {
                    var show = await ShowService.GetShowAsync(imdbId);
                    if (show != null)
                    {
                        show.IsFavorite = true;
                        shows.Add(show);
                    }
                });

                DispatcherHelper.CheckBeginInvokeOnUI(async() =>
                {
                    Shows.Clear();
                    Shows.AddRange(shows.Where(a => Genre != null
                        ? a.Genres.Contains(Genre.EnglishName)
                        : a.Genres.TrueForAll(b => true) && a.Rating.Percentage >= Rating * 10));
                    IsLoadingShows       = false;
                    IsShowFound          = Shows.Any();
                    CurrentNumberOfShows = Shows.Count;
                    MaxNumberOfShows     = Shows.Count;
                    await UserService.SyncShowHistoryAsync(Shows).ConfigureAwait(false);
                });
            }
            catch (Exception exception)
            {
                Logger.Error(
                    $"Error while loading shows favorite page {Page}: {exception.Message}");
                HasLoadingFailed = true;
                Messenger.Default.Send(new ManageExceptionMessage(exception));
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Info(
                    $"Loaded shows favorite page {Page} in {elapsedMs} milliseconds.");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Load shows asynchronously
        /// </summary>
        public override async Task LoadShowsAsync()
        {
            var watch = Stopwatch.StartNew();

            Page++;

            if (Page > 1 && Shows.Count == MaxNumberOfShows)
            {
                return;
            }

            Logger.Info(
                $"Loading page {Page}...");

            HasLoadingFailed = false;

            try
            {
                IsLoadingShows = true;

                var shows =
                    await ShowService.GetPopularShowsAsync(Page,
                                                           MaxShowsPerPage,
                                                           Rating,
                                                           CancellationLoadingShows.Token,
                                                           Genre).ConfigureAwait(false);

                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    Shows.AddRange(shows.Item1);
                    IsLoadingShows       = false;
                    IsShowFound          = Shows.Any();
                    CurrentNumberOfShows = Shows.Count;
                    MaxNumberOfShows     = shows.Item2;
                });
            }
            catch (Exception exception)
            {
                Page--;
                Logger.Error(
                    $"Error while loading page {Page}: {exception.Message}");
                HasLoadingFailed = true;
                Messenger.Default.Send(new ManageExceptionMessage(exception));
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Info(
                    $"Loaded page {Page} in {elapsedMs} milliseconds.");
            }
        }
Ejemplo n.º 4
0
        private async Task PerformSearchQueryAsync(TraktShow selectedShow)
        {
            Shows.Clear();
            SearchResultsAvailable = true;

            var query = string.Empty;

            query = selectedShow == null ? SearchQuery : selectedShow.Title;

            var searchResults = await _client.Search.GetTextQueryResultsAsync(TraktSearchResultType.Show, query);

            var tasks = searchResults
                        .Select(result => result.Show.Ids.Trakt.ToString())
                        .Select(showId => _client.Shows.GetShowAsync(showId, new TraktExtendedOption {
                Full = true, Images = true
            }))
                        .ToList();

            var fullShows = await Task.WhenAll(tasks);

            Shows.AddRange(fullShows);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Load movies asynchronously
        /// </summary>
        public override async Task LoadShowsAsync(bool reset = false)
        {
            await LoadingSemaphore.WaitAsync();

            StopLoadingShows();
            if (reset)
            {
                Shows.Clear();
                Page = 0;
            }

            var watch = Stopwatch.StartNew();

            Page++;
            if (Page > 1 && Shows.Count == MaxNumberOfShows)
            {
                Page--;
                LoadingSemaphore.Release();
                return;
            }

            Logger.Info(
                $"Loading shows favorite page {Page}...");
            HasLoadingFailed = false;
            try
            {
                IsLoadingShows = true;
                var imdbIds =
                    await UserService.GetFavoritesShows(Page);

                if (!_needSync)
                {
                    var shows = new List <ShowJson>();
                    await imdbIds.shows.ParallelForEachAsync(async imdbId =>
                    {
                        var show = await ShowService.GetShowAsync(imdbId);
                        if (show != null)
                        {
                            show.IsFavorite = true;
                            shows.Add(show);
                        }
                    });

                    var updatedShows = shows.OrderBy(a => a.Title)
                                       .Where(a => (Genre != null
                                        ? a.Genres.Any(
                                                        genre => genre.ToLowerInvariant() ==
                                                        Genre.EnglishName.ToLowerInvariant())
                                        : a.Genres.TrueForAll(b => true)) && a.Rating.Percentage >= Rating * 10);
                    Shows.AddRange(updatedShows.Except(Shows.ToList(), new ShowComparer()));
                }
                else
                {
                    var showsToDelete = Shows.Select(a => a.ImdbId).Except(imdbIds.allShows);
                    var showsToAdd    = imdbIds.allShows.Except(Shows.Select(a => a.ImdbId));
                    foreach (var movie in showsToDelete.ToList())
                    {
                        Shows.Remove(Shows.FirstOrDefault(a => a.ImdbId == movie));
                    }

                    var shows = showsToAdd.ToList();
                    await shows.ParallelForEachAsync(async imdbId =>
                    {
                        var show = await ShowService.GetShowAsync(imdbId);
                        if ((Genre != null
                                    ? show.Genres.Any(
                                 genre => genre.ToLowerInvariant() ==
                                 Genre.EnglishName.ToLowerInvariant())
                                    : show.Genres.TrueForAll(b => true)) && show.Rating.Percentage >= Rating * 10)
                        {
                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                Shows.Add(show);
                            });
                        }
                    });
                }

                IsLoadingShows       = false;
                IsShowFound          = Shows.Any();
                CurrentNumberOfShows = Shows.Count;
                MaxNumberOfShows     = imdbIds.nbShows;
                await UserService.SyncShowHistoryAsync(Shows);
            }
            catch (Exception exception)
            {
                Page--;
                Logger.Error(
                    $"Error while loading shows favorite page {Page}: {exception.Message}");
                HasLoadingFailed = true;
                Messenger.Default.Send(new ManageExceptionMessage(exception));
            }
            finally
            {
                Shows.Sort((a, b) => String.Compare(a.Title, b.Title, StringComparison.Ordinal));
                _needSync = false;
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Info(
                    $"Loaded shows favorite page {Page} in {elapsedMs} milliseconds.");
                LoadingSemaphore.Release();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Search shows asynchronously
        /// </summary>
        /// <param name="searchFilter">The parameter of the search</param>
        public async Task SearchShowsAsync(string searchFilter)
        {
            var watch = Stopwatch.StartNew();

            if (SearchFilter != searchFilter)
            {
                // We start an other search
                StopLoadingShows();
                Shows.Clear();
                Page = 0;
                CurrentNumberOfShows = 0;
                MaxNumberOfShows     = 0;
                IsLoadingShows       = false;
            }

            Page++;
            if (Page > 1 && Shows.Count == MaxNumberOfShows)
            {
                return;
            }
            Logger.Info(
                $"Loading shows search page {Page} with criteria: {searchFilter}");
            HasLoadingFailed = false;
            try
            {
                SearchFilter   = searchFilter;
                IsLoadingShows = true;
                var result =
                    await ShowService.SearchShowsAsync(searchFilter,
                                                       Page,
                                                       MaxNumberOfShows,
                                                       Genre,
                                                       Rating * 10,
                                                       CancellationLoadingShows.Token)
                    .ConfigureAwait(false);

                DispatcherHelper.CheckBeginInvokeOnUI(async() =>
                {
                    Shows.AddRange(result.shows);
                    IsLoadingShows       = false;
                    IsShowFound          = Shows.Any();
                    CurrentNumberOfShows = Shows.Count;
                    MaxNumberOfShows     = result.nbShows;
                    await UserService.SyncShowHistoryAsync(Shows).ConfigureAwait(false);
                });
            }
            catch (Exception exception)
            {
                Page--;
                Logger.Error(
                    $"Error while loading shows search page {Page} with criteria {searchFilter}: {exception.Message}");
                HasLoadingFailed = true;
                Messenger.Default.Send(new ManageExceptionMessage(exception));
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Info(
                    $"Loaded shows search page {Page} with criteria {searchFilter} in {elapsedMs} milliseconds.");
            }
        }
        /// <summary>
        /// Load shows asynchronously
        /// </summary>
        public override async Task LoadShowsAsync(bool reset = false)
        {
            await LoadingSemaphore.WaitAsync();

            if (reset)
            {
                Shows.Clear();
                Page           = 0;
                VerticalScroll = 0d;
            }

            var watch = Stopwatch.StartNew();

            Page++;
            if (Page > 1 && Shows.Count == MaxNumberOfShows)
            {
                Page--;
                LoadingSemaphore.Release();
                return;
            }

            StopLoadingShows();
            Logger.Info(
                $"Loading page {Page}...");
            HasLoadingFailed = false;
            try
            {
                IsLoadingShows = true;
                await Task.Run(async() =>
                {
                    var getMoviesWatcher = new Stopwatch();
                    getMoviesWatcher.Start();
                    var result =
                        await ShowService.Discover(Page).ConfigureAwait(false);
                    getMoviesWatcher.Stop();
                    var getMoviesEllapsedTime = getMoviesWatcher.ElapsedMilliseconds;
                    if (reset && getMoviesEllapsedTime < 500)
                    {
                        // Wait for VerticalOffset to reach 0 (animation lasts 500ms)
                        await Task.Delay(500 - (int)getMoviesEllapsedTime).ConfigureAwait(false);
                    }

                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        Shows.AddRange(result.Item1.Except(Shows, new ShowLightComparer()));
                        IsLoadingShows       = false;
                        IsShowFound          = Shows.Any();
                        CurrentNumberOfShows = Shows.Count;
                        MaxNumberOfShows     = result.nbMovies;
                        UserService.SyncShowHistory(Shows);
                    });
                }).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                Page--;
                Logger.Error(
                    $"Error while loading page {Page}: {exception.Message}");
                HasLoadingFailed = true;
                Messenger.Default.Send(new ManageExceptionMessage(exception));
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Info(
                    $"Loaded page {Page} in {elapsedMs} milliseconds.");
                LoadingSemaphore.Release();
            }
        }