PWA Fundvelo der Caritas.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UserDataProvider.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. public event EventHandler Changed;
  9. private const string KeyName = "account";
  10. private readonly IJSRuntime jsRuntime;
  11. private bool initialized;
  12. public UserDataProvider(IJSRuntime _jsRuntime) {
  13. this.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 (!this.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. this.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 = JsonConvert.DeserializeObject<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 = JsonConvert.SerializeObject(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. userData.Mobile = report.Mobil;
  52. userData.Email = report.Mail;
  53. }
  54. public static void MapReport(Report report, UserData userData) {
  55. report.Anrede = userData.Salutation;
  56. report.Vorname = userData.Firstname;
  57. report.Nachname = userData.Lastname;
  58. report.Telefon = userData.Phone;
  59. report.Mobil = userData.Mobile;
  60. report.Mail = userData.Email;
  61. }
  62. public static void MapReport(Report report, char Salutation, string Firstname, string Lastname, string Phone) {
  63. report.Anrede = new string(Salutation, 1);
  64. report.Vorname = Firstname;
  65. report.Nachname = Lastname;
  66. report.Telefon = Phone;
  67. }
  68. public static void MapMissingReport(MissingReport report, UserData userData) {
  69. report.Anrede = userData.Salutation;
  70. report.Vorname = userData.Firstname;
  71. report.Nachname = userData.Lastname;
  72. report.Telefon = userData.Phone;
  73. report.PersonOrt = userData.City;
  74. report.PersonPLZ = userData.Zip;
  75. report.PersonStrasse = userData.Address;
  76. report.Postfach = userData.Postbox;
  77. report.Mobil = userData.Mobile;
  78. report.Mail = userData.Email;
  79. }
  80. }
  81. }