2 Commits

Author SHA1 Message Date
40abf65d06 Aggiungi gestione dei film preferiti e integrazione con Blazored.LocalStorage 2026-01-20 14:24:09 +01:00
86b91263a8 movie detail 2026-01-20 13:46:12 +01:00
6 changed files with 146 additions and 11 deletions

View File

@@ -1,11 +0,0 @@
@page "/movie/{id}"
<PageTitle>Movie Details</PageTitle>
<h1>TODO</h1>
<p>Movie ID: @id</p>
@code {
[Parameter]
public string id { get; set; }
}

View File

@@ -0,0 +1,76 @@
@page "/movie/{id}"
<PageTitle>Movie Details</PageTitle>
<style>
.my-grid {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: auto auto;
gap: 20px;
max-width: 800px;
margin-right: auto;
}
.favorite {
background-color: gold;
color: black;
}
.not-favorite {
background-color: gray;
color: white;
}
</style>
<div class="my-grid">
@if (movie is not null)
{
<h1>@movie.Title</h1>
<div></div>
<img src="@movie.Poster" alt="@movie.Title Poster" />
<div class="details">
<p>@movie.Runtime</p>
<p>@movie.Plot</p>
@if(ManageFavorite.IsFavorite(id))
{
<button class="btn-secondary favorite" @onclick="ToggleFavoriteMovie">Remove Favorite</button>
}
else
{
<button class="btn-secondary not-favorite" @onclick="ToggleFavoriteMovie">Add to Favorites</button>
}
</div>
}
else
{
<p>Loading movie details...</p>
}
</div>
@inject OmdbService OmdbService
@inject ManageFavorite ManageFavorite
@code {
[Parameter]
public required string id { get; set; }
private Movie? movie;
protected override async Task OnInitializedAsync()
{
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);
}
}

View File

@@ -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<OmdbService>();
builder.Services.AddScoped<ManageFavorite>();
var app = builder.Build();

View 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;
}
}

View File

@@ -9,4 +9,8 @@
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
</ItemGroup>
</Project>