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

MasterDataService.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 const string KeyNameSearchservices = "searchServices";
  19. private bool _initializedColors;
  20. private bool _initializedBcTypes;
  21. private bool _initializedBrands;
  22. private bool _initializedSearchServices;
  23. private bool _firstActivation;
  24. public bool FirstActivation { get => _firstActivation; }
  25. private ColorItem[] _colors;
  26. private BicycleType[] _bicycleTypes;
  27. private Brand[] _brands;
  28. private SearchService[] _searchServices;
  29. public ColorItem[] Colors {
  30. get => _colors;
  31. }
  32. public BicycleType[] BicycleTypes {
  33. get => _bicycleTypes;
  34. }
  35. public Brand[] Brands {
  36. get => _brands;
  37. }
  38. public SearchService[] SearchServices {
  39. get => _searchServices;
  40. }
  41. public event EventHandler Changed;
  42. public MasterDataService(IJSRuntime jsRuntime, IBicycleRestService bicycleRestService, IStringLocalizer<Resources> i18n, OnlineStatusProvider onlineStatusProvider) {
  43. _jsRuntime = jsRuntime;
  44. _bicycleRestService = bicycleRestService;
  45. _i18n = i18n;
  46. _onlineStatusProvider = onlineStatusProvider;
  47. _firstActivation = true;
  48. _brands = Defaults.GetBrandDefaults(_i18n).ToArray();
  49. _colors = Defaults.GetColorDefaults(_i18n).ToArray();
  50. _bicycleTypes = Defaults.GetBicycleTypeDefaults(_i18n).ToArray();
  51. _searchServices = Defaults.GetSearchServiceDefaults(_i18n).ToArray();
  52. }
  53. public async Task SynchronizeMasterdata() {
  54. if (_onlineStatusProvider.Online) {
  55. try {
  56. await SynchronizeColors();
  57. await SynchronizeBcTypes();
  58. await SynchronizeBrands();
  59. await SynchronizeSearchServices();
  60. } finally {
  61. _firstActivation = false;
  62. }
  63. } else {
  64. throw new Exception("No internet connection available!");
  65. }
  66. }
  67. public async Task SynchronizeColors() {
  68. ColorItem[] colors = (await _bicycleRestService.GetColors()).ToArray();
  69. if (colors != null && colors.Length > 0) {
  70. _colors = colors;
  71. await SaveColorsToStorage(colors);
  72. };
  73. }
  74. public async Task SynchronizeBcTypes() {
  75. BicycleType[] bicycleTypes = (await _bicycleRestService.GetBicycleTypes()).ToArray();
  76. if (bicycleTypes != null && bicycleTypes.Length > 0) {
  77. _bicycleTypes = bicycleTypes;
  78. await SaveBcTypesToStorage(bicycleTypes);
  79. }
  80. }
  81. public async Task SynchronizeBrands() {
  82. Brand[] brands = (await _bicycleRestService.GetBrands()).ToArray();
  83. if (brands != null && brands.Length > 0) {
  84. _brands = brands;
  85. await SaveBrandsToStorage(brands);
  86. }
  87. }
  88. public async Task SynchronizeSearchServices() {
  89. SearchService[] searchServices = (await _bicycleRestService.GetSearchServices()).ToArray();
  90. if (searchServices != null && searchServices.Length > 0) {
  91. _searchServices = searchServices;
  92. await SaveSearchServicesToStorage(searchServices);
  93. }
  94. }
  95. public async Task<ColorItem[]> GetColors() {
  96. ColorItem[] colors = await GetColorsFromStorage();
  97. if (colors != null && colors.Length > 0) {
  98. _colors = colors;
  99. }
  100. _colors = SortColors(new List<ColorItem>(_colors)).ToArray();
  101. return Colors;
  102. }
  103. public async Task<BicycleType[]> GetBicycleTypes() {
  104. BicycleType[] bicycleTypes = await GetBicycleTypesFromStorage();
  105. if (bicycleTypes != null && bicycleTypes.Length > 0) {
  106. _bicycleTypes = bicycleTypes;
  107. }
  108. _bicycleTypes = SortBicycleTypes(new List<BicycleType>(_bicycleTypes)).ToArray();
  109. return BicycleTypes;
  110. }
  111. public async Task<Brand[]> GetBrands() {
  112. Brand[] brands = await GetBrandsFromStorage();
  113. if (brands != null && brands.Length > 0) {
  114. _brands = brands;
  115. }
  116. _brands = SortBrands(new List<Brand>(_brands)).ToArray();
  117. return Brands;
  118. }
  119. public async Task<SearchService[]> GetSearchServices() {
  120. SearchService[] searchServices = await GetSearchServicesFromStorage();
  121. if (searchServices != null && searchServices.Length > 0) {
  122. _searchServices = searchServices;
  123. }
  124. _searchServices = SortSearchServices(new List<SearchService>(_searchServices)).ToArray();
  125. return SearchServices;
  126. }
  127. // This method is called from BlazorRegisterStorageEvent when the storage changed
  128. [JSInvokable]
  129. public void OnStorageUpdated(string key) {
  130. // Reset the settings. The next call to Get will reload the data
  131. if (key == KeyNameColors) {
  132. _colors = null;
  133. Changed?.Invoke(this, EventArgs.Empty);
  134. } else if (key == KeyNameBcTypes) {
  135. _bicycleTypes = null;
  136. Changed?.Invoke(this, EventArgs.Empty);
  137. } else if (key == KeyNameBrands) {
  138. _brands = null;
  139. Changed?.Invoke(this, EventArgs.Empty);
  140. }
  141. }
  142. private async ValueTask<ColorItem[]> GetColorsFromStorage() {
  143. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  144. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  145. if (!_initializedColors) {
  146. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  147. var reference = DotNetObjectReference.Create(this);
  148. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  149. _initializedColors = true;
  150. }
  151. // Read the JSON string that contains the data from the local storage
  152. ColorItem[] result;
  153. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameColors);
  154. if (String.IsNullOrEmpty(str)) {
  155. result = Array.Empty<ColorItem>();
  156. } else {
  157. result = JsonConvert.DeserializeObject<ColorItem[]>(str) ?? Array.Empty<ColorItem>();
  158. }
  159. return result;
  160. }
  161. private async ValueTask<BicycleType[]> GetBicycleTypesFromStorage() {
  162. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  163. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  164. if (!_initializedBcTypes) {
  165. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  166. var reference = DotNetObjectReference.Create(this);
  167. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  168. _initializedBcTypes = true;
  169. }
  170. // Read the JSON string that contains the data from the local storage
  171. BicycleType[] result;
  172. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameBcTypes);
  173. if (String.IsNullOrEmpty(str)) {
  174. result = Array.Empty<BicycleType>();
  175. } else {
  176. result = JsonConvert.DeserializeObject<BicycleType[]>(str) ?? Array.Empty<BicycleType>();
  177. }
  178. return result;
  179. }
  180. private async ValueTask<Brand[]> GetBrandsFromStorage() {
  181. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  182. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  183. if (!_initializedBrands) {
  184. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  185. var reference = DotNetObjectReference.Create(this);
  186. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  187. _initializedBrands = true;
  188. }
  189. // Read the JSON string that contains the data from the local storage
  190. Brand[] result;
  191. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameBrands);
  192. if (String.IsNullOrEmpty(str)) {
  193. result = Array.Empty<Brand>();
  194. } else {
  195. result = JsonConvert.DeserializeObject<Brand[]>(str) ?? Array.Empty<Brand>();
  196. }
  197. return result;
  198. }
  199. private async ValueTask<SearchService[]> GetSearchServicesFromStorage() {
  200. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  201. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  202. if (!_initializedSearchServices) {
  203. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  204. var reference = DotNetObjectReference.Create(this);
  205. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  206. _initializedSearchServices = true;
  207. }
  208. // Read the JSON string that contains the data from the local storage
  209. SearchService[] result;
  210. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameSearchservices);
  211. if (String.IsNullOrEmpty(str)) {
  212. result = Array.Empty<SearchService>();
  213. } else {
  214. result = JsonConvert.DeserializeObject<SearchService[]>(str) ?? Array.Empty<SearchService>();
  215. }
  216. return result;
  217. }
  218. private async Task SaveColorsToStorage(ColorItem[] colors) {
  219. var json = JsonConvert.SerializeObject(colors);
  220. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json);
  221. }
  222. private async Task SaveBcTypesToStorage(BicycleType[] bicycleTypes) {
  223. var json = JsonConvert.SerializeObject(bicycleTypes);
  224. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBcTypes, json);
  225. }
  226. private async Task SaveBrandsToStorage(Brand[] brands) {
  227. var json = JsonConvert.SerializeObject(brands);
  228. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBrands, json);
  229. }
  230. private async Task SaveSearchServicesToStorage(SearchService[] searchServices) {
  231. var json = JsonConvert.SerializeObject(searchServices);
  232. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameSearchservices, json);
  233. }
  234. private static List<ColorItem> SortColors(List<ColorItem> cols) {
  235. cols.Sort(delegate (ColorItem c1, ColorItem c2) { return c1.Id == 0 ? -1 : c2.Id == 0 ? 1 : c1.Bezeichnung.CompareTo(c2.Bezeichnung); });
  236. return cols;
  237. }
  238. private static List<BicycleType> SortBicycleTypes(List<BicycleType> bcTypes) {
  239. bcTypes.Sort(delegate (BicycleType bct1, BicycleType bct2) { return bct1.Id == 0 ? -1 : bct2.Id == 0 ? 1 : bct1.Bezeichnung.CompareTo(bct2.Bezeichnung); });
  240. return bcTypes;
  241. }
  242. private static List<Brand> SortBrands(List<Brand> brands) {
  243. brands.Sort(delegate (Brand b1, Brand b2) { return b1.Id == 0 ? -1 : b2.Id == 0 ? 1 : b1.Bezeichnung.CompareTo(b2.Bezeichnung); });
  244. return brands;
  245. }
  246. private static List<SearchService> SortSearchServices(List<SearchService> searchServices) {
  247. searchServices.Sort(delegate (SearchService ss1, SearchService ss2) { return ss1.Id == 0 ? -1 : ss2.Id == 0 ? 1 : ss1.Bezeichnung.CompareTo(ss2.Bezeichnung); });
  248. return searchServices;
  249. }
  250. }
  251. }