using Blazored.LocalStorage; namespace ECommerceModule.Services; public class ShopStateService { private readonly ILocalStorageService _localStorage; // keys private const string FavoritesKey = "shop_favorites"; private const string CategoryIdKey = "shop_category_id"; // events public event Action? OnChange; public ShopStateService(ILocalStorageService localStorage) { _localStorage = localStorage; } public List Favorites { get; private set; } = new(); public Guid? CategoryId { get; private set; } public async Task InitializeAsync() { Favorites = await _localStorage.GetItemAsync>(FavoritesKey) ?? new List(); CategoryId = await _localStorage.GetItemAsync(CategoryIdKey); NotifyStateChanged(); } public async Task ToggleFavorite(string productId) { if (Favorites.Contains(productId)) { Favorites.Remove(productId); } else { Favorites.Add(productId); } await _localStorage.SetItemAsync(FavoritesKey, Favorites); NotifyStateChanged(); } public bool IsFavorite(string productId) { return Favorites.Contains(productId); } public async Task SetCategoryId(Guid? categoryId) { CategoryId = categoryId; await _localStorage.SetItemAsync(CategoryIdKey, CategoryId); NotifyStateChanged(); } private void NotifyStateChanged() => OnChange?.Invoke(); }