PWA Fundvelo der Caritas.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MasterDataService.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. }
  115. }
  116. private async ValueTask<ColorItem[]> GetColorsFromStorage() {
  117. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  118. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  119. if (!_initializedColors) {
  120. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  121. var reference = DotNetObjectReference.Create(this);
  122. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  123. _initializedColors = true;
  124. }
  125. // Read the JSON string that contains the data from the local storage
  126. ColorItem[] result;
  127. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameColors);
  128. if (String.IsNullOrEmpty(str)) {
  129. result = Array.Empty<ColorItem>();
  130. } else {
  131. result = JsonConvert.DeserializeObject<ColorItem[]>(str) ?? Array.Empty<ColorItem>();
  132. }
  133. return result;
  134. }
  135. private async ValueTask<BicycleType[]> GetBicycleTypesFromStorage() {
  136. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  137. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  138. if (!_initializedBcTypes) {
  139. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  140. var reference = DotNetObjectReference.Create(this);
  141. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  142. _initializedBcTypes = true;
  143. }
  144. // Read the JSON string that contains the data from the local storage
  145. BicycleType[] result;
  146. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameBcTypes);
  147. if (String.IsNullOrEmpty(str)) {
  148. result = Array.Empty<BicycleType>();
  149. } else {
  150. result = JsonConvert.DeserializeObject<BicycleType[]>(str) ?? Array.Empty<BicycleType>();
  151. }
  152. return result;
  153. }
  154. private async ValueTask<Brand[]> GetBrandsFromStorage() {
  155. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  156. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  157. if (!_initializedBrands) {
  158. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  159. var reference = DotNetObjectReference.Create(this);
  160. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  161. _initializedBrands = true;
  162. }
  163. // Read the JSON string that contains the data from the local storage
  164. Brand[] result;
  165. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameBrands);
  166. if (String.IsNullOrEmpty(str)) {
  167. result = Array.Empty<Brand>();
  168. } else {
  169. result = JsonConvert.DeserializeObject<Brand[]>(str) ?? Array.Empty<Brand>();
  170. }
  171. return result;
  172. }
  173. private async Task SaveColorsToStorage(ColorItem[] colors) {
  174. var json = JsonConvert.SerializeObject(colors);
  175. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json);
  176. }
  177. private async Task SaveBcTypesToStorage(BicycleType[] bicycleTypes) {
  178. var json = JsonConvert.SerializeObject(bicycleTypes);
  179. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBcTypes, json);
  180. }
  181. private async Task SaveBrandsToStorage(Brand[] brands) {
  182. var json = JsonConvert.SerializeObject(brands);
  183. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBrands, json);
  184. }
  185. private static List<ColorItem> SortColors(List<ColorItem> cols) {
  186. cols.Sort(delegate (ColorItem c1, ColorItem c2) { return c1.Id == 0 ? -1 : c2.Id == 0 ? 1 : c1.Bezeichnung.CompareTo(c2.Bezeichnung); });
  187. return cols;
  188. }
  189. private static List<BicycleType> SortBicycleTypes(List<BicycleType> bcTypes) {
  190. bcTypes.Sort(delegate (BicycleType bct1, BicycleType bct2) { return bct1.Id == 0 ? -1 : bct2.Id == 0 ? 1 : bct1.Bezeichnung.CompareTo(bct2.Bezeichnung); });
  191. return bcTypes;
  192. }
  193. private static List<Brand> SortBrands(List<Brand> brands) {
  194. brands.Sort(delegate (Brand b1, Brand b2) { return b1.Id == 0 ? -1 : b2.Id == 0 ? 1 : b1.Bezeichnung.CompareTo(b2.Bezeichnung); });
  195. return brands;
  196. }
  197. }
  198. }