refactor: constants

This commit is contained in:
estrogen elf
2025-06-19 07:35:19 -05:00
parent d526354fea
commit be81fc7d57
9 changed files with 35 additions and 20 deletions

View File

@@ -5,13 +5,13 @@ using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Needlework.Net.Constants;
using Needlework.Net.Extensions; using Needlework.Net.Extensions;
using Needlework.Net.ViewModels.MainWindow; using Needlework.Net.ViewModels.MainWindow;
using Needlework.Net.ViewModels.Pages; using Needlework.Net.ViewModels.Pages;
using Needlework.Net.Views.MainWindow; using Needlework.Net.Views.MainWindow;
using System; using System;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -26,7 +26,7 @@ public partial class App : Application, IEnableLogger
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
this.Log() this.Log()
.Debug("NeedleworkDotNet version: {Version}", Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "0.0.0.0"); .Debug("NeedleworkDotNet version: {Version}", AppInfo.Version);
this.Log() this.Log()
.Debug("OS description: {Description}", System.Runtime.InteropServices.RuntimeInformation.OSDescription); .Debug("OS description: {Description}", System.Runtime.InteropServices.RuntimeInformation.OSDescription);
} }

View File

@@ -4,6 +4,8 @@ namespace Needlework.Net.Constants
{ {
public static class AppInfo public static class AppInfo
{ {
public static readonly string Name = "Needlework.Net";
public static readonly string Version = Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "0.0.0.0"; public static readonly string Version = Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "0.0.0.0";
} }
} }

View File

@@ -0,0 +1,11 @@
namespace Needlework.Net.Constants
{
public static class FlurlClientKeys
{
public static readonly string GithubClient = nameof(GithubClient);
public static readonly string GithubUserContentClient = nameof(GithubUserContentClient);
public static readonly string Client = nameof(Client);
}
}

View File

@@ -4,6 +4,7 @@ using Avalonia;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Flurl.Http.Configuration; using Flurl.Http.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Needlework.Net.Constants;
using Needlework.Net.Extensions; using Needlework.Net.Extensions;
using Needlework.Net.Services; using Needlework.Net.Services;
using Needlework.Net.ViewModels.MainWindow; using Needlework.Net.ViewModels.MainWindow;
@@ -114,9 +115,9 @@ class Program
return new SqlRawPersistentBlobCache("Data/data.sqlite"); return new SqlRawPersistentBlobCache("Data/data.sqlite");
}); });
builder.AddSingleton<IFlurlClientCache>(new FlurlClientCache() builder.AddSingleton<IFlurlClientCache>(new FlurlClientCache()
.Add("GithubClient", "https://api.github.com") .Add(FlurlClientKeys.GithubClient, "https://api.github.com")
.Add("GithubUserContentClient", "https://raw.githubusercontent.com") .Add(FlurlClientKeys.GithubUserContentClient, "https://raw.githubusercontent.com")
.Add("Client")); .Add(FlurlClientKeys.Client));
builder.AddLogging((builder) => builder.AddSerilog(EnableLoggerExtensions.Log(null))); builder.AddLogging((builder) => builder.AddSerilog(EnableLoggerExtensions.Log(null)));
} }

View File

@@ -2,6 +2,7 @@
using Flurl.Http; using Flurl.Http;
using Flurl.Http.Configuration; using Flurl.Http.Configuration;
using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Readers;
using Needlework.Net.Constants;
using Needlework.Net.Extensions; using Needlework.Net.Extensions;
using Needlework.Net.Models; using Needlework.Net.Models;
using System; using System;
@@ -18,7 +19,7 @@ namespace Needlework.Net
public DocumentService(IFlurlClientCache clients) public DocumentService(IFlurlClientCache clients)
{ {
_githubUserContentClient = clients.Get("GithubUserContentClient"); _githubUserContentClient = clients.Get(FlurlClientKeys.GithubUserContentClient);
} }
public async Task<Document> GetLcuSchemaDocumentAsync(CancellationToken cancellationToken = default) public async Task<Document> GetLcuSchemaDocumentAsync(CancellationToken cancellationToken = default)

View File

@@ -20,8 +20,8 @@ namespace Needlework.Net.Services
public GithubService(IBlobCache blobCache, IFlurlClientCache clients) public GithubService(IBlobCache blobCache, IFlurlClientCache clients)
{ {
_githubClient = clients.Get("GithubClient"); _githubClient = clients.Get(FlurlClientKeys.GithubClient);
_githubUserContentClient = clients.Get("GithubUserContentClient"); _githubUserContentClient = clients.Get(FlurlClientKeys.GithubUserContentClient);
_blobCache = blobCache; _blobCache = blobCache;
} }
@@ -33,7 +33,7 @@ namespace Needlework.Net.Services
.Debug("Downloading latest release info from GitHub..."); .Debug("Downloading latest release info from GitHub...");
var release = await _githubClient var release = await _githubClient
.Request("/repos/BlossomiShymae/Needlework.Net/releases/latest") .Request("/repos/BlossomiShymae/Needlework.Net/releases/latest")
.WithHeader("User-Agent", $"Needlework.Net/{AppInfo.Version}") .WithHeader("User-Agent", $"{AppInfo.Name}/{AppInfo.Version}")
.GetJsonAsync<GithubRelease>(); .GetJsonAsync<GithubRelease>();
return release; return release;
}, DateTimeOffset.Now + Intervals.CheckForUpdates); }, DateTimeOffset.Now + Intervals.CheckForUpdates);

View File

@@ -23,7 +23,6 @@ using System.Linq;
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Reactive; using System.Reactive;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -103,7 +102,7 @@ public partial class MainWindowViewModel
var message = "Failed to check for updates. Please check your internet connection or try again later."; var message = "Failed to check for updates. Please check your internet connection or try again later.";
this.Log() this.Log()
.Error(ex, message); .Error(ex, message);
_notificationService.Notify("Needlework.Net", message, InfoBarSeverity.Error); _notificationService.Notify(AppInfo.Name, message, InfoBarSeverity.Error);
_checkForUpdatesDisposable?.Dispose(); _checkForUpdatesDisposable?.Dispose();
} }
}); });
@@ -121,7 +120,7 @@ public partial class MainWindowViewModel
var message = "Failed to check for schema version. Please check your internet connection or try again later."; var message = "Failed to check for schema version. Please check your internet connection or try again later.";
this.Log() this.Log()
.Error(ex, message); .Error(ex, message);
_notificationService.Notify("Needlework.Net", message, InfoBarSeverity.Error); _notificationService.Notify(AppInfo.Name, message, InfoBarSeverity.Error);
_checkForSchemaVersionDisposable?.Dispose(); _checkForSchemaVersionDisposable?.Dispose();
} }
}); });
@@ -154,9 +153,9 @@ public partial class MainWindowViewModel
public bool IsSchemaVersionChecked { get; private set; } public bool IsSchemaVersionChecked { get; private set; }
public string Version { get; } = Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "0.0.0.0"; public string AppName => AppInfo.Name;
public string Title => $"Needlework.Net {Version}"; public string Title => $"{AppInfo.Name} {AppInfo.Version}";
partial void OnSelectedNavigationViewItemChanged(NavigationViewItem value) partial void OnSelectedNavigationViewItemChanged(NavigationViewItem value)
{ {
@@ -228,11 +227,11 @@ public partial class MainWindowViewModel
private async Task CheckForUpdatesAsync() private async Task CheckForUpdatesAsync()
{ {
var release = await _githubService.GetLatestReleaseAsync(); var release = await _githubService.GetLatestReleaseAsync();
if (release.IsLatest(Version)) if (release.IsLatest(AppInfo.Version))
{ {
this.Log() this.Log()
.Information("New version available: {TagName}", release.TagName); .Information("New version available: {TagName}", release.TagName);
_notificationService.Notify("Needlework.Net", $"New version available: {release.TagName}", InfoBarSeverity.Informational, null, "https://github.com/BlossomiShymae/Needlework.Net/releases/latest"); _notificationService.Notify(AppInfo.Name, $"New version available: {release.TagName}", InfoBarSeverity.Informational, null, "https://github.com/BlossomiShymae/Needlework.Net/releases/latest");
_checkForUpdatesDisposable?.Dispose(); _checkForUpdatesDisposable?.Dispose();
} }
} }
@@ -262,7 +261,7 @@ public partial class MainWindowViewModel
{ {
this.Log() this.Log()
.Warning("LCU Schema version mismatch: Current {CurrentVersion}, Latest {LatestVersion}", lcuSchemaDocument.Info.Version, systemBuild.Version); .Warning("LCU Schema version mismatch: Current {CurrentVersion}, Latest {LatestVersion}", lcuSchemaDocument.Info.Version, systemBuild.Version);
_notificationService.Notify("Needlework.Net", $"LCU Schema is possibly outdated compared to latest system build. Consider submitting a pull request on dysolix/hasagi-types.\nCurrent: {string.Join(".", currentSemVer)}\nLatest: {string.Join(".", latestSemVer)}", InfoBarSeverity.Warning, null, "https://github.com/dysolix/hasagi-types#updating-the-types"); _notificationService.Notify(AppInfo.Name, $"LCU Schema is possibly outdated compared to latest system build. Consider submitting a pull request on dysolix/hasagi-types.\nCurrent: {string.Join(".", currentSemVer)}\nLatest: {string.Join(".", latestSemVer)}", InfoBarSeverity.Warning, null, "https://github.com/dysolix/hasagi-types#updating-the-types");
_checkForSchemaVersionDisposable?.Dispose(); _checkForSchemaVersionDisposable?.Dispose();
} }
} }

View File

@@ -8,6 +8,7 @@ using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging; using CommunityToolkit.Mvvm.Messaging;
using Flurl.Http; using Flurl.Http;
using Flurl.Http.Configuration; using Flurl.Http.Configuration;
using Needlework.Net.Constants;
using Needlework.Net.Extensions; using Needlework.Net.Extensions;
using Needlework.Net.Messages; using Needlework.Net.Messages;
using Needlework.Net.Services; using Needlework.Net.Services;
@@ -36,7 +37,7 @@ public partial class WebSocketViewModel : PageBase, IEnableLogger
public WebSocketViewModel(IFlurlClientCache clients, NotificationService notificationService) : base("Event Viewer", "fa-solid fa-plug", -100) public WebSocketViewModel(IFlurlClientCache clients, NotificationService notificationService) : base("Event Viewer", "fa-solid fa-plug", -100)
{ {
_githubUserContentClient = clients.Get("GithubUserContentClient"); _githubUserContentClient = clients.Get(FlurlClientKeys.GithubUserContentClient);
_notificationService = notificationService; _notificationService = notificationService;
EventLog.CollectionChanged += (s, e) => OnPropertyChanged(nameof(FilteredEventLog)); EventLog.CollectionChanged += (s, e) => OnPropertyChanged(nameof(FilteredEventLog));
@@ -102,7 +103,7 @@ public partial class WebSocketViewModel : PageBase, IEnableLogger
var message = "Failed to get event types from GitHub. Please check your internet connection or try again later."; var message = "Failed to get event types from GitHub. Please check your internet connection or try again later.";
this.Log() this.Log()
.Error(ex, message); .Error(ex, message);
_notificationService.Notify("Needlework.Net", message, FluentAvalonia.UI.Controls.InfoBarSeverity.Error); _notificationService.Notify(AppInfo.Name, message, FluentAvalonia.UI.Controls.InfoBarSeverity.Error);
} }
} }

View File

@@ -12,7 +12,7 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Needlework.Net.Views.MainWindow.MainWindowView" x:Class="Needlework.Net.Views.MainWindow.MainWindowView"
x:DataType="vm:MainWindowViewModel" x:DataType="vm:MainWindowViewModel"
Title="Needlework.Net" Title="{Binding AppName}"
Icon="/Assets/app.ico" Icon="/Assets/app.ico"
Width="1280" Width="1280"
Height="720"> Height="720">