forceRefresh method

Future<List<Movie>> forceRefresh(
  1. CacheCategory category
)

Forces refresh of cached data for a specific category.

Implementation

Future<List<Movie>> forceRefresh(CacheCategory category) async {
  developer.log(
    'Force refreshing cache for ${category.value}',
    name: 'CachedMovieService',
  );

  // Clear existing cache.

  await _cacheService.clearCacheForCategory(category);

  // Fetch fresh data.

  final List<Movie> movies;
  switch (category) {
    case CacheCategory.toWatch:
      throw UnsupportedError(
        'To Watch movies are user data and should not be cached via CachedMovieService. '
        'Use FavoritesService instead.',
      );
    case CacheCategory.watched:
      throw UnsupportedError(
        'Watched movies are user data and should not be cached via CachedMovieService. '
        'Use FavoritesService instead.',
      );
    case CacheCategory.popular:
      movies = await _movieService.getPopularMovies();
    case CacheCategory.nowPlaying:
      movies = await _movieService.getNowPlayingMovies();
    case CacheCategory.topRated:
      movies = await _movieService.getTopRatedMovies();
    case CacheCategory.upcoming:
      movies = await _movieService.getUpcomingMovies();
  }

  // Cache the fresh data.

  await _cacheService.cacheMoviesForCategory(category, movies);

  developer.log(
    'Force refreshed ${movies.length} movies for ${category.value}',
    name: 'CachedMovieService',
  );

  return movies;
}