PWA Fundvelo der Caritas.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

UserDataProvider.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using cwebplusApp.Shared.Models;
  2. using Microsoft.JSInterop;
  3. using System;
  4. using System.Threading.Tasks;
  5. namespace cwebplusApp.Shared.Services {
  6. public sealed class UserDataProvider {
  7. private const string KeyName = "account";
  8. private readonly IJSRuntime _jsRuntime;
  9. private bool _initialized;
  10. public event EventHandler Changed;
  11. public bool AutoSave { get; set; } = true;
  12. public UserDataProvider(IJSRuntime jsRuntime) {
  13. _jsRuntime = jsRuntime;
  14. }
  15. public async ValueTask<UserData> Get() {
  16. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  17. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  18. if (!_initialized) {
  19. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  20. var reference = DotNetObjectReference.Create(this);
  21. await _jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  22. _initialized = true;
  23. }
  24. // Read the JSON string that contains the data from the local storage
  25. UserData result;
  26. var str = await _jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyName);
  27. if (str != null) {
  28. result = System.Text.Json.JsonSerializer.Deserialize<UserData>(str) ?? new UserData();
  29. } else {
  30. result = new UserData();
  31. }
  32. return result;
  33. }
  34. public async Task Save(UserData _data) {
  35. var json = System.Text.Json.JsonSerializer.Serialize(_data);
  36. await _jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyName, json);
  37. }
  38. // This method is called from BlazorRegisterStorageEvent when the storage changed
  39. [JSInvokable]
  40. public void OnStorageUpdated(string key) {
  41. if (key == KeyName) {
  42. // Reset the settings. The next call to Get will reload the data
  43. Changed?.Invoke(this, EventArgs.Empty);
  44. }
  45. }
  46. public static void mapUserData(UserData userData, Report report) {
  47. userData.Salutation = report.Anrede;
  48. userData.Firstname = report.Vorname;
  49. userData.Lastname = report.Nachname; ;
  50. userData.Phone = report.Telefon;
  51. }
  52. public static void mapReport(Report report, UserData userData) {
  53. report.Anrede = userData.Salutation;
  54. report.Vorname = userData.Firstname;
  55. report.Nachname = userData.Lastname;
  56. report.Telefon = userData.Phone;
  57. }
  58. }
  59. }