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