| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using CaritasPWA.Shared.Models;
- using Microsoft.JSInterop;
- using System;
- 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 bool _initializedColors;
- private bool _initializedBcTypes;
- private bool _initializedBrands;
- 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) {
- _jsRuntime = jsRuntime;
- _lFBicycleRest = lFBicycleRest;
- }
-
- public async Task SynchronizeMasterdata() {
- await SynchronizeColors();
- await SynchronizeBcTypes();
- await SynchronizeBrands();
- }
-
- 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 SynchronizeBrands() {
- Brands = _lFBicycleRest.GetBrands().ToArray();
- if (Brands != null && Brands.Length > 0) {
- await SaveBrandsToStorage();
- }
- }
-
- public async Task<ColorItem[]> GetColors() {
- ColorItem[] colors = await GetColorsFromStorage();
- Colors = (colors != null && colors.Length > 0) ? colors : Defaults.ColorItems;
- return Colors;
- }
-
- public async Task<BicycleType[]> GetBicycleTypes() {
- BicycleType[] bicycleTypes = await GetBicycleTypesFromStorage();
- BicycleTypes = (bicycleTypes != null && bicycleTypes.Length > 0) ? bicycleTypes : Defaults.BycicleTypes;
- return BicycleTypes;
- }
-
- public async Task<BicycleType[]> GetBrands() {
- Brand[] brands = await GetBrandsFromStorage();
- Brands = (brands != null && brands.Length > 0) ? brands : Defaults.Brands;
- return BicycleTypes;
- }
-
- 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 (str != null) {
- result = System.Text.Json.JsonSerializer.Deserialize<ColorItem[]>(str) ?? Array.Empty<ColorItem>();
- } else {
- result = Array.Empty<ColorItem>();
- }
- _colors = result;
- 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 (str != null) {
- result = System.Text.Json.JsonSerializer.Deserialize<BicycleType[]>(str) ?? Array.Empty<BicycleType>();
- } else {
- result = Array.Empty<BicycleType>();
- }
- _bicycleTypes = result;
- 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 (str != null) {
- result = System.Text.Json.JsonSerializer.Deserialize<Brand[]>(str) ?? Array.Empty<Brand>();
- } else {
- result = Array.Empty<Brand>();
- }
- _brands = 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);
- }
-
- private async Task SaveBrandsToStorage() {
- var json = System.Text.Json.JsonSerializer.Serialize(_brands);
- await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBrands, 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);
- }
- }
- }
- }
|