/// <summary> /// Search movies /// </summary> /// <param name="searchFilter">The parameter of the search</param> public async Task SearchMoviesAsync(string searchFilter) { if (SearchFilter != searchFilter) { // We start an other search StopLoadingMovies(); Movies.Clear(); Page = 0; } var watch = Stopwatch.StartNew(); Page++; Logger.Info( $"Loading page {Page} with criteria: {searchFilter}"); HasLoadingFailed = false; try { SearchFilter = searchFilter; IsLoadingMovies = true; var movies = await MovieService.SearchMoviesAsync(searchFilter, Page, MaxMoviesPerPage, Genre, Rating, CancellationLoadingMovies.Token); Movies = new ObservableCollection <MovieShort>(Movies.Union(movies.Item1, new MovieShortComparer())); IsLoadingMovies = false; IsMovieFound = Movies.Any(); CurrentNumberOfMovies = Movies.Count; MaxNumberOfMovies = movies.Item2; await MovieHistoryService.ComputeMovieHistoryAsync(movies.Item1); await MovieService.DownloadCoverImageAsync(movies.Item1, CancellationLoadingMovies); } catch (Exception exception) { Page--; Logger.Error( $"Error while loading 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 page {Page} with criteria {searchFilter} in {elapsedMs} milliseconds."); } }
/// <summary> /// Load next page /// </summary> public override async Task LoadMoviesAsync() { if (Page == LastPage) { return; } Page++; IsLoadingMovies = true; try { var movieResults = await MovieService.GetRecentMoviesAsync(Page, MaxMoviesPerPage, CancellationLoadNextPageToken.Token); var movies = movieResults.Item1.ToList(); MaxNumberOfMovies = movieResults.Item2; foreach (var movie in movies) { Movies.Add(movie); } if (!movies.Any()) { LastPage = Page; } IsLoadingMovies = false; await MovieHistoryService.ComputeMovieHistoryAsync(movies); await MovieService.DownloadCoverImageAsync(movies); } catch { Page--; } finally { IsLoadingMovies = false; IsMovieFound = Movies.Any(); CurrentNumberOfMovies = Movies.Count(); if (!IsMovieFound) { Page = 0; } } }
/// <summary> /// Load next page with an optional search parameter /// </summary> public override async Task LoadMoviesAsync() { var watch = Stopwatch.StartNew(); Page++; Logger.Info( $"Loading page {Page}..."); HasLoadingFailed = false; try { IsLoadingMovies = true; var movies = await MovieService.GetGreatestMoviesAsync(Page, MaxMoviesPerPage, Rating, CancellationLoadingMovies.Token, Genre); Movies = new ObservableCollection <MovieShort>(Movies.Union(movies.Item1, new MovieShortComparer())); IsLoadingMovies = false; IsMovieFound = Movies.Any(); CurrentNumberOfMovies = Movies.Count; MaxNumberOfMovies = movies.Item2; await MovieHistoryService.ComputeMovieHistoryAsync(movies.Item1); await MovieService.DownloadCoverImageAsync(movies.Item1, CancellationLoadingMovies); } 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."); } }
/// <summary> /// Register messages /// </summary> private void RegisterMessages() { Messenger.Default.Register <ChangeLanguageMessage>( this, async message => { foreach (var movie in Movies.ToList()) { await Task.Delay(1000); await MovieService.TranslateMovieShortAsync(movie); } }); Messenger.Default.Register <ChangeFavoriteMovieMessage>( this, async message => { await MovieHistoryService.ComputeMovieHistoryAsync(Movies); }); }
/// <summary> /// Search movies /// </summary> /// <param name="searchFilter">The parameter of the search</param> public async Task SearchMoviesAsync(string searchFilter) { if (SearchFilter != searchFilter) { // We start an other search StopSearchingMovies(); Movies.Clear(); Page = 0; } SearchFilter = searchFilter; Page++; var lastPage = int.MaxValue; if (!LastPageFilterMapping.ContainsKey(searchFilter) || (LastPageFilterMapping.TryGetValue(searchFilter, out lastPage) && Page < lastPage)) { try { IsLoadingMovies = true; var movieResults = await MovieService.SearchMoviesAsync(searchFilter, Page, MaxMoviesPerPage, CancellationSearchToken.Token); var movies = movieResults.Item1.ToList(); MaxNumberOfMovies = movieResults.Item2; foreach (var movie in movies) { Movies.Add(movie); } IsLoadingMovies = false; await MovieHistoryService.ComputeMovieHistoryAsync(movies); await MovieService.DownloadCoverImageAsync(movies); if (!LastPageFilterMapping.ContainsKey(searchFilter) && !movies.Any()) { LastPageFilterMapping.Add(searchFilter, Page); } } catch { Page--; } finally { IsLoadingMovies = false; IsMovieFound = Movies.Any(); CurrentNumberOfMovies = Movies.Count(); if (!IsMovieFound) { Page = 0; } } } }