using cwebplusApp.Shared.Models; using cwebplusApp.Shared.ResourceFiles; using Microsoft.Extensions.Localization; using Microsoft.JSInterop; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace cwebplusApp.Shared.Services { public class MasterDataService { private readonly IBicycleRestService bicycleRestService; private readonly IJSRuntime jsRuntime; private readonly IStringLocalizer i18n; private readonly OnlineStatusProvider onlineStatusProvider; private const string KeyNameColors = "colors"; private const string KeyNameBcTypes = "bicycleTypes"; private const string KeyNameBrands = "brands"; private const string KeyNameSearchservices = "searchServices"; private const string KeyNameZipCities = "zipCities"; private bool initializedColors; private bool initializedBcTypes; private bool initializedBrands; private bool initializedSearchServices; private bool initializedZipCities; private ColorItem[] colors; private BicycleType[] bicycleTypes; private Brand[] brands; private SearchService[] searchServices; private ZipCity[] zipCities; public ColorItem[] Colors { get => this.colors; } public BicycleType[] BicycleTypes { get => this.bicycleTypes; } public Brand[] Brands { get => this.brands; } public ZipCity[] ZipCities { get => this.zipCities; } public SearchService[] SearchServices { get => this.searchServices; } public event EventHandler Changed; public MasterDataService(IJSRuntime _jsRuntime, IBicycleRestService _bicycleRestService, IStringLocalizer _i18n, OnlineStatusProvider _onlineStatusProvider) { this.jsRuntime = _jsRuntime; this.bicycleRestService = _bicycleRestService; this.i18n = _i18n; this.onlineStatusProvider = _onlineStatusProvider; this.brands = Defaults.GetBrandDefaults(i18n).ToArray(); this.colors = Defaults.GetColorDefaults(i18n).ToArray(); this.bicycleTypes = Defaults.GetBicycleTypeDefaults(i18n).ToArray(); this.searchServices = Defaults.GetSearchServiceDefaults(i18n).ToArray(); } public async Task SynchronizeMasterdata() { if (onlineStatusProvider.Online) { await SynchronizeColors(); await SynchronizeBcTypes(); await SynchronizeBrands(); await SynchronizeSearchServices(); await SynchronizeZipCities(); } else { throw new Exception("No internet connection available!"); } } public async Task SynchronizeColors() { ColorItem[] _colors = (await bicycleRestService.GetColors()).ToArray(); if (_colors != null && _colors.Length > 0) { this.colors = _colors; await SaveColorsToStorage(this.colors); }; } public async Task SynchronizeBcTypes() { BicycleType[] _bicycleTypes = (await bicycleRestService.GetBicycleTypes()).ToArray(); if (_bicycleTypes != null && _bicycleTypes.Length > 0) { this.bicycleTypes = _bicycleTypes; await SaveBcTypesToStorage(this.bicycleTypes); } } public async Task SynchronizeBrands() { Brand[] _brands = (await bicycleRestService.GetBrands()).ToArray(); if (_brands != null && _brands.Length > 0) { this.brands = _brands; await SaveBrandsToStorage(this.brands); } } public async Task SynchronizeSearchServices() { SearchService[] _searchServices = (await bicycleRestService.GetSearchServices()).ToArray(); if (_searchServices != null && _searchServices.Length > 0) { this.searchServices = _searchServices; await SaveSearchServicesToStorage(this.searchServices); } } public async Task SynchronizeZipCities() { ZipCity[] _zipCities = (await bicycleRestService.GetZipCities()).ToArray(); if (_zipCities != null && _zipCities.Length > 0) { this.zipCities = _zipCities; await SaveZipCitiesToStorage(this.zipCities); } } public async Task GetColors() { ColorItem[] _colors = await GetColorsFromStorage(); if (_colors != null && _colors.Length > 0) { this.colors = _colors; } this.colors = SortColors(new List(this.colors)).ToArray(); return Colors; } public async Task GetBicycleTypes() { BicycleType[] _bicycleTypes = await GetBicycleTypesFromStorage(); if (_bicycleTypes != null && _bicycleTypes.Length > 0) { this.bicycleTypes = _bicycleTypes; } this.bicycleTypes = SortBicycleTypes(new List(this.bicycleTypes)).ToArray(); return BicycleTypes; } public async Task GetBrands() { Brand[] _brands = await GetBrandsFromStorage(); if (_brands != null && _brands.Length > 0) { this.brands = _brands; } this.brands = SortBrands(new List(this.brands)).ToArray(); return Brands; } public async Task GetSearchServices() { SearchService[] _searchServices = await GetSearchServicesFromStorage(); if (_searchServices != null && _searchServices.Length > 0) { this.searchServices = _searchServices; } this.searchServices = SortSearchServices(new List(this.searchServices)).ToArray(); return SearchServices; } public async Task GetZipCities() { ZipCity[] _zipCities = await GetZipCitiesFromStorage(); if (_zipCities != null && _zipCities.Length > 0) { this.zipCities = _zipCities; } this.zipCities = SortZipCities(new List(this.zipCities)).ToArray(); return ZipCities; } // 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) { this.colors = null; Changed?.Invoke(this, EventArgs.Empty); } else if (key == KeyNameBcTypes) { this.bicycleTypes = null; Changed?.Invoke(this, EventArgs.Empty); } else if (key == KeyNameBrands) { this.brands = 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); this.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 = JsonConvert.DeserializeObject(str) ?? Array.Empty(); } 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); this.initializedBcTypes = true; } // Read the JSON string that contains the data from the local storage BicycleType[] result; var str = await jsRuntime.InvokeAsync("BlazorGetLocalStorage", KeyNameBcTypes); if (String.IsNullOrEmpty(str)) { result = Array.Empty(); } else { result = JsonConvert.DeserializeObject(str) ?? Array.Empty(); } 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); this.initializedBrands = true; } // Read the JSON string that contains the data from the local storage Brand[] result; var str = await jsRuntime.InvokeAsync("BlazorGetLocalStorage", KeyNameBrands); if (String.IsNullOrEmpty(str)) { result = Array.Empty(); } else { result = JsonConvert.DeserializeObject(str) ?? Array.Empty(); } return result; } private async ValueTask GetSearchServicesFromStorage() { // 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 (!initializedSearchServices) { // 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); this.initializedSearchServices = true; } // Read the JSON string that contains the data from the local storage SearchService[] result; var str = await jsRuntime.InvokeAsync("BlazorGetLocalStorage", KeyNameSearchservices); if (String.IsNullOrEmpty(str)) { result = Array.Empty(); } else { result = JsonConvert.DeserializeObject(str) ?? Array.Empty(); } return result; } private async ValueTask GetZipCitiesFromStorage() { // 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 (!this.initializedZipCities) { // 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); this.initializedZipCities = true; } // Read the JSON string that contains the data from the local storage ZipCity[] result; var str = await jsRuntime.InvokeAsync("BlazorGetLocalStorage", KeyNameZipCities); if (String.IsNullOrEmpty(str)) { result = Array.Empty(); } else { result = JsonConvert.DeserializeObject(str) ?? Array.Empty(); } return result; } private async Task SaveColorsToStorage(ColorItem[] colors) { var json = JsonConvert.SerializeObject(colors); await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json); } private async Task SaveBcTypesToStorage(BicycleType[] bicycleTypes) { var json = JsonConvert.SerializeObject(bicycleTypes); await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBcTypes, json); } private async Task SaveBrandsToStorage(Brand[] brands) { var json = JsonConvert.SerializeObject(brands); await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBrands, json); } private async Task SaveSearchServicesToStorage(SearchService[] searchServices) { var json = JsonConvert.SerializeObject(searchServices); await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameSearchservices, json); } private async Task SaveZipCitiesToStorage(ZipCity[] zipCities) { var json = JsonConvert.SerializeObject(zipCities); await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameZipCities, json); } private static List SortColors(List colors) { colors.Sort(delegate (ColorItem c1, ColorItem c2) { return c1.Id == 0 ? 1 : c2.Id == 0 ? -1 : c1.Bezeichnung.CompareTo(c2.Bezeichnung); }); return colors; } 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; } private static List SortSearchServices(List searchServices) { searchServices.Sort(delegate (SearchService ss1, SearchService ss2) { return ss1.Id == 0 ? -1 : ss2.Id == 0 ? 1 : ss1.Bezeichnung.CompareTo(ss2.Bezeichnung); }); return searchServices; } private static List SortZipCities(List zipCities) { int v = int.Parse("22"); zipCities.Sort(delegate (ZipCity zc1, ZipCity zc2) { return int.Parse(zc1.Zip).CompareTo(int.Parse(zc2.Zip)); }); return zipCities; } } }