using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using CaritasPWA.Shared.Models; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; namespace CaritasPWA.Shared { public class MasterDataService { private ILFBicycleRest _lFBicycleRest; private const string KeyNameColors = "colors"; private const string KeyNameBcTypes = "bicycleTypes"; private readonly IJSRuntime _jsRuntime; private bool _initializedColors; private bool _initializedBcTypes; private ColorItem[] _colors = new ColorItem[] { }; private BicycleType[] _bicycleTypes = new BicycleType[] { }; public ColorItem[] Colors { get => _colors; set => _colors = value; } public BicycleType[] BicycleTypes { get => _bicycleTypes; set => _bicycleTypes = value; } public event EventHandler Changed; public MasterDataService(IJSRuntime jsRuntime, ILFBicycleRest lFBicycleRest) { _jsRuntime = jsRuntime; _lFBicycleRest = lFBicycleRest; } public async Task SynchronizeMasterdata() { await SynchronizeColors(); await SynchronizeBcTypes(); } public async Task SynchronizeColors() { Colors = _lFBicycleRest.GetColors().ToArray(); if (Colors != null && Colors.Length > 0) { await SaveColorsToStorage(); }; } public async Task SynchronizeBcTypes() { BicycleTypes = _lFBicycleRest.GetBicycleTypes().ToArray(); if (BicycleTypes != null && BicycleTypes.Length > 0) { await SaveBcTypesToStorage(); } } public async Task GetColors() { ColorItem[] colors = await GetColorsFromStorage(); Colors = (colors != null && colors.Length > 0) ? colors : Defaults.ColorItems; return Colors; } public async Task GetBicycleTypes() { BicycleType[] bicycleTypes = await GetBicycleTypesFromStorage(); BicycleTypes = (bicycleTypes != null && bicycleTypes.Length > 0) ? bicycleTypes : Defaults.BycicleTypes; return BicycleTypes; } private async ValueTask GetColorsFromStorage() { // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed. // This way, you can reload the settings when another instance of the application (tab / window) save the settings if (!_initializedColors) { // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated" var reference = DotNetObjectReference.Create(this); await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference); _initializedColors = true; } // Read the JSON string that contains the data from the local storage ColorItem[] result; var str = await _jsRuntime.InvokeAsync("BlazorGetLocalStorage", KeyNameColors); if (str != null) { result = System.Text.Json.JsonSerializer.Deserialize(str) ?? new ColorItem[] { }; } else { result = new ColorItem[] { }; } _colors= result; return result; } private async ValueTask GetBicycleTypesFromStorage() { // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed. // This way, you can reload the settings when another instance of the application (tab / window) save the settings if (!_initializedBcTypes) { // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated" var reference = DotNetObjectReference.Create(this); await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference); _initializedBcTypes = true; } // Read the JSON string that contains the data from the local storage BicycleType[] result; var str = await _jsRuntime.InvokeAsync("BlazorGetLocalStorage", KeyNameBcTypes); if (str != null) { result = System.Text.Json.JsonSerializer.Deserialize(str) ?? new BicycleType[] { }; } else { result = new BicycleType[] { }; } _bicycleTypes = result; return result; } private async Task SaveColorsToStorage() { var json = System.Text.Json.JsonSerializer.Serialize(_colors); await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json); } private async Task SaveBcTypesToStorage() { var json = System.Text.Json.JsonSerializer.Serialize(_bicycleTypes); await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBcTypes, json); } // This method is called from BlazorRegisterStorageEvent when the storage changed [JSInvokable] public void OnStorageUpdated(string key) { // Reset the settings. The next call to Get will reload the data if (key == KeyNameColors) { _colors = null; Changed?.Invoke(this, EventArgs.Empty); } else if (key == KeyNameBcTypes) { _bicycleTypes = null; Changed?.Invoke(this, EventArgs.Empty); } } } }