PWA Fundvelo der Caritas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MasterDataService.cs 13KB

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