PWA Fundvelo der Caritas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MasterDataService.cs 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using CaritasPWA.Shared.Models;
  2. using CaritasPWA.Shared.ResourceFiles;
  3. using Json.Net;
  4. using Microsoft.Extensions.Localization;
  5. using Microsoft.JSInterop;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Runtime.CompilerServices;
  9. using System.Threading.Tasks;
  10. namespace CaritasPWA.Shared.Services {
  11. public class MasterDataService {
  12. private readonly ILFBicycleRest _lFBicycleRest;
  13. private const string KeyNameColors = "colors";
  14. private const string KeyNameBcTypes = "bicycleTypes";
  15. private const string KeyNameBrands = "brands";
  16. private readonly IJSRuntime _jsRuntime;
  17. private readonly IStringLocalizer<Resources> _i18n;
  18. private bool _initializedColors;
  19. private bool _initializedBcTypes;
  20. private bool _initializedBrands;
  21. private bool _firstActivation;
  22. public bool FirstActivation { get => _firstActivation; }
  23. private ColorItem[] _colors;
  24. private BicycleType[] _bicycleTypes;
  25. private Brand[] _brands;
  26. public ColorItem[] Colors {
  27. get => _colors;
  28. set => _colors = value;
  29. }
  30. public BicycleType[] BicycleTypes {
  31. get => _bicycleTypes;
  32. set => _bicycleTypes = value;
  33. }
  34. public Brand[] Brands {
  35. get => _brands;
  36. set => _brands = value;
  37. }
  38. public event EventHandler Changed;
  39. public MasterDataService(IJSRuntime jsRuntime, ILFBicycleRest lFBicycleRest, IStringLocalizer<Resources> i18n) {
  40. _jsRuntime = jsRuntime;
  41. _lFBicycleRest = lFBicycleRest;
  42. _i18n = i18n;
  43. _firstActivation = true;
  44. }
  45. public async Task SynchronizeMasterdata() {
  46. await SynchronizeColors();
  47. await SynchronizeBcTypes();
  48. await SynchronizeBrands();
  49. _firstActivation = false;
  50. }
  51. public async Task SynchronizeColors() {
  52. Colors = (await _lFBicycleRest.GetColors()).ToArray();
  53. if (Colors != null && Colors.Length > 0) {
  54. await SaveColorsToStorage();
  55. };
  56. }
  57. public async Task SynchronizeBcTypes() {
  58. BicycleTypes = _lFBicycleRest.GetBicycleTypes().ToArray();
  59. if (BicycleTypes != null && BicycleTypes.Length > 0) {
  60. await SaveBcTypesToStorage();
  61. }
  62. }
  63. public async Task SynchronizeBrands() {
  64. Brands = _lFBicycleRest.GetBrands().ToArray();
  65. if (Brands != null && Brands.Length > 0) {
  66. await SaveBrandsToStorage();
  67. }
  68. }
  69. public async Task<ColorItem[]> GetColors() {
  70. ColorItem[] colors = await GetColorsFromStorage();
  71. Colors = (colors != null && colors.Length > 0) ? colors: Defaults.GetColorDefaults(_i18n).ToArray();
  72. Colors = SortColors(new List<ColorItem>(Colors)).ToArray();
  73. return Colors;
  74. }
  75. public async Task<BicycleType[]> GetBicycleTypes() {
  76. BicycleType[] bicycleTypes = await GetBicycleTypesFromStorage();
  77. BicycleTypes = (bicycleTypes != null && bicycleTypes.Length > 0) ? bicycleTypes : Defaults.GetBicycleTypeDefaults(_i18n).ToArray();
  78. BicycleTypes = SortBicycleTypes(new List<BicycleType>(BicycleTypes)).ToArray();
  79. return BicycleTypes;
  80. }
  81. public async Task<Brand[]> GetBrands() {
  82. Brand[] brands = await GetBrandsFromStorage();
  83. Brands = (brands != null && brands.Length > 0) ? brands : Defaults.GetBrandDefaults(_i18n).ToArray();
  84. Brands = SortBrands(new List<Brand>(Brands)).ToArray();
  85. return Brands;
  86. }
  87. // This method is called from BlazorRegisterStorageEvent when the storage changed
  88. [JSInvokable]
  89. public void OnStorageUpdated(string key) {
  90. // Reset the settings. The next call to Get will reload the data
  91. if (key == KeyNameColors) {
  92. _colors = null;
  93. Changed?.Invoke(this, EventArgs.Empty);
  94. } else if (key == KeyNameBcTypes) {
  95. _bicycleTypes = null;
  96. Changed?.Invoke(this, EventArgs.Empty);
  97. }
  98. }
  99. private async ValueTask<ColorItem[]> GetColorsFromStorage() {
  100. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  101. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  102. if (!_initializedColors) {
  103. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  104. var reference = DotNetObjectReference.Create(this);
  105. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  106. _initializedColors = true;
  107. }
  108. // Read the JSON string that contains the data from the local storage
  109. ColorItem[] result;
  110. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameColors);
  111. if (String.IsNullOrEmpty(str)) {
  112. result = Array.Empty<ColorItem>();
  113. } else {
  114. result = JsonNet.Deserialize<ColorItem[]>(str) ?? Array.Empty<ColorItem>();
  115. }
  116. _colors = result;
  117. return result;
  118. }
  119. private async ValueTask<BicycleType[]> GetBicycleTypesFromStorage() {
  120. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  121. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  122. if (!_initializedBcTypes) {
  123. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  124. var reference = DotNetObjectReference.Create(this);
  125. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  126. _initializedBcTypes = true;
  127. }
  128. // Read the JSON string that contains the data from the local storage
  129. BicycleType[] result;
  130. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameBcTypes);
  131. if (str != null) {
  132. result = JsonNet.Deserialize<BicycleType[]>(str) ?? Array.Empty<BicycleType>();
  133. } else {
  134. result = Array.Empty<BicycleType>();
  135. }
  136. _bicycleTypes = result;
  137. return result;
  138. }
  139. private async ValueTask<Brand[]> GetBrandsFromStorage() {
  140. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  141. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  142. if (!_initializedBrands) {
  143. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  144. var reference = DotNetObjectReference.Create(this);
  145. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  146. _initializedBrands = true;
  147. }
  148. // Read the JSON string that contains the data from the local storage
  149. Brand[] result;
  150. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameBrands);
  151. if (str != null) {
  152. result = JsonNet.Deserialize<Brand[]>(str) ?? Array.Empty<Brand>();
  153. } else {
  154. result = Array.Empty<Brand>();
  155. }
  156. _brands = result;
  157. return result;
  158. }
  159. private async Task SaveColorsToStorage() {
  160. var json = JsonNet.Serialize(_colors);
  161. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json);
  162. }
  163. private async Task SaveBcTypesToStorage() {
  164. var json = JsonNet.Serialize(_bicycleTypes);
  165. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBcTypes, json);
  166. }
  167. private async Task SaveBrandsToStorage() {
  168. var json = JsonNet.Serialize(_brands);
  169. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBrands, json);
  170. }
  171. private static List<ColorItem> SortColors(List<ColorItem> cols) {
  172. cols.Sort(delegate (ColorItem c1, ColorItem c2) { return c1.Id == 0 ? -1 : c2.Id == 0 ? 1 : c1.Bezeichnung.CompareTo(c2.Bezeichnung); });
  173. return cols;
  174. }
  175. private static List<BicycleType> SortBicycleTypes(List<BicycleType> bcTypes) {
  176. bcTypes.Sort(delegate (BicycleType bct1, BicycleType bct2) { return bct1.Id == 0 ? -1 : bct2.Id == 0 ? 1 : bct1.Bezeichnung.CompareTo(bct2.Bezeichnung); });
  177. return bcTypes;
  178. }
  179. private static List<Brand> SortBrands(List<Brand> brands) {
  180. brands.Sort(delegate (Brand b1, Brand b2) { return b1.Id == 0 ? -1 : b2.Id == 0 ? 1 : b1.Bezeichnung.CompareTo(b2.Bezeichnung); });
  181. return brands;
  182. }
  183. }
  184. }