From be81fc7d57f75614639741120ebe4f0c49aff028 Mon Sep 17 00:00:00 2001 From: estrogen elf <87099578+BlossomiShymae@users.noreply.github.com> Date: Thu, 19 Jun 2025 07:35:19 -0500 Subject: [PATCH] refactor: constants --- Needlework.Net/App.axaml.cs | 4 ++-- Needlework.Net/Constants/AppInfo.cs | 2 ++ Needlework.Net/Constants/FlurlClientKeys.cs | 11 +++++++++++ Needlework.Net/Program.cs | 7 ++++--- Needlework.Net/Services/DocumentService.cs | 3 ++- Needlework.Net/Services/GithubService.cs | 6 +++--- .../ViewModels/MainWindow/MainWindowViewModel.cs | 15 +++++++-------- .../Pages/Websocket/WebsocketViewModel.cs | 5 +++-- .../Views/MainWindow/MainWindowView.axaml | 2 +- 9 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 Needlework.Net/Constants/FlurlClientKeys.cs diff --git a/Needlework.Net/App.axaml.cs b/Needlework.Net/App.axaml.cs index 7a5bcdf..d94e763 100644 --- a/Needlework.Net/App.axaml.cs +++ b/Needlework.Net/App.axaml.cs @@ -5,13 +5,13 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Templates; using Avalonia.Markup.Xaml; using Microsoft.Extensions.DependencyInjection; +using Needlework.Net.Constants; using Needlework.Net.Extensions; using Needlework.Net.ViewModels.MainWindow; using Needlework.Net.ViewModels.Pages; using Needlework.Net.Views.MainWindow; using System; using System.Reactive.Linq; -using System.Reflection; using System.Text.Json; using System.Threading.Tasks; @@ -26,7 +26,7 @@ public partial class App : Application, IEnableLogger _serviceProvider = serviceProvider; this.Log() - .Debug("NeedleworkDotNet version: {Version}", Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "0.0.0.0"); + .Debug("NeedleworkDotNet version: {Version}", AppInfo.Version); this.Log() .Debug("OS description: {Description}", System.Runtime.InteropServices.RuntimeInformation.OSDescription); } diff --git a/Needlework.Net/Constants/AppInfo.cs b/Needlework.Net/Constants/AppInfo.cs index f785746..07e2043 100644 --- a/Needlework.Net/Constants/AppInfo.cs +++ b/Needlework.Net/Constants/AppInfo.cs @@ -4,6 +4,8 @@ namespace Needlework.Net.Constants { 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"; } } diff --git a/Needlework.Net/Constants/FlurlClientKeys.cs b/Needlework.Net/Constants/FlurlClientKeys.cs new file mode 100644 index 0000000..2178fb6 --- /dev/null +++ b/Needlework.Net/Constants/FlurlClientKeys.cs @@ -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); + } +} diff --git a/Needlework.Net/Program.cs b/Needlework.Net/Program.cs index e1224e9..970acf9 100644 --- a/Needlework.Net/Program.cs +++ b/Needlework.Net/Program.cs @@ -4,6 +4,7 @@ using Avalonia; using Avalonia.Controls.Templates; using Flurl.Http.Configuration; using Microsoft.Extensions.DependencyInjection; +using Needlework.Net.Constants; using Needlework.Net.Extensions; using Needlework.Net.Services; using Needlework.Net.ViewModels.MainWindow; @@ -114,9 +115,9 @@ class Program return new SqlRawPersistentBlobCache("Data/data.sqlite"); }); builder.AddSingleton(new FlurlClientCache() - .Add("GithubClient", "https://api.github.com") - .Add("GithubUserContentClient", "https://raw.githubusercontent.com") - .Add("Client")); + .Add(FlurlClientKeys.GithubClient, "https://api.github.com") + .Add(FlurlClientKeys.GithubUserContentClient, "https://raw.githubusercontent.com") + .Add(FlurlClientKeys.Client)); builder.AddLogging((builder) => builder.AddSerilog(EnableLoggerExtensions.Log(null))); } diff --git a/Needlework.Net/Services/DocumentService.cs b/Needlework.Net/Services/DocumentService.cs index 6e364b6..20a5f94 100644 --- a/Needlework.Net/Services/DocumentService.cs +++ b/Needlework.Net/Services/DocumentService.cs @@ -2,6 +2,7 @@ using Flurl.Http; using Flurl.Http.Configuration; using Microsoft.OpenApi.Readers; +using Needlework.Net.Constants; using Needlework.Net.Extensions; using Needlework.Net.Models; using System; @@ -18,7 +19,7 @@ namespace Needlework.Net public DocumentService(IFlurlClientCache clients) { - _githubUserContentClient = clients.Get("GithubUserContentClient"); + _githubUserContentClient = clients.Get(FlurlClientKeys.GithubUserContentClient); } public async Task GetLcuSchemaDocumentAsync(CancellationToken cancellationToken = default) diff --git a/Needlework.Net/Services/GithubService.cs b/Needlework.Net/Services/GithubService.cs index af91ee3..080e930 100644 --- a/Needlework.Net/Services/GithubService.cs +++ b/Needlework.Net/Services/GithubService.cs @@ -20,8 +20,8 @@ namespace Needlework.Net.Services public GithubService(IBlobCache blobCache, IFlurlClientCache clients) { - _githubClient = clients.Get("GithubClient"); - _githubUserContentClient = clients.Get("GithubUserContentClient"); + _githubClient = clients.Get(FlurlClientKeys.GithubClient); + _githubUserContentClient = clients.Get(FlurlClientKeys.GithubUserContentClient); _blobCache = blobCache; } @@ -33,7 +33,7 @@ namespace Needlework.Net.Services .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}") + .WithHeader("User-Agent", $"{AppInfo.Name}/{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 dd171f3..b0f495e 100644 --- a/Needlework.Net/ViewModels/MainWindow/MainWindowViewModel.cs +++ b/Needlework.Net/ViewModels/MainWindow/MainWindowViewModel.cs @@ -23,7 +23,6 @@ using System.Linq; using System.Net.Http.Json; using System.Reactive; using System.Reactive.Linq; -using System.Reflection; using System.Threading; 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."; this.Log() .Error(ex, message); - _notificationService.Notify("Needlework.Net", message, InfoBarSeverity.Error); + _notificationService.Notify(AppInfo.Name, message, InfoBarSeverity.Error); _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."; this.Log() .Error(ex, message); - _notificationService.Notify("Needlework.Net", message, InfoBarSeverity.Error); + _notificationService.Notify(AppInfo.Name, message, InfoBarSeverity.Error); _checkForSchemaVersionDisposable?.Dispose(); } }); @@ -154,9 +153,9 @@ public partial class MainWindowViewModel 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) { @@ -228,11 +227,11 @@ public partial class MainWindowViewModel private async Task CheckForUpdatesAsync() { var release = await _githubService.GetLatestReleaseAsync(); - if (release.IsLatest(Version)) + if (release.IsLatest(AppInfo.Version)) { this.Log() .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(); } } @@ -262,7 +261,7 @@ public partial class MainWindowViewModel { this.Log() .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(); } } diff --git a/Needlework.Net/ViewModels/Pages/Websocket/WebsocketViewModel.cs b/Needlework.Net/ViewModels/Pages/Websocket/WebsocketViewModel.cs index 2b07ae7..3bbf9e2 100644 --- a/Needlework.Net/ViewModels/Pages/Websocket/WebsocketViewModel.cs +++ b/Needlework.Net/ViewModels/Pages/Websocket/WebsocketViewModel.cs @@ -8,6 +8,7 @@ using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using Flurl.Http; using Flurl.Http.Configuration; +using Needlework.Net.Constants; using Needlework.Net.Extensions; using Needlework.Net.Messages; 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) { - _githubUserContentClient = clients.Get("GithubUserContentClient"); + _githubUserContentClient = clients.Get(FlurlClientKeys.GithubUserContentClient); _notificationService = notificationService; 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."; this.Log() .Error(ex, message); - _notificationService.Notify("Needlework.Net", message, FluentAvalonia.UI.Controls.InfoBarSeverity.Error); + _notificationService.Notify(AppInfo.Name, message, FluentAvalonia.UI.Controls.InfoBarSeverity.Error); } } diff --git a/Needlework.Net/Views/MainWindow/MainWindowView.axaml b/Needlework.Net/Views/MainWindow/MainWindowView.axaml index 86d8201..46533da 100644 --- a/Needlework.Net/Views/MainWindow/MainWindowView.axaml +++ b/Needlework.Net/Views/MainWindow/MainWindowView.axaml @@ -12,7 +12,7 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="Needlework.Net.Views.MainWindow.MainWindowView" x:DataType="vm:MainWindowViewModel" - Title="Needlework.Net" + Title="{Binding AppName}" Icon="/Assets/app.ico" Width="1280" Height="720">