PWA Fundvelo der Caritas.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UserDataProvider.cs 3.1KB

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