diff --git a/Needlework.Net/Constants/AppInfo.cs b/Needlework.Net/Constants/AppInfo.cs new file mode 100644 index 0000000..f785746 --- /dev/null +++ b/Needlework.Net/Constants/AppInfo.cs @@ -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"; + } +} diff --git a/Needlework.Net/Constants/BlobCacheKeys.cs b/Needlework.Net/Constants/BlobCacheKeys.cs new file mode 100644 index 0000000..c229115 --- /dev/null +++ b/Needlework.Net/Constants/BlobCacheKeys.cs @@ -0,0 +1,7 @@ +namespace Needlework.Net.Constants +{ + public static class BlobCacheKeys + { + public static readonly string GithubLatestRelease = nameof(GithubLatestRelease); + } +} diff --git a/Needlework.Net/Constants/Intervals.cs b/Needlework.Net/Constants/Intervals.cs new file mode 100644 index 0000000..b1d2d03 --- /dev/null +++ b/Needlework.Net/Constants/Intervals.cs @@ -0,0 +1,9 @@ +using System; + +namespace Needlework.Net.Constants +{ + public static class Intervals + { + public static readonly TimeSpan CheckForUpdates = TimeSpan.FromMinutes(60); + } +} diff --git a/Needlework.Net/Program.cs b/Needlework.Net/Program.cs index 1251061..e1224e9 100644 --- a/Needlework.Net/Program.cs +++ b/Needlework.Net/Program.cs @@ -107,6 +107,7 @@ class Program builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton((_) => { Directory.CreateDirectory("Data"); diff --git a/Needlework.Net/Services/GithubService.cs b/Needlework.Net/Services/GithubService.cs new file mode 100644 index 0000000..af91ee3 --- /dev/null +++ b/Needlework.Net/Services/GithubService.cs @@ -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 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(); + return release; + }, DateTimeOffset.Now + Intervals.CheckForUpdates); + } + } +} diff --git a/Needlework.Net/ViewModels/MainWindow/MainWindowViewModel.cs b/Needlework.Net/ViewModels/MainWindow/MainWindowViewModel.cs index 354d0d1..dd171f3 100644 --- a/Needlework.Net/ViewModels/MainWindow/MainWindowViewModel.cs +++ b/Needlework.Net/ViewModels/MainWindow/MainWindowViewModel.cs @@ -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 pages, DialogService dialogService, DocumentService documentService, NotificationService notificationService, IFlurlClientCache clients, SchemaPaneService schemaPaneService) + public MainWindowViewModel(IEnumerable 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(); - + var release = await _githubService.GetLatestReleaseAsync(); if (release.IsLatest(Version)) { this.Log()