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 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.JSInterop;
  7. namespace CaritasPWA.Shared {
  8. public class MasterDataService {
  9. private const string KeyNameColors = "colors";
  10. private const string KeyNameBcTypes = "bicycleTypes";
  11. private readonly IJSRuntime _jsRuntime;
  12. private bool _initializedColors;
  13. private ColorItem[] _colors = new ColorItem[] { };
  14. public ColorItem[] Colors {
  15. get => _colors;
  16. set => _colors = value;
  17. }
  18. public event EventHandler Changed;
  19. public MasterDataService(IJSRuntime jsRuntime) {
  20. _jsRuntime = jsRuntime;
  21. }
  22. public async Task SynchronizeColors() {
  23. // TODO: Get the color from server via REST and save it when not empty
  24. _colors = Defaults.ColorItems;
  25. await SaveColorsToStorage();
  26. }
  27. public async Task<ColorItem[]> GetColors() {
  28. // To search first in local storage, when nothing is found return the defaults.
  29. //return Defaults.ColorItems;
  30. return await GetColorsFromStorage();
  31. }
  32. private async ValueTask<ColorItem[]> GetColorsFromStorage() {
  33. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  34. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  35. if (!_initializedColors) {
  36. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  37. var reference = DotNetObjectReference.Create(this);
  38. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  39. _initializedColors = true;
  40. }
  41. // Read the JSON string that contains the data from the local storage
  42. ColorItem[] result;
  43. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameColors);
  44. if (str != null) {
  45. result = System.Text.Json.JsonSerializer.Deserialize<ColorItem[]>(str) ?? new ColorItem[] { };
  46. } else {
  47. result = new ColorItem[] { };
  48. }
  49. _colors= result;
  50. return result;
  51. }
  52. private async Task SaveColorsToStorage() {
  53. var json = System.Text.Json.JsonSerializer.Serialize(_colors);
  54. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameColors, json);
  55. }
  56. // This method is called from BlazorRegisterStorageEvent when the storage changed
  57. [JSInvokable]
  58. public void OnStorageUpdated(string key) {
  59. // Reset the settings. The next call to Get will reload the data
  60. if (key == KeyNameColors) {
  61. _colors = null;
  62. Changed?.Invoke(this, EventArgs.Empty);
  63. } else if (key == KeyNameBcTypes) {
  64. //_bicycleType = null;
  65. Changed?.Invoke(this, EventArgs.Empty);
  66. }
  67. }
  68. }
  69. }