| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using cwebplusApp.Shared.Models;
- using Microsoft.JSInterop;
- using Newtonsoft.Json;
- using System;
- using System.Threading.Tasks;
-
- namespace cwebplusApp.Shared.Services {
- public sealed class UserDataProvider {
-
- public event EventHandler Changed;
-
- private const string KeyName = "account";
- private readonly IJSRuntime jsRuntime;
- private bool initialized;
-
-
- public UserDataProvider(IJSRuntime _jsRuntime) {
- this.jsRuntime = _jsRuntime;
- }
-
- public async ValueTask<UserData> Get() {
-
- // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
- // This way, you can reload the settings when another instance of the application (tab / window) save the settings
- if (!this.initialized) {
- // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
- var reference = DotNetObjectReference.Create(this);
- await jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
- this.initialized = true;
- }
-
- // Read the JSON string that contains the data from the local storage
- UserData result;
- var str = await jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyName);
- if (str != null) {
- result = JsonConvert.DeserializeObject<UserData>(str) ?? new UserData();
- } else {
- result = new UserData();
- }
- return result;
- }
-
- public async Task Save(UserData data) {
- var json = JsonConvert.SerializeObject(data);
- await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyName, json);
- }
-
- // This method is called from BlazorRegisterStorageEvent when the storage changed
- [JSInvokable]
- public void OnStorageUpdated(string key) {
- if (key == KeyName) {
- // Reset the settings. The next call to Get will reload the data
- Changed?.Invoke(this, EventArgs.Empty);
- }
- }
-
- public static void MapUserData(UserData userData, Report report) {
- userData.Salutation = report.Anrede;
- userData.Firstname = report.Vorname;
- userData.Lastname = report.Nachname;
- userData.Phone = report.Telefon;
- userData.Mobile = report.Mobil;
- userData.Email = report.Mail;
- }
-
- public static void MapReport(Report report, UserData userData) {
- report.Anrede = userData.Salutation;
- report.Vorname = userData.Firstname;
- report.Nachname = userData.Lastname;
- report.Telefon = userData.Phone;
- report.Mobil = userData.Mobile;
- report.Mail = userData.Email;
- }
-
- public static void MapReport(Report report, char Salutation, string Firstname, string Lastname, string Phone) {
- report.Anrede = new string(Salutation, 1);
- report.Vorname = Firstname;
- report.Nachname = Lastname;
- report.Telefon = Phone;
- }
-
- public static void MapMissingReport(MissingReport report, UserData userData) {
- report.Anrede = userData.Salutation;
- report.Vorname = userData.Firstname;
- report.Nachname = userData.Lastname;
- report.Telefon = userData.Phone;
- report.PersonOrt = userData.City;
- report.PersonPLZ = userData.Zip;
- report.PersonStrasse = userData.Address;
- report.Postfach = userData.Postbox;
- report.Mobil = userData.Mobile;
- report.Mail = userData.Email;
- }
-
- }
- }
|