138 lines
4.5 KiB
C#
138 lines
4.5 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// TODO: Add all localStorage Ops regarding BlogsPosts here
|
|
/// - Updater
|
|
/// - Topic-Fetcher
|
|
/// - ...
|
|
/// </summary>
|
|
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<Dictionary<string, int>>($"{DBKEY_PREFIX}local");
|
|
unsentStats ??= new Dictionary<string, int>();
|
|
unsentStats.Remove(postId);
|
|
unsentStats.Add(postId, 1);
|
|
await localStorage.SetItemAsync($"{DBKEY_PREFIX}local", unsentStats);
|
|
_ = SyncViewStats();
|
|
}
|
|
|
|
public async Task<int> GetPostViewCount(string postId)
|
|
{
|
|
var remoteStats = await localStorage.GetItemAsync<Dictionary<string, int>>($"{DBKEY_PREFIX}remote");
|
|
var unsentStats = await localStorage.GetItemAsync<Dictionary<string, int>>($"{DBKEY_PREFIX}local");
|
|
|
|
int remoteCount = 0;
|
|
remoteStats?.TryGetValue(postId, out remoteCount);
|
|
int localCount = 0;
|
|
unsentStats?.TryGetValue(postId, out localCount);
|
|
|
|
return localCount + remoteCount;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetch a locally cached post. Optionally fetch images from remote. Optionally fetch post from remote.
|
|
/// </summary>
|
|
/// <param name="postId"></param>
|
|
/// <param name="tryCacheImgs"></param>
|
|
/// <param name="tryRemote"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> SyncViewStats()
|
|
{
|
|
if (!networkStatus.IsOnline)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine("SyncViewStats: Offline, not syncing");
|
|
return false;
|
|
}
|
|
|
|
var unsentStats = await localStorage.GetItemAsync<Dictionary<string, int>>($"{DBKEY_PREFIX}local");
|
|
if ((unsentStats?.Count ?? 0) > 0)
|
|
{
|
|
try
|
|
{
|
|
var newData = await httpClient.PostJsonAsync<Dictionary<string, int>>($"ViewStats", unsentStats);
|
|
await localStorage.RemoveItemAsync($"{DBKEY_PREFIX}local");
|
|
await localStorage.SetItemAsync<Dictionary<string, int>>($"{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<Dictionary<string, int>>($"ViewStats");
|
|
await localStorage.SetItemAsync<Dictionary<string, int>>($"{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(() => {
|
|
//
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|