diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/UpdateViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/UpdateViewModel.cs index 3c7cfef0..b475c174 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/UpdateViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/UpdateViewModel.cs @@ -2,10 +2,12 @@ using System.Diagnostics; using System.IO; using System.Net.Http; +using System.Text.RegularExpressions; using System.Threading.Tasks; using AsyncAwaitBestPractices; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using Semver; using StabilityMatrix.Avalonia.ViewModels.Base; using StabilityMatrix.Avalonia.Views.Dialogs; using StabilityMatrix.Core.Attributes; @@ -24,6 +26,8 @@ public partial class UpdateViewModel : ContentDialogViewModelBase private readonly IHttpClientFactory httpClientFactory; private readonly IUpdateHelper updateHelper; + private bool isLoaded; + [ObservableProperty] private bool isUpdateAvailable; @@ -60,31 +64,36 @@ public partial class UpdateViewModel : ContentDialogViewModelBase updateHelper.StartCheckingForUpdates().SafeFireAndForget(); } - public override async Task OnLoadedAsync() + /// + /// Formats changelog markdown including up to the current version + /// + internal static string? FormatChangelog(string markdown, SemVersion currentVersion) + { + var pattern = $@"(##[\s\S]+?)(?:## v{currentVersion.WithoutPrereleaseOrMetadata()})"; + + var match = Regex.Match(markdown, pattern); + + return match.Success ? match.Groups[1].Value.TrimEnd() : null; + } + + public async Task Preload() { if (UpdateInfo is null) return; - UpdateText = - $"Stability Matrix v{UpdateInfo.Version} is now available! You currently have v{Compat.AppVersion}. Would you like to update now?"; - - var client = httpClientFactory.CreateClient(); + using var client = httpClientFactory.CreateClient(); var response = await client.GetAsync(UpdateInfo.ChangelogUrl); if (response.IsSuccessStatusCode) { - ReleaseNotes = await response.Content.ReadAsStringAsync(); + var changelog = await response.Content.ReadAsStringAsync(); // Formatting for new changelog format // https://keepachangelog.com/en/1.1.0/ if (UpdateInfo.ChangelogUrl.EndsWith(".md", StringComparison.OrdinalIgnoreCase)) { - // Skip until the first occurrence of "##" - const string delimiter = "##"; - var startIndex = ReleaseNotes.IndexOf(delimiter, StringComparison.Ordinal); - if (startIndex != -1) - { - ReleaseNotes = ReleaseNotes[startIndex..]; - } + ReleaseNotes = + FormatChangelog(changelog, UpdateInfo.Version) + ?? "## Unable to format release notes"; } } else @@ -93,6 +102,27 @@ public partial class UpdateViewModel : ContentDialogViewModelBase } } + public override async Task OnLoadedAsync() + { + if (UpdateInfo is null) + return; + + UpdateText = + $"Stability Matrix v{UpdateInfo.Version} is now available! You currently have v{Compat.AppVersion}. Would you like to update now?"; + + if (!isLoaded) + { + await Preload(); + } + } + + /// + public override void OnUnloaded() + { + base.OnUnloaded(); + isLoaded = false; + } + [RelayCommand] private async Task InstallUpdate() {