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

MasterDataService.cs 5.7KB

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