| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using cwebplusApp.Shared.Models;
- using Microsoft.JSInterop;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
-
- namespace cwebplusApp.Shared.Services {
- public sealed class ReportRepositoryService {
-
- private const string KeyNameFoundReports = "foundReportRepository";
- private const string KeyNameMissingReports = "missingReportRepository";
- private readonly IJSRuntime jsRuntime;
- private bool initialized;
-
- public event EventHandler Changed;
-
- public ReportRepositoryService(IJSRuntime jsRuntime) {
- this.jsRuntime = jsRuntime;
- }
-
- public async Task SaveReport(ReportRepositoryItem reportRepositoryItem) {
- if (reportRepositoryItem is FoundReportRepositoryItem fItem) {
- await SaveFoundReport(fItem);
- } else if (reportRepositoryItem is MissingReportRepositoryItem mItem) {
- await SaveMissingReport(mItem);
- }
- }
-
- public async ValueTask<List<FoundReportRepositoryItem>> GetFoundReports() {
- // 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 (!initialized) {
- // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
- var reference = DotNetObjectReference.Create(this);
- await this.jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
- this.initialized = true;
- }
-
- // Read the JSON string that contains the data from the local storage
- List<FoundReportRepositoryItem> result;
- var str = await this.jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameFoundReports);
- if (str != null) {
- result = JsonConvert.DeserializeObject<List<FoundReportRepositoryItem>>(str) ?? new();
- } else {
- result = new();
- }
- return result;
- }
-
- public async ValueTask<List<FoundReportRepositoryItem>> GetPendingFoundReports() {
- List<FoundReportRepositoryItem> pendingFoundReps = new();
- List<FoundReportRepositoryItem> foundReps = await GetFoundReports();
- foreach (FoundReportRepositoryItem report in foundReps) {
- if (!report.Transmitted) {
- pendingFoundReps.Add(report);
- }
- }
- return pendingFoundReps;
- }
-
- public async ValueTask<List<MissingReportRepositoryItem>> GetMissingReports() {
- // 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 (!initialized) {
- // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
- var reference = DotNetObjectReference.Create(this);
- await this.jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
- this.initialized = true;
- }
-
- // Read the JSON string that contains the data from the local storage
- List<MissingReportRepositoryItem> result;
- var str = await this.jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameMissingReports);
- if (str != null) {
- result = JsonConvert.DeserializeObject<List<MissingReportRepositoryItem>>(str) ?? new();
- } else {
- result = new();
- }
- return result;
- }
-
- public async ValueTask<List<MissingReportRepositoryItem>> GetPendingMissingReports() {
- List<MissingReportRepositoryItem> pendingMissingReps = new();
- List<MissingReportRepositoryItem> missingReps = await GetMissingReports();
- foreach (MissingReportRepositoryItem report in missingReps) {
- if (!report.Transmitted) {
- pendingMissingReps.Add(report);
- }
- }
- return pendingMissingReps;
- }
-
- public async Task SaveFoundReport(FoundReportRepositoryItem foundReportRepositoryItem) {
- List<FoundReportRepositoryItem> foundReps = await GetFoundReports();
- foundReps.Remove(foundReportRepositoryItem);
- foundReps.Add(foundReportRepositoryItem);
- var json = JsonConvert.SerializeObject(foundReps);
- await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameFoundReports, json);
- }
-
- public async Task SaveMissingReport(MissingReportRepositoryItem missingReportRepositoryItem) {
- List<MissingReportRepositoryItem> missingReps = await GetMissingReports();
- missingReps.Remove(missingReportRepositoryItem);
- missingReps.Add(missingReportRepositoryItem);
- var json = JsonConvert.SerializeObject(missingReps);
- await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameMissingReports, json);
- }
-
- // This method is called from BlazorRegisterStorageEvent when the storage changed
- [JSInvokable]
- public void OnStorageUpdated(string key) {
- if (key == KeyNameMissingReports || key == KeyNameFoundReports) {
- // Reset the settings. The next call to Get will reload the data
- Changed?.Invoke(this, EventArgs.Empty);
- }
- }
- }
- }
|