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.

ReportRepositoryService.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using cwebplusApp.Shared.Models;
  2. using Microsoft.JSInterop;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading.Tasks;
  7. namespace cwebplusApp.Shared.Services {
  8. public sealed class ReportRepositoryService {
  9. private const string KeyNameFoundReports = "foundReportRepository";
  10. private const string KeyNameMissingReports = "missingReportRepository";
  11. private readonly IJSRuntime jsRuntime;
  12. private bool initialized;
  13. public event EventHandler Changed;
  14. public ReportRepositoryService(IJSRuntime jsRuntime) {
  15. this.jsRuntime = jsRuntime;
  16. }
  17. public async Task SaveReport(ReportRepositoryItem reportRepositoryItem) {
  18. if (reportRepositoryItem is FoundReportRepositoryItem fItem) {
  19. await SaveFoundReport(fItem);
  20. } else if (reportRepositoryItem is MissingReportRepositoryItem mItem) {
  21. await SaveMissingReport(mItem);
  22. }
  23. }
  24. public async ValueTask<List<FoundReportRepositoryItem>> GetFoundReports() {
  25. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  26. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  27. if (!initialized) {
  28. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  29. var reference = DotNetObjectReference.Create(this);
  30. await this.jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  31. this.initialized = true;
  32. }
  33. // Read the JSON string that contains the data from the local storage
  34. List<FoundReportRepositoryItem> result;
  35. var str = await this.jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameFoundReports);
  36. if (str != null) {
  37. result = JsonConvert.DeserializeObject<List<FoundReportRepositoryItem>>(str) ?? new();
  38. } else {
  39. result = new();
  40. }
  41. return result;
  42. }
  43. public async ValueTask<List<FoundReportRepositoryItem>> GetPendingFoundReports() {
  44. List<FoundReportRepositoryItem> pendingFoundReps = new();
  45. List<FoundReportRepositoryItem> foundReps = await GetFoundReports();
  46. foreach (FoundReportRepositoryItem report in foundReps) {
  47. if (!report.Transmitted) {
  48. pendingFoundReps.Add(report);
  49. }
  50. }
  51. return pendingFoundReps;
  52. }
  53. public async ValueTask<List<MissingReportRepositoryItem>> GetMissingReports() {
  54. // Register the Storage event handler. This handler calls OnStorageUpdated when the storage changed.
  55. // This way, you can reload the settings when another instance of the application (tab / window) save the settings
  56. if (!initialized) {
  57. // Create a reference to the current object, so the JS function can call the public method "OnStorageUpdated"
  58. var reference = DotNetObjectReference.Create(this);
  59. await this.jsRuntime.InvokeVoidAsync("BlazorRegisterStorageEvent", reference);
  60. this.initialized = true;
  61. }
  62. // Read the JSON string that contains the data from the local storage
  63. List<MissingReportRepositoryItem> result;
  64. var str = await this.jsRuntime.InvokeAsync<string>("BlazorGetLocalStorage", KeyNameMissingReports);
  65. if (str != null) {
  66. result = JsonConvert.DeserializeObject<List<MissingReportRepositoryItem>>(str) ?? new();
  67. } else {
  68. result = new();
  69. }
  70. return result;
  71. }
  72. public async ValueTask<List<MissingReportRepositoryItem>> GetPendingMissingReports() {
  73. List<MissingReportRepositoryItem> pendingMissingReps = new();
  74. List<MissingReportRepositoryItem> missingReps = await GetMissingReports();
  75. foreach (MissingReportRepositoryItem report in missingReps) {
  76. if (!report.Transmitted) {
  77. pendingMissingReps.Add(report);
  78. }
  79. }
  80. return pendingMissingReps;
  81. }
  82. public async Task SaveFoundReport(FoundReportRepositoryItem foundReportRepositoryItem) {
  83. List<FoundReportRepositoryItem> foundReps = await GetFoundReports();
  84. foundReps.Remove(foundReportRepositoryItem);
  85. foundReps.Add(foundReportRepositoryItem);
  86. var json = JsonConvert.SerializeObject(foundReps);
  87. await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameFoundReports, json);
  88. }
  89. public async Task SaveMissingReport(MissingReportRepositoryItem missingReportRepositoryItem) {
  90. List<MissingReportRepositoryItem> missingReps = await GetMissingReports();
  91. missingReps.Remove(missingReportRepositoryItem);
  92. missingReps.Add(missingReportRepositoryItem);
  93. var json = JsonConvert.SerializeObject(missingReps);
  94. await jsRuntime.InvokeVoidAsync("BlazorSetLocalStorage", KeyNameMissingReports, json);
  95. }
  96. public async ValueTask<int> GetNbrOfPendingReports() {
  97. List<FoundReportRepositoryItem> foundReportRepositoryItems = await GetPendingFoundReports();
  98. List<MissingReportRepositoryItem> missingReportRepositoryItems = await GetPendingMissingReports();
  99. return foundReportRepositoryItems.Count + missingReportRepositoryItems.Count;
  100. }
  101. // This method is called from BlazorRegisterStorageEvent when the storage changed
  102. [JSInvokable]
  103. public void OnStorageUpdated(string key) {
  104. if (key == KeyNameMissingReports || key == KeyNameFoundReports) {
  105. // Reset the settings. The next call to Get will reload the data
  106. Changed?.Invoke(this, EventArgs.Empty);
  107. }
  108. }
  109. public static long GetCurrentTimeInMillis() {
  110. return DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
  111. }
  112. public static string GetCurrentDateTimeFromMillis(long millis) {
  113. TimeSpan time = TimeSpan.FromMilliseconds(millis);
  114. DateTime date = new DateTime() + time;
  115. return date.ToString("dd.MM.yyyy HH:mm:ss");
  116. }
  117. }
  118. }