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

MasterDataService.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using CaritasPWA.Shared.Models;
  6. using Microsoft.AspNetCore.Components;
  7. using Microsoft.JSInterop;
  8. namespace CaritasPWA.Shared {
  9. public class MasterDataService {
  10. private ILFBicycleRest _lFBicycleRest;
  11. private const string KeyNameColors = "colors";
  12. private const string KeyNameBcTypes = "bicycleTypes";
  13. private readonly IJSRuntime _jsRuntime;
  14. private bool _initializedColors;
  15. private bool _initializedBcTypes;
  16. private ColorItem[] _colors = new ColorItem[] { };
  17. private BicycleType[] _bicycleTypes = new BicycleType[] { };
  18. public ColorItem[] Colors {
  19. get => _colors;
  20. set => _colors = value;
  21. }
  22. public BicycleType[] BicycleTypes {
  23. get => _bicycleTypes;
  24. set => _bicycleTypes = value;
  25. }
  26. public event EventHandler Changed;
  27. public MasterDataService(IJSRuntime jsRuntime, ILFBicycleRest lFBicycleRest) {
  28. _jsRuntime = jsRuntime;
  29. _lFBicycleRest = lFBicycleRest;
  30. }
  31. public async Task SynchronizeMasterdata() {
  32. await SynchronizeColors();
  33. await SynchronizeBcTypes();
  34. }
  35. public async Task SynchronizeColors() {
  36. Colors = _lFBicycleRest.GetColors().ToArray();
  37. if (Colors != null && Colors.Length > 0) {
  38. await SaveColorsToStorage();
  39. };
  40. }
  41. public async Task SynchronizeBcTypes() {
  42. BicycleTypes = _lFBicycleRest.GetBicycleTypes().ToArray();
  43. if (BicycleTypes != null && BicycleTypes.Length > 0) {
  44. await SaveBcTypesToStorage();
  45. }
  46. }
  47. public async Task<ColorItem[]> GetColors() {
  48. ColorItem[] colors = await GetColorsFromStorage();
  49. Colors = (colors != null && colors.Length > 0) ? colors : Defaults.ColorItems;
  50. return Colors;
  51. }
  52. public async Task<BicycleType[]> GetBicycleTypes() {
  53. BicycleType[] bicycleTypes = await GetBicycleTypesFromStorage();
  54. BicycleTypes = (bicycleTypes != null && bicycleTypes.Length > 0) ? bicycleTypes : Defaults.BycicleTypes;
  55. return BicycleTypes;
  56. }
  57. private async ValueTask<ColorItem[]> GetColorsFromStorage() {
  58. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  59. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  60. if (!_initializedColors) {
  61. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  62. var reference = DotNetObjectReference.Create(this);
  63. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  64. _initializedColors = true;
  65. }
  66. // Read the JSON string that contains the data from the local storage
  67. ColorItem[] result;
  68. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameColors);
  69. if (str != null) {
  70. result = System.Text.Json.JsonSerializer.Deserialize<ColorItem[]>(str) ?? new ColorItem[] { };
  71. } else {
  72. result = new ColorItem[] { };
  73. }
  74. _colors= result;
  75. return result;
  76. }
  77. private async ValueTask<BicycleType[]> GetBicycleTypesFromStorage() {
  78. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  79. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  80. if (!_initializedBcTypes) {
  81. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  82. var reference = DotNetObjectReference.Create(this);
  83. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  84. _initializedBcTypes = true;
  85. }
  86. // Read the JSON string that contains the data from the local storage
  87. BicycleType[] result;
  88. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameBcTypes);
  89. if (str != null) {
  90. result = System.Text.Json.JsonSerializer.Deserialize<BicycleType[]>(str) ?? new BicycleType[] { };
  91. } else {
  92. result = new BicycleType[] { };
  93. }
  94. _bicycleTypes = result;
  95. return result;
  96. }
  97. private async Task SaveColorsToStorage() {
  98. var json = System.Text.Json.JsonSerializer.Serialize(_colors);
  99. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json);
  100. }
  101. private async Task SaveBcTypesToStorage() {
  102. var json = System.Text.Json.JsonSerializer.Serialize(_bicycleTypes);
  103. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameBcTypes, json);
  104. }
  105. // This method is called from BlazorRegisterStorageEvent when the storage changed
  106. [JSInvokable]
  107. public void OnStorageUpdated(string key) {
  108. // Reset the settings. The next call to Get will reload the data
  109. if (key == KeyNameColors) {
  110. _colors = null;
  111. Changed?.Invoke(this, EventArgs.Empty);
  112. } else if (key == KeyNameBcTypes) {
  113. _bicycleTypes = null;
  114. Changed?.Invoke(this, EventArgs.Empty);
  115. }
  116. }
  117. }
  118. }