From 40abf65d069120a3bb9ef8fea67a0c216efc60f9 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 20 Jan 2026 14:24:09 +0100 Subject: [PATCH] Aggiungi gestione dei film preferiti e integrazione con Blazored.LocalStorage --- Components/Pages/MovieDetail.razor | 45 ++++++++++++- ...to Test 2026.pdf => Progetto Test 2026.pdf | Bin Program.cs | 3 + Services/ManageFavorite.cs | 63 ++++++++++++++++++ test-alebro.csproj | 4 ++ 5 files changed, 113 insertions(+), 2 deletions(-) rename pdfs/Progetto Test 2026.pdf => Progetto Test 2026.pdf (100%) create mode 100644 Services/ManageFavorite.cs diff --git a/Components/Pages/MovieDetail.razor b/Components/Pages/MovieDetail.razor index 7d458c7..86afece 100644 --- a/Components/Pages/MovieDetail.razor +++ b/Components/Pages/MovieDetail.razor @@ -2,7 +2,26 @@ Movie Details -
+ + +
@if (movie is not null) {

@movie.Title

@@ -11,7 +30,14 @@

@movie.Runtime

@movie.Plot

- + @if(ManageFavorite.IsFavorite(id)) + { + + } + else + { + + }
} else @@ -22,6 +48,7 @@ @inject OmdbService OmdbService +@inject ManageFavorite ManageFavorite @code { [Parameter] @@ -32,4 +59,18 @@ { movie = await OmdbService.FetchMovieDetail(id); } + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + await ManageFavorite.LoadFavorites(); + StateHasChanged(); + } + } + + private async Task ToggleFavoriteMovie() + { + await ManageFavorite.ToggleFavoriteMovie(id); + } } diff --git a/pdfs/Progetto Test 2026.pdf b/Progetto Test 2026.pdf similarity index 100% rename from pdfs/Progetto Test 2026.pdf rename to Progetto Test 2026.pdf diff --git a/Program.cs b/Program.cs index e4ac6c2..27eb826 100644 --- a/Program.cs +++ b/Program.cs @@ -1,13 +1,16 @@ using test_alebro.Components; +using Blazored.LocalStorage; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); +builder.Services.AddBlazoredLocalStorage(); // Mine services builder.Services.AddSingleton(); +builder.Services.AddScoped(); var app = builder.Build(); diff --git a/Services/ManageFavorite.cs b/Services/ManageFavorite.cs new file mode 100644 index 0000000..e3a7e4a --- /dev/null +++ b/Services/ManageFavorite.cs @@ -0,0 +1,63 @@ + + +/// +/// 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; + } +} \ No newline at end of file diff --git a/test-alebro.csproj b/test-alebro.csproj index 9ad1317..131067e 100644 --- a/test-alebro.csproj +++ b/test-alebro.csproj @@ -9,4 +9,8 @@ true + + + +