///
/// Service to manage favorite movies using local storage
///
/// The local storage service
class ManageFavorite(Blazored.LocalStorage.ILocalStorageService localStorage)
{
// Inject the local storage service
private readonly Blazored.LocalStorage.ILocalStorageService LocalStorage = localStorage;
private readonly List MoviesIDs = [];
///
/// Load favorite movies from local storage
/// Use Immediately after service instantiation otherwise the list will be empty
///
///
public async Task LoadFavorites()
{
var favorites = await LocalStorage.GetItemAsync>("favorite") ?? [];
MoviesIDs.Clear();
MoviesIDs.AddRange(favorites);
}
///
/// Check if a movie is favorite or not
///
/// The movie ID to check
/// True if the movie is favorite, otherwise false
public bool IsFavorite(string movieID)
{
return MoviesIDs.Contains(movieID);
}
///
/// Toggle favorite status of a movie (by ID)
///
/// The movie ID to toggle
/// The new favorite status
public async Task ToggleFavoriteMovie(string movieID)
{
if (!IsFavorite(movieID))
{
MoviesIDs.Add(movieID);
}
else
{
MoviesIDs.RemoveAll(id => id == movieID);
}
await LocalStorage.SetItemAsync("favorite", MoviesIDs);
return IsFavorite(movieID);
}
///
/// Get the list of favorite movie IDs
///
/// The list of favorite movie IDs
public List GetFavoriteMovies()
{
return MoviesIDs;
}
}