Browse Source

Preloading and better parsing for UpdateViewModel

pull/165/head
Ionite 1 year ago
parent
commit
60a497d1f1
No known key found for this signature in database
  1. 56
      StabilityMatrix.Avalonia/ViewModels/Dialogs/UpdateViewModel.cs

56
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()
/// <summary>
/// Formats changelog markdown including up to the current version
/// </summary>
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();
}
}
/// <inheritdoc />
public override void OnUnloaded()
{
base.OnUnloaded();
isLoaded = false;
}
[RelayCommand]
private async Task InstallUpdate()
{

Loading…
Cancel
Save