using CaritasPWA.Shared.Models; using CaritasPWA.Shared.ResourceFiles; using Json.Net; using Microsoft.Extensions.Localization; using Microsoft.JSInterop; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace CaritasPWA.Shared.Services { public class MasterDataService { private readonly ILFBicycleRest _lFBicycleRest; private const string KeyNameColors = "colors"; private const string KeyNameBcTypes = "bicycleTypes"; private const string KeyNameBrands = "brands"; private readonly IJSRuntime _jsRuntime; private readonly IStringLocalizer _i18n; private bool _initializedColors; private bool _initializedBcTypes; private bool _initializedBrands; private bool _firstActivation; public bool FirstActivation { get => _firstActivation; } private ColorItem[] _colors; private BicycleType[] _bicycleTypes; private Brand[] _brands; public ColorItem[] Colors { get => _colors; set => _colors = value; } public BicycleType[] BicycleTypes { get => _bicycleTypes; set => _bicycleTypes = value; } public Brand[] Brands { get => _brands; set => _brands = value; } public event EventHandler Changed; public MasterDataService(IJSRuntime jsRuntime, ILFBicycleRest lFBicycleRest, IStringLocalizer i18n) { _jsRuntime = jsRuntime; _lFBicycleRest = lFBicycleRest; _i18n = i18n; _firstActivation = true; } public async Task SynchronizeMasterdata() { await SynchronizeColors(); await SynchronizeBcTypes(); await SynchronizeBrands(); _firstActivation = false; } public async Task SynchronizeColors() { Colors = (await _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 SynchronizeBrands() { Brands = _lFBicycleRest.GetBrands().ToArray(); if (Brands != null && Brands.Length > 0) { await SaveBrandsToStorage(); } } public async Task GetColors() { ColorItem[] colors = await GetColorsFromStorage(); Colors = (colors != null && colors.Length > 0) ? colors: Defaults.GetColorDefaults(_i18n).ToArray(); Colors = SortColors(new List(Colors)).ToArray(); return Colors; } public async Task GetBicycleTypes() { BicycleType[] bicycleTypes = await GetBicycleTypesFromStorage(); BicycleTypes = (bicycleTypes != null && bicycleTypes.Length > 0) ? bicycleTypes : Defaults.GetBicycleTypeDefaults(_i18n).ToArray(); BicycleTypes = SortBicycleTypes(new List(BicycleTypes)).ToArray(); return BicycleTypes; } public async Task GetBrands() { Brand[] brands = await GetBrandsFromStorage(); Brands = (brands != null && brands.Length > 0) ? brands : Defaults.GetBrandDefaults(_i18n).ToArray(); Brands = SortBrands(new List(Brands)).ToArray(); return Brands; } // 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); } } 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 (String.IsNullOrEmpty(str)) { result = Array.Empty(); } else { result = JsonNet.Deserialize(str) ?? Array.Empty(); } _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 = JsonNet.Deserialize(str) ?? Array.Empty(); } else { result = Array.Empty(); } _bicycleTypes = result; return result; } private async ValueTask GetBrandsFromStorage() { // 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 (!_initializedBrands) { // 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); _initializedBrands = true; } // Read the JSON string that contains the data from the local storage Brand[] result; var str = await _jsRuntime.InvokeAsync("BlazorGetLocalStorage", KeyNameBrands); if (str != null) { result = JsonNet.Deserialize(str) ?? Array.Empty(); } else { result = Array.Empty(); } _brands = result; return result; } private async Task SaveColorsToStorage() { var json = JsonNet.Serialize(_colors); await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json); } private async Task SaveBcTypesToStorage() { var json = JsonNet.Serialize(_bicycleTypes); await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBcTypes, json); } private async Task SaveBrandsToStorage() { var json = JsonNet.Serialize(_brands); await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBrands, json); } private static List SortColors(List cols) { cols.Sort(delegate (ColorItem c1, ColorItem c2) { return c1.Id == 0 ? -1 : c2.Id == 0 ? 1 : c1.Bezeichnung.CompareTo(c2.Bezeichnung); }); return cols; } private static List SortBicycleTypes(List bcTypes) { bcTypes.Sort(delegate (BicycleType bct1, BicycleType bct2) { return bct1.Id == 0 ? -1 : bct2.Id == 0 ? 1 : bct1.Bezeichnung.CompareTo(bct2.Bezeichnung); }); return bcTypes; } private static List SortBrands(List brands) { brands.Sort(delegate (Brand b1, Brand b2) { return b1.Id == 0 ? -1 : b2.Id == 0 ? 1 : b1.Bezeichnung.CompareTo(b2.Bezeichnung); }); return brands; } } }