PWA Fundvelo der Caritas.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MasterDataService.cs 10KB

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