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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 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 = 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. }