removeMovieFromList method

Future<bool> removeMovieFromList(
  1. String movieListId,
  2. Movie movie
)

Removes a movie from a MovieList.

Implementation

Future<bool> removeMovieFromList(String movieListId, Movie movie) async {
  try {
    final movieList = await getMovieList(movieListId);
    if (movieList == null) return false;

    final currentMovies = List<Movie>.from(movieList['movies'] ?? []);

    // Check if movie exists before trying to remove.

    final existingIndex = currentMovies.indexWhere((m) => m.id == movie.id);
    if (existingIndex < 0) {
      return true;
    }

    // Remove the movie.

    currentMovies.removeWhere((m) => m.id == movie.id);

    // Update the MovieList.

    final updatedTtl = TurtleSerializer.createMovieList(
      movieListId,
      movieList['name'],
      movies: currentMovies,
    );

    // Get the standard file path for writing.

    final filePath = _getMovieListFilePath(movieListId);

    if (!_context.mounted) return false;
    final result = await writePod(
      filePath,
      updatedTtl,
      _context,
      _child,
      encrypted: false,
    );

    if (result == SolidFunctionCallStatus.success) {
      // Update cache with new data.

      _movieListCache[movieListId] = {
        'id': movieListId,
        'name': movieList['name'],
        'movies': currentMovies,
        'filePath': movieList['filePath'],
      };
      return true;
    }

    debugPrint('❌ Failed to write updated MovieList to POD');
    return false;
  } catch (e) {
    debugPrint('❌ Failed to remove movie from list: $e');
    return false;
  }
}