getMovieList method

Future<Map<String, dynamic>?> getMovieList(
  1. String movieListId, {
  2. bool forceRefresh = false,
})

Gets a MovieList by ID and loads full movie data for each movie reference.

Implementation

Future<Map<String, dynamic>?> getMovieList(String movieListId,
    {bool forceRefresh = false}) async {
  try {
    // Force refresh bypasses cache.

    if (!forceRefresh && _movieListCache.containsKey(movieListId)) {
      final cachedData = _movieListCache[movieListId]!;
      // Check if cached movies have full data (not just placeholders).

      if (cachedData['movies'] is List<Movie>) {
        final movies = cachedData['movies'] as List<Movie>;
        if (movies.isNotEmpty && movies.first.posterUrl.isNotEmpty) {
          return cachedData;
        }
      }
    }

    final loggedIn = await isLoggedIn();
    if (!loggedIn) return null;

    // Try to read from POD.

    if (!_context.mounted) return null;
    try {
      // Get the standard file path for this MovieList.

      final filePath = _getMovieListFilePath(movieListId);

      if (!_context.mounted) return null;
      final result =
          await readPod('moviestar/data/$filePath', _context, _child);

      if (result.isNotEmpty) {
        // Parse the MovieList data using TurtleSerializer.

        final movieListData = TurtleSerializer.movieListFromTurtle(result);

        if (movieListData != null) {
          // Load full movie data for each movie reference.

          final placeholderMovies =
              movieListData['movies'] as List<Movie>? ?? [];
          final fullMovies = <Movie>[];

          for (final placeholderMovie in placeholderMovies) {
            try {
              // Try to load full movie data from individual movie file.

              final fullMovieData =
                  await _loadFullMovieData(placeholderMovie.id);
              if (fullMovieData != null) {
                fullMovies.add(fullMovieData);
              } else {
                // If no individual movie file exists, keep the placeholder.
                // but try to get basic data from TMDB if we have a movie service.

                fullMovies.add(placeholderMovie);
              }
            } catch (e) {
              debugPrint(
                  '❌ Failed to load full data for movie ${placeholderMovie.id}: $e');
              // Keep placeholder as fallback.

              fullMovies.add(placeholderMovie);
            }
          }

          // Update the movie list data with full movie objects.

          movieListData['movies'] = fullMovies;

          // Update cache with enhanced data.

          _movieListCache[movieListId] = movieListData;
          return movieListData;
        } else {
          debugPrint('❌ Failed to parse MovieList TTL content');
        }
      } else {
        debugPrint('❌ MovieList file is empty or not found');
      }
    } catch (e) {
      debugPrint('❌ Failed to read MovieList from POD: $e');
    }

    return null;
  } catch (e) {
    debugPrint('❌ Failed to get movie list: $e');
    return null;
  }
}