PWA Fundvelo der Caritas.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MasterDataService.cs 9.6KB

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