UpdateDialog: Fix cancellation and buttons
All checks were successful
Test / Run tests (push) Successful in 2m24s

This commit is contained in:
2024-07-01 12:04:13 +02:00
parent 62f63ef63d
commit e9de54415a
2 changed files with 23 additions and 10 deletions

View File

@ -5,9 +5,10 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True"
WindowStartupLocation="CenterOwner" WindowStartupLocation="CenterOwner"
Title="Neues Update verfügbar - Elwig" Height="180" Width="400"> Title="Neues Update verfügbar - Elwig" Height="190" Width="400"
Closed="OnClosed">
<Grid> <Grid>
<TextBlock x:Name="Description" FontSize="14" Margin="0,0,0,30" <TextBlock x:Name="Description" FontSize="14" Margin="0,0,0,40"
HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"> HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center">
Version <Run x:Name="VersionText" FontWeight="Bold">0.0.0</Run> von Elwig ist verfügbar! Version <Run x:Name="VersionText" FontWeight="Bold">0.0.0</Run> von Elwig ist verfügbar!
(<Hyperlink NavigateUri="https://elwig.at/changelog" RequestNavigate="Hyperlink_RequestNavigate">Änderungen</Hyperlink>)<LineBreak/> (<Hyperlink NavigateUri="https://elwig.at/changelog" RequestNavigate="Hyperlink_RequestNavigate">Änderungen</Hyperlink>)<LineBreak/>
@ -20,12 +21,12 @@
HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"
Height="27" Width="300" SnapsToDevicePixels="True"/> Height="27" Width="300" SnapsToDevicePixels="True"/>
<Button x:Name="InstallButton" Content="Installieren" Margin="10,10,115,10" <Button x:Name="InstallButton" Content="Installieren" Margin="10,10,115,20"
FontSize="14" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Bottom"
Width="100" Height="27" Width="100" Height="27"
Click="InstallButton_Click"/> Click="InstallButton_Click"/>
<Button x:Name="CancelButton" Content="Abbrechen" Margin="10,10,10,10" IsCancel="True" IsDefault="True" <Button x:Name="CancelButton" Content="Abbrechen" Margin="115,10,10,20" IsCancel="True"
FontSize="14" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Bottom"
Width="100" Height="27"/> Width="100" Height="27"/>
</Grid> </Grid>
</Window> </Window>

View File

@ -3,6 +3,7 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Navigation; using System.Windows.Navigation;
@ -13,36 +14,47 @@ namespace Elwig.Dialogs {
public string Version { get; private set; } public string Version { get; private set; }
public string Url { get; private set; } public string Url { get; private set; }
private readonly CancellationTokenSource Cancellation;
public UpdateDialog(string version, string url, long size) { public UpdateDialog(string version, string url, long size) {
Version = version; Version = version;
Url = url; Url = url;
Cancellation = new();
InitializeComponent(); InitializeComponent();
VersionText.Text = version; VersionText.Text = version;
SizeText.Text = $"{size / 1024 / 1024}"; SizeText.Text = $"{size / 1024 / 1024}";
} }
private void OnClosed(object sender, EventArgs evt) {
Cancellation.Cancel();
}
private async void InstallButton_Click(object sender, RoutedEventArgs evt) { private async void InstallButton_Click(object sender, RoutedEventArgs evt) {
Description.Visibility = Visibility.Hidden; Description.Visibility = Visibility.Hidden;
ProgressBar.Visibility = Visibility.Visible; ProgressBar.Visibility = Visibility.Visible;
InstallButton.IsEnabled = false; InstallButton.IsEnabled = false;
await Install(); await Install();
DialogResult = true;
Close(); Close();
} }
public async Task Install() { public async Task Install() {
var fileName = Path.Combine(App.TempPath, $"Elwig-{Version}.exe"); var fileName = Path.Combine(App.TempPath, $"Elwig-{Version}.exe");
{ try {
using var stream = new FileStream(fileName, FileMode.Create); using var stream = new FileStream(fileName, FileMode.Create);
using var client = new HttpClient() { using var client = new HttpClient() {
Timeout = TimeSpan.FromSeconds(5), Timeout = TimeSpan.FromSeconds(5),
}; };
await client.DownloadAsync(Url, stream, new Progress<double>(p => { await client.DownloadAsync(Url, stream, new Progress<double>(p => {
ProgressBar.Value = p * 100.0; ProgressBar.Value = p * 100.0;
})); }), Cancellation.Token);
} catch (OperationCanceledException) {
File.Delete(fileName);
return;
} catch (Exception exc) {
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
} }
Process.Start(fileName); Process.Start(fileName);
DialogResult = true;
} }
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) { private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) {