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

UserDataProvider.cs 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  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. public static void MapReport(Report report, char Salutation, string Firstname, string Lastname, string Phone) {
  59. report.Anrede = new string(Salutation, 1);
  60. report.Vorname = Firstname;
  61. report.Nachname = Lastname;
  62. report.Telefon = Phone;
  63. }
  64. public static void MapMissingReport(MissingReport report, UserData userData) {
  65. report.Anrede = userData.Salutation;
  66. report.Vorname = userData.Firstname;
  67. report.Nachname = userData.Lastname;
  68. report.Telefon = userData.Phone;
  69. report.PersonOrt = userData.City;
  70. report.PersonPLZ = userData.Zip;
  71. report.PersonStrasse = userData.Address;
  72. report.Mobil = userData.Phone;
  73. report.Mail = userData.Email;
  74. }
  75. }
  76. }