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

MasterDataService.cs 14KB

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