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.7KB

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