| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- 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<Resources> 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<Resources> _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<ColorItem[]> GetColors() {
- ColorItem[] _colors = await GetColorsFromStorage();
- if (_colors != null && _colors.Length > 0) {
- this.colors = _colors;
- }
- this.colors = SortColors(new List<ColorItem>(this.colors)).ToArray();
- return Colors;
- }
-
- public async Task<BicycleType[]> GetBicycleTypes() {
- BicycleType[] _bicycleTypes = await GetBicycleTypesFromStorage();
- if (_bicycleTypes != null && _bicycleTypes.Length > 0) {
- this.bicycleTypes = _bicycleTypes;
- }
- this.bicycleTypes = SortBicycleTypes(new List<BicycleType>(this.bicycleTypes)).ToArray();
- return BicycleTypes;
- }
-
- public async Task<Brand[]> GetBrands() {
- Brand[] _brands = await GetBrandsFromStorage();
- if (_brands != null && _brands.Length > 0) {
- this.brands = _brands;
- }
- this.brands = SortBrands(new List<Brand>(this.brands)).ToArray();
- return Brands;
- }
-
- public async Task<SearchService[]> GetSearchServices() {
- SearchService[] _searchServices = await GetSearchServicesFromStorage();
- if (_searchServices != null && _searchServices.Length > 0) {
- this.searchServices = _searchServices;
- }
- this.searchServices = SortSearchServices(new List<SearchService>(this.searchServices)).ToArray();
- return SearchServices;
- }
-
- public async Task<ZipCity[]> GetZipCities() {
- ZipCity[] _zipCities = await GetZipCitiesFromStorage();
- if (_zipCities != null && _zipCities.Length > 0) {
- this.zipCities = _zipCities;
- }
- this.zipCities = SortZipCities(new List<ZipCity>(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<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);
- this.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 = JsonConvert.DeserializeObject<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);
- this.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 = JsonConvert.DeserializeObject<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);
- this.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 = JsonConvert.DeserializeObject<Brand[]>(str) ?? Array.Empty<Brand>();
- }
- return result;
- }
-
- private async ValueTask<SearchService[]> 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<string>("BlazorGetLocalStorage", KeyNameSearchservices);
- if (String.IsNullOrEmpty(str)) {
- result = Array.Empty<SearchService>();
- } else {
- result = JsonConvert.DeserializeObject<SearchService[]>(str) ?? Array.Empty<SearchService>();
- }
- return result;
- }
-
- private async ValueTask<ZipCity[]> 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<string>("BlazorGetLocalStorage", KeyNameZipCities);
- if (String.IsNullOrEmpty(str)) {
- result = Array.Empty<ZipCity>();
- } else {
- result = JsonConvert.DeserializeObject<ZipCity[]>(str) ?? Array.Empty<ZipCity>();
- }
- 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<ColorItem> SortColors(List<ColorItem> 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<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;
- }
-
- private static List<SearchService> SortSearchServices(List<SearchService> 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<ZipCity> SortZipCities(List<ZipCity> 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;
- }
- }
- }
|