PWA Fundvelo der Caritas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserDataProvider.cs 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. public static void mapMissingReport(MissingReport report, UserData userData) {
  66. report.Anrede = userData.Salutation;
  67. report.Vorname = userData.Firstname;
  68. report.Nachname = userData.Lastname;
  69. report.Telefon = userData.Phone;
  70. report.PersonOrt = userData.City;
  71. report.PersonPLZ = userData.Zip;
  72. report.PersonStrasse = userData.Address;
  73. report.Mobil = userData.Phone;
  74. report.Mail = userData.Email;
  75. }
  76. }
  77. }