addMovieToList method
Adds a movie to a MovieList.
Implementation
Future<bool> addMovieToList(String movieListId, Movie movie) async {
try {
final movieList = await getMovieList(movieListId);
if (movieList == null) {
debugPrint('❌ MovieList $movieListId not found');
return false;
}
final currentMovies = List<Movie>.from(movieList['movies'] ?? []);
// Check if movie is already in the list.
final existingIndex = currentMovies.indexWhere((m) => m.id == movie.id);
if (existingIndex >= 0) {
return true;
}
// Create individual movie file to ensure full data is available.
await _createMovieFile(movie);
currentMovies.add(movie);
// 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 add movie to list: $e');
return false;
}
}