示例#1
0
        public async Task  Test02()
        {
            var id = GetRandomNumber();

            var result = await _showService.GetShowAsync(id, CancellationToken.None);

            Assert.True(result.HasValue);
            Assert.Equal(result.Value, _show);
            _showRepositoryMock.Verify(srm => srm.FindAsync(id, CancellationToken.None), Times.Once);
            _showRepositoryMock.VerifyNoOtherCalls();
        }
        /// <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.");
            }
        }
        /// <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);
                    foreach (var show in updatedShows.Except(Shows.ToList(), new ShowComparer()))
                    {
                        var pair = Shows
                                   .Select((value, index) => new { value, index })
                                   .FirstOrDefault(x => string.CompareOrdinal(x.value.Title, show.Title) > 0);

                        if (pair == null)
                        {
                            Shows.Add(show);
                        }
                        else
                        {
                            Shows.Insert(pair.index, show);
                        }
                    }
                }
                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();
                    var showsToAddAndToOrder = new List <ShowJson>();
                    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)
                        {
                            showsToAddAndToOrder.Add(show);
                        }
                    });

                    foreach (var show in showsToAddAndToOrder.Except(Shows.ToList(), new ShowComparer()))
                    {
                        var pair = Shows
                                   .Select((value, index) => new { value, index })
                                   .FirstOrDefault(x => string.CompareOrdinal(x.value.Title, show.Title) > 0);

                        if (pair == null)
                        {
                            Shows.Add(show);
                        }
                        else
                        {
                            Shows.Insert(pair.index, 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
            {
                NeedSync = false;
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Info(
                    $"Loaded shows favorite page {Page} in {elapsedMs} milliseconds.");
                LoadingSemaphore.Release();
            }
        }