Movie Detail (#3)
* Implemented Movie Detail * Implemented basic favorites Reviewed-on: #3 Co-authored-by: Berack96 <giacomobertolazzi7@gmail.com> Co-committed-by: Berack96 <giacomobertolazzi7@gmail.com>
This commit was merged in pull request #3.
This commit is contained in:
63
Services/ManageFavorite.cs
Normal file
63
Services/ManageFavorite.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user