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

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