using Microsoft.AspNetCore.Components; using Blazored.LocalStorage; using Blog3000.Shared; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Net.Http; using Markdig.Extensions.Tables; using Blog3000.Client.Services; using System.Security.Cryptography; using System.ComponentModel; using System.Data; using System.Net.Security; namespace Blog3000.Client { /// /// TODO: Add all localStorage Ops regarding BlogsPosts here /// - Updater /// - Topic-Fetcher /// - ... /// public class ViewStats : IDisposable { public static string DBKEY_PREFIX = "viewstats-"; private readonly ILocalStorageService localStorage; private readonly NetworkStatus networkStatus; private readonly HttpClient httpClient; public ViewStats(ILocalStorageService ls, NetworkStatus ns, HttpClient http) { this.localStorage = ls; this.networkStatus = ns; this.httpClient = http; localStorage.Changed += LocalStorage_Changed; } public void Dispose() { localStorage.Changed -= LocalStorage_Changed; } public async Task AddPostView(string postId) { var unsentStats = await localStorage.GetItemAsync>($"{DBKEY_PREFIX}local"); unsentStats ??= new Dictionary(); unsentStats.Remove(postId); unsentStats.Add(postId, 1); await localStorage.SetItemAsync($"{DBKEY_PREFIX}local", unsentStats); _ = SyncViewStats(); } public async Task GetPostViewCount(string postId) { var remoteStats = await localStorage.GetItemAsync>($"{DBKEY_PREFIX}remote"); var unsentStats = await localStorage.GetItemAsync>($"{DBKEY_PREFIX}local"); int remoteCount = 0; remoteStats?.TryGetValue(postId, out remoteCount); int localCount = 0; unsentStats?.TryGetValue(postId, out localCount); return localCount + remoteCount; } /// /// Fetch a locally cached post. Optionally fetch images from remote. Optionally fetch post from remote. /// /// /// /// /// public async Task SyncViewStats() { if (!networkStatus.IsOnline) { System.Diagnostics.Debug.WriteLine("SyncViewStats: Offline, not syncing"); return false; } var unsentStats = await localStorage.GetItemAsync>($"{DBKEY_PREFIX}local"); if ((unsentStats?.Count ?? 0) > 0) { try { var newData = await httpClient.PostJsonAsync>($"ViewStats", unsentStats); await localStorage.RemoveItemAsync($"{DBKEY_PREFIX}local"); await localStorage.SetItemAsync>($"{DBKEY_PREFIX}remote", newData); System.Diagnostics.Debug.WriteLine("SyncViewStats: Stats synced"); return true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"SyncViewStats: Sync failed: {ex.ToString()}"); } } else { System.Diagnostics.Debug.WriteLine("SyncViewStats: UnsetStats empty, not sending, only fetching"); } try { var newData = await httpClient.GetJsonAsync>($"ViewStats"); await localStorage.SetItemAsync>($"{DBKEY_PREFIX}remote", newData); System.Diagnostics.Debug.WriteLine("SyncViewStats: Stats fetched"); return true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"SyncViewStats: Fetch failed: {ex.ToString()}"); } return false; } private void LocalStorage_Changed(object sender, ChangedEventArgs e) { if (e.Key.StartsWith(ViewStats.DBKEY_PREFIX)) Task.Run(() => { // }); } } }