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

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