feat: use file kv-store for updates

This commit is contained in:
estrogen elf
2025-06-19 07:12:52 -05:00
parent 4dc2d74ccf
commit d526354fea
6 changed files with 75 additions and 12 deletions

View File

@@ -0,0 +1,9 @@
using System.Reflection;
namespace Needlework.Net.Constants
{
public static class AppInfo
{
public static readonly string Version = Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "0.0.0.0";
}
}

View File

@@ -0,0 +1,7 @@
namespace Needlework.Net.Constants
{
public static class BlobCacheKeys
{
public static readonly string GithubLatestRelease = nameof(GithubLatestRelease);
}
}

View File

@@ -0,0 +1,9 @@
using System;
namespace Needlework.Net.Constants
{
public static class Intervals
{
public static readonly TimeSpan CheckForUpdates = TimeSpan.FromMinutes(60);
}
}

View File

@@ -107,6 +107,7 @@ class Program
builder.AddSingleton<NotificationService>();
builder.AddSingleton<SchemaPaneService>();
builder.AddSingleton<HextechDocsPostService>();
builder.AddSingleton<GithubService>();
builder.AddSingleton<IBlobCache>((_) =>
{
Directory.CreateDirectory("Data");

View File

@@ -0,0 +1,42 @@
using Akavache;
using Flurl.Http;
using Flurl.Http.Configuration;
using Needlework.Net.Constants;
using Needlework.Net.Extensions;
using Needlework.Net.Models;
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
namespace Needlework.Net.Services
{
public class GithubService : IEnableLogger
{
private readonly IFlurlClient _githubClient;
private readonly IFlurlClient _githubUserContentClient;
private readonly IBlobCache _blobCache;
public GithubService(IBlobCache blobCache, IFlurlClientCache clients)
{
_githubClient = clients.Get("GithubClient");
_githubUserContentClient = clients.Get("GithubUserContentClient");
_blobCache = blobCache;
}
public async Task<GithubRelease> GetLatestReleaseAsync()
{
return await _blobCache.GetOrFetchObject(BlobCacheKeys.GithubLatestRelease, async () =>
{
this.Log()
.Debug("Downloading latest release info from GitHub...");
var release = await _githubClient
.Request("/repos/BlossomiShymae/Needlework.Net/releases/latest")
.WithHeader("User-Agent", $"Needlework.Net/{AppInfo.Version}")
.GetJsonAsync<GithubRelease>();
return release;
}, DateTimeOffset.Now + Intervals.CheckForUpdates);
}
}
}

View File

@@ -7,8 +7,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using FluentAvalonia.UI.Controls;
using Flurl.Http;
using Flurl.Http.Configuration;
using Needlework.Net.Constants;
using Needlework.Net.Extensions;
using Needlework.Net.Helpers;
using Needlework.Net.Messages;
@@ -35,7 +34,7 @@ public partial class MainWindowViewModel
{
private readonly DocumentService _documentService;
private readonly IFlurlClient _githubClient;
private readonly GithubService _githubService;
private readonly NotificationService _notificationService;
@@ -47,13 +46,13 @@ public partial class MainWindowViewModel
private readonly IDisposable _checkForSchemaVersionDisposable;
public MainWindowViewModel(IEnumerable<PageBase> pages, DialogService dialogService, DocumentService documentService, NotificationService notificationService, IFlurlClientCache clients, SchemaPaneService schemaPaneService)
public MainWindowViewModel(IEnumerable<PageBase> pages, DialogService dialogService, DocumentService documentService, NotificationService notificationService, GithubService githubService, SchemaPaneService schemaPaneService)
{
_dialogService = dialogService;
_documentService = documentService;
_notificationService = notificationService;
_schemaPaneService = schemaPaneService;
_githubClient = clients.Get("GithubClient");
_githubService = githubService;
NavigationViewItems = pages
.OrderBy(p => p.Index)
@@ -91,7 +90,7 @@ public partial class MainWindowViewModel
}
});
_checkForUpdatesDisposable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(10))
_checkForUpdatesDisposable = Observable.Timer(TimeSpan.Zero, Intervals.CheckForUpdates)
.Select(time => Unit.Default)
.Subscribe(async _ =>
{
@@ -109,7 +108,7 @@ public partial class MainWindowViewModel
}
});
_checkForSchemaVersionDisposable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(10))
_checkForSchemaVersionDisposable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(5))
.Select(time => Unit.Default)
.Subscribe(async _ =>
{
@@ -228,11 +227,7 @@ public partial class MainWindowViewModel
private async Task CheckForUpdatesAsync()
{
var release = await _githubClient
.Request("/repos/BlossomiShymae/Needlework.Net/releases/latest")
.WithHeader("User-Agent", $"Needlework.Net/{Version}")
.GetJsonAsync<GithubRelease>();
var release = await _githubService.GetLatestReleaseAsync();
if (release.IsLatest(Version))
{
this.Log()