Ionite
1 year ago
9 changed files with 260 additions and 3 deletions
@ -0,0 +1,11 @@
|
||||
using System.Net.Http; |
||||
|
||||
namespace StabilityMatrix.Avalonia.DesignData; |
||||
|
||||
public class MockHttpClientFactory : IHttpClientFactory |
||||
{ |
||||
public HttpClient CreateClient(string name) |
||||
{ |
||||
throw new System.NotImplementedException(); |
||||
} |
||||
} |
@ -0,0 +1,91 @@
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Net.Http; |
||||
using System.Threading.Tasks; |
||||
using AsyncAwaitBestPractices; |
||||
using CommunityToolkit.Mvvm.ComponentModel; |
||||
using CommunityToolkit.Mvvm.Input; |
||||
using StabilityMatrix.Avalonia.Views.Dialogs; |
||||
using StabilityMatrix.Core.Attributes; |
||||
using StabilityMatrix.Core.Helper; |
||||
using StabilityMatrix.Core.Models.Progress; |
||||
using StabilityMatrix.Core.Models.Update; |
||||
using StabilityMatrix.Core.Services; |
||||
using StabilityMatrix.Core.Updater; |
||||
|
||||
namespace StabilityMatrix.Avalonia.ViewModels.Dialogs; |
||||
|
||||
[View(typeof(UpdateDialog))] |
||||
public partial class UpdateViewModel : ContentDialogViewModelBase |
||||
{ |
||||
private readonly ISettingsManager settingsManager; |
||||
private readonly IHttpClientFactory httpClientFactory; |
||||
private readonly IUpdateHelper updateHelper; |
||||
|
||||
[ObservableProperty] private bool isUpdateAvailable; |
||||
[ObservableProperty] private UpdateInfo? updateInfo; |
||||
|
||||
[ObservableProperty] private string? releaseNotes; |
||||
[ObservableProperty] private string? updateText; |
||||
[ObservableProperty] private int progressValue; |
||||
[ObservableProperty] private bool showProgressBar; |
||||
|
||||
public UpdateViewModel( |
||||
ISettingsManager settingsManager, |
||||
IHttpClientFactory httpClientFactory, |
||||
IUpdateHelper updateHelper) |
||||
{ |
||||
this.settingsManager = settingsManager; |
||||
this.httpClientFactory = httpClientFactory; |
||||
this.updateHelper = updateHelper; |
||||
|
||||
EventManager.Instance.UpdateAvailable += (_, info) => |
||||
{ |
||||
IsUpdateAvailable = true; |
||||
UpdateInfo = info; |
||||
}; |
||||
updateHelper.StartCheckingForUpdates().SafeFireAndForget(); |
||||
} |
||||
|
||||
public override async Task OnLoadedAsync() |
||||
{ |
||||
UpdateText = $"Stability Matrix v{UpdateInfo?.Version} is now available! You currently have v{Utilities.GetAppVersion()}. Would you like to update now?"; |
||||
|
||||
var client = httpClientFactory.CreateClient(); |
||||
var response = await client.GetAsync(UpdateInfo?.ChangelogUrl); |
||||
if (response.IsSuccessStatusCode) |
||||
{ |
||||
ReleaseNotes = await response.Content.ReadAsStringAsync(); |
||||
} |
||||
else |
||||
{ |
||||
ReleaseNotes = "## Unable to load release notes"; |
||||
} |
||||
} |
||||
|
||||
[RelayCommand] |
||||
private async Task InstallUpdate() |
||||
{ |
||||
if (UpdateInfo == null) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
ShowProgressBar = true; |
||||
UpdateText = $"Downloading update v{UpdateInfo.Version}..."; |
||||
await updateHelper.DownloadUpdate(UpdateInfo, new Progress<ProgressReport>(report => |
||||
{ |
||||
ProgressValue = Convert.ToInt32(report.Percentage); |
||||
})); |
||||
|
||||
UpdateText = "Update complete. Restarting Stability Matrix in 3 seconds..."; |
||||
await Task.Delay(1000); |
||||
UpdateText = "Update complete. Restarting Stability Matrix in 2 seconds..."; |
||||
await Task.Delay(1000); |
||||
UpdateText = "Update complete. Restarting Stability Matrix in 1 second..."; |
||||
await Task.Delay(1000); |
||||
|
||||
Process.Start(UpdateHelper.ExecutablePath); |
||||
App.Shutdown(); |
||||
} |
||||
} |
@ -0,0 +1,85 @@
|
||||
<controls:UserControlBase xmlns="https://github.com/avaloniaui" |
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
||||
xmlns:vm="clr-namespace:StabilityMatrix.Avalonia.ViewModels" |
||||
xmlns:dialogs="clr-namespace:StabilityMatrix.Avalonia.ViewModels.Dialogs" |
||||
xmlns:ui="using:FluentAvalonia.UI.Controls" |
||||
xmlns:mocks="clr-namespace:StabilityMatrix.Avalonia.DesignData" |
||||
xmlns:controls="clr-namespace:StabilityMatrix.Avalonia.Controls" |
||||
xmlns:system="clr-namespace:System;assembly=System.Runtime" |
||||
xmlns:mdxaml="https://github.com/whistyun/Markdown.Avalonia.Tight" |
||||
d:DataContext="{x:Static mocks:DesignData.UpdateViewModel}" |
||||
x:DataType="dialogs:UpdateViewModel" |
||||
mc:Ignorable="d" d:DesignWidth="700" d:DesignHeight="550" |
||||
x:Class="StabilityMatrix.Avalonia.Views.Dialogs.UpdateDialog"> |
||||
<Grid RowDefinitions="Auto,Auto,Auto,Auto,*,Auto,Auto"> |
||||
|
||||
<!--<TitleBar Background="{ui:ThemeResource ApplicationBackgroundBrush}"> |
||||
<TitleBar.Header> |
||||
<TextBlock Margin="16,8" Text="Stability Matrix - Update Available" /> |
||||
</TitleBar.Header> |
||||
</TitleBar>--> |
||||
|
||||
<TextBlock Grid.Row="0" |
||||
Text="A new version of Stability Matrix is available!" |
||||
HorizontalAlignment="Center" |
||||
FontWeight="Thin" |
||||
Margin="0,16,0,0" |
||||
FontSize="28"/> |
||||
|
||||
<TextBlock Grid.Row="1" |
||||
HorizontalAlignment="Center" |
||||
FontSize="18" |
||||
TextWrapping="Wrap" |
||||
TextAlignment="Center" |
||||
Text="{Binding UpdateText}" |
||||
Margin="16,32,16,0"/> |
||||
|
||||
<TextBlock Grid.Row="2" |
||||
Text="Release Notes" |
||||
FontSize="16" |
||||
IsVisible="{Binding !ShowProgressBar}" |
||||
Margin="32,16,32,0"/> |
||||
|
||||
<ProgressBar Grid.Row="3" |
||||
Height="200" |
||||
Value="{Binding ProgressValue}" |
||||
IsVisible="{Binding ShowProgressBar}" |
||||
Margin="32"/> |
||||
|
||||
<Grid Grid.Row="4" |
||||
Margin="8" |
||||
IsVisible="{Binding !ShowProgressBar}"> |
||||
<Border Margin="32, 16" |
||||
CornerRadius="8" |
||||
Background="{DynamicResource ButtonBackgroundPressed}"> |
||||
<mdxaml:MarkdownScrollViewer |
||||
Margin="16" |
||||
Markdown="{Binding ReleaseNotes, Mode=OneWay}"/> |
||||
</Border> |
||||
</Grid> |
||||
|
||||
<ui:InfoBar Grid.Row="5" |
||||
Margin="64,0,64,16" |
||||
IsOpen="True" |
||||
IsClosable="False" |
||||
Title="The app will relaunch after updating" /> |
||||
|
||||
<StackPanel Grid.Row="6" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,16"> |
||||
<Button Content="Remind Me Later" |
||||
Classes="info" |
||||
Margin="0,0,8,0" |
||||
FontSize="18" |
||||
Command="{Binding OnCloseButtonClick}" |
||||
IsEnabled="{Binding !InstallUpdateCommand.IsRunning}" /> |
||||
|
||||
<Button Content="Install Now" |
||||
Classes="success" |
||||
Margin="8,0,0,0" |
||||
FontSize="18" |
||||
Command="{Binding InstallUpdateCommand}" /> |
||||
</StackPanel> |
||||
|
||||
</Grid> |
||||
</controls:UserControlBase> |
@ -0,0 +1,18 @@
|
||||
using Avalonia; |
||||
using Avalonia.Controls; |
||||
using Avalonia.Markup.Xaml; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Views.Dialogs; |
||||
|
||||
public partial class UpdateDialog : UserControl |
||||
{ |
||||
public UpdateDialog() |
||||
{ |
||||
InitializeComponent(); |
||||
} |
||||
|
||||
private void InitializeComponent() |
||||
{ |
||||
AvaloniaXamlLoader.Load(this); |
||||
} |
||||
} |
Loading…
Reference in new issue