| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using cwebplusApp.Shared.Models;
- using cwebplusApp.Shared.ResourceFiles;
- using Json.Net;
- using Microsoft.Extensions.Localization;
- using Microsoft.JSInterop;
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
-
- namespace cwebplusApp.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<Resources> _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;
- }
-
- public BicycleType[] BicycleTypes {
- get => _bicycleTypes;
- }
-
- public Brand[] Brands {
- get => _brands;
- }
-
- public event EventHandler Changed;
-
- public MasterDataService(IJSRuntime jsRuntime, ILFBicycleRest lFBicycleRest, IStringLocalizer<Resources> i18n) {
- _jsRuntime = jsRuntime;
- _lFBicycleRest = lFBicycleRest;
- _i18n = i18n;
- _firstActivation = true;
- _brands = Defaults.GetBrandDefaults(_i18n).ToArray();
- _colors = Defaults.GetColorDefaults(_i18n).ToArray();
- _bicycleTypes = Defaults.GetBicycleTypeDefaults(_i18n).ToArray();
- Console.WriteLine("MasterDataService constructor / colors: " + _colors.Length);
- }
-
- public async Task SynchronizeMasterdata() {
- try {
- await SynchronizeColors();
- await SynchronizeBcTypes();
- await SynchronizeBrands();
- } finally {
- _firstActivation = false;
- }
- }
-
- public async Task SynchronizeColors() {
- ColorItem[] colors = (await _lFBicycleRest.GetColors()).ToArray();
- if (colors != null && colors.Length > 0) {
- _colors = colors;
- await SaveColorsToStorage(colors);
- };
- }
-
- public async Task SynchronizeBcTypes() {
- BicycleType[] bicycleTypes = (await _lFBicycleRest.GetBicycleTypes()).ToArray();
- if (bicycleTypes != null && bicycleTypes.Length > 0) {
- _bicycleTypes = bicycleTypes;
- await SaveBcTypesToStorage(bicycleTypes);
- }
- }
-
- public async Task SynchronizeBrands() {
- Brand[] brands = (await _lFBicycleRest.GetBrands()).ToArray();
- if (brands != null && brands.Length > 0) {
- _brands = brands;
- await SaveBrandsToStorage(brands);
- }
- }
-
- public async Task<ColorItem[]> GetColors() {
- ColorItem[] colors = await GetColorsFromStorage();
- if (colors != null && colors.Length > 0) {
- _colors = colors;
- }
- _colors = SortColors(new List<ColorItem>(_colors)).ToArray();
- return Colors;
- }
-
- public async Task<BicycleType[]> GetBicycleTypes() {
- BicycleType[] bicycleTypes = await GetBicycleTypesFromStorage();
- if (bicycleTypes != null && bicycleTypes.Length > 0) {
- _bicycleTypes = bicycleTypes;
- }
- _bicycleTypes = SortBicycleTypes(new List<BicycleType>(_bicycleTypes)).ToArray();
- return BicycleTypes;
- }
-
- public async Task<Brand[]> GetBrands() {
- Brand[] brands = await GetBrandsFromStorage();
- if (brands != null && brands.Length > 0) {
- _brands = brands;
- }
- _brands = SortBrands(new List<Brand>(_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<ColorItem[]> 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<string>("BlazorGetLocalStorage", KeyNameColors);
- if (String.IsNullOrEmpty(str)) {
- result = Array.Empty<ColorItem>();
- } else {
- result = JsonNet.Deserialize<ColorItem[]>(str) ?? Array.Empty<ColorItem>();
- }
- return result;
- }
-
- private async ValueTask<BicycleType[]> 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<string>("BlazorGetLocalStorage", KeyNameBcTypes);
- if (String.IsNullOrEmpty(str)) {
- result = Array.Empty<BicycleType>();
- } else {
- result = JsonNet.Deserialize<BicycleType[]>(str) ?? Array.Empty<BicycleType>();
- }
- return result;
- }
-
- private async ValueTask<Brand[]> 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<string>("BlazorGetLocalStorage", KeyNameBrands);
- if (String.IsNullOrEmpty(str)) {
- result = Array.Empty<Brand>();
- } else {
- result = JsonNet.Deserialize<Brand[]>(str) ?? Array.Empty<Brand>();
- }
- return result;
- }
-
-
- private async Task SaveColorsToStorage(ColorItem[] colors) {
- var json = JsonNet.Serialize(colors);
- await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json);
- }
-
- private async Task SaveBcTypesToStorage(BicycleType[] bicycleTypes) {
- var json = JsonNet.Serialize(bicycleTypes);
- await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBcTypes, json);
- }
-
- private async Task SaveBrandsToStorage(Brand[] brands) {
- var json = JsonNet.Serialize(brands);
- await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBrands, json);
- }
-
- private static List<ColorItem> SortColors(List<ColorItem> 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<BicycleType> SortBicycleTypes(List<BicycleType> 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<Brand> SortBrands(List<Brand> brands) {
- brands.Sort(delegate (Brand b1, Brand b2) { return b1.Id == 0 ? -1 : b2.Id == 0 ? 1 : b1.Bezeichnung.CompareTo(b2.Bezeichnung); });
- return brands;
- }
- }
- }
|