Add upgrade UI (download progress bar)

This commit is contained in:
Tichau committed 2016-06-03 18:56:50 +02:00
1 parent ea522531cf
commit 10114acba7
8 files changed
+240 -34

No files matched your search

+8 -1
View File
@@ -3,11 +3,18 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xaml="clr-namespace:Markdown.Xaml;assembly=Markdown.Xaml"
xmlns:valueConverters="clr-namespace:FileConverter.ValueConverters"
xmlns:generic="clr-namespace:FileConverter.ValueConverters.Generic"
StartupUri="Windows/MainWindow.xaml" ShutdownMode="OnMainWindowClose">
<Application.Resources>
<!-- Converters -->
<valueConverters:ApplicationVersionToApplicationName x:Key="ApplicationVersionToApplicationName"/>
<generic:BoolToVisibility x:Key="BoolToVisibility"/>
<generic:ValueConverterGroup x:Key="InvBoolToVisibility">
<generic:BoolInverterConverter/>
<generic:BoolToVisibility/>
</generic:ValueConverterGroup>
<!-- Markdown style -->
<Style TargetType="FlowDocument" x:Key="DocumentStyle">
<Setter Property="FontFamily" Value="Segoe UI" />
+18 -9
View File
@@ -42,8 +42,7 @@ namespace FileConverter
private bool needToRunConversionThread;
private bool cancelAutoExit;
private bool isSessionEnding;
private UpgradeVersionDescription upgradeVersionDescription = null;
public Application()
{
this.ConvertionJobs = this.conversionJobs.AsReadOnly();
@@ -83,6 +82,12 @@ namespace FileConverter
set;
}
public UpgradeVersionDescription UpgradeVersionDescription
{
get;
private set;
}
public bool Verbose
{
get;
@@ -118,23 +123,23 @@ namespace FileConverter
Debug.Log("Exit application.");
if (!this.isSessionEnding && this.upgradeVersionDescription != null && this.upgradeVersionDescription.NeedToUpgrade)
if (!this.isSessionEnding && this.UpgradeVersionDescription != null && this.UpgradeVersionDescription.NeedToUpgrade)
{
Debug.Log("A new version of file converter has been found: {0}.", this.upgradeVersionDescription.LatestVersion);
Debug.Log("A new version of file converter has been found: {0}.", this.UpgradeVersionDescription.LatestVersion);
if (string.IsNullOrEmpty(this.upgradeVersionDescription.InstallerPath))
if (string.IsNullOrEmpty(this.UpgradeVersionDescription.InstallerPath))
{
Debug.LogError("Invalid installer path.");
}
else
{
Debug.Log("Wait for the end of the installer download.");
while (this.upgradeVersionDescription.InstallerDownloadInProgress)
while (this.UpgradeVersionDescription.InstallerDownloadInProgress)
{
Thread.Sleep(1000);
}
string installerPath = this.upgradeVersionDescription.InstallerPath;
string installerPath = this.UpgradeVersionDescription.InstallerPath;
if (!System.IO.File.Exists(installerPath))
{
Debug.LogError("Can't find upgrade installer ({0}). Try to restart the application.", installerPath);
@@ -142,7 +147,7 @@ namespace FileConverter
}
// Start process.
Debug.Log("Start file converter upgrade from version {0} to {1}.", ApplicationVersion, this.upgradeVersionDescription.LatestVersion);
Debug.Log("Start file converter upgrade from version {0} to {1}.", ApplicationVersion, this.UpgradeVersionDescription.LatestVersion);
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(installerPath)
{
@@ -289,6 +294,9 @@ namespace FileConverter
// Check upgrade.
if (this.Settings.CheckUpgradeAtStartup)
{
#if DEBUG
Task<UpgradeVersionDescription> task = Upgrade.Helpers.GetLatestVersionDescriptionAsync(this.OnGetLatestVersionDescription);
#else
long fileTime = Registry.GetValue<long>(Registry.Keys.LastUpdateCheckDate);
DateTime lastUpdateDateTime = DateTime.FromFileTime(fileTime);
@@ -297,6 +305,7 @@ namespace FileConverter
{
Task<UpgradeVersionDescription> task = Upgrade.Helpers.GetLatestVersionDescriptionAsync(this.OnGetLatestVersionDescription);
}
#endif
}
ConversionPreset conversionPreset = null;
@@ -483,7 +492,7 @@ namespace FileConverter
return;
}
this.upgradeVersionDescription = upgradeVersionDescription;
this.UpgradeVersionDescription = upgradeVersionDescription;
(this.MainWindow as MainWindow).OnNewVersionReleased(upgradeVersionDescription);
}
}
+35 -14
View File
@@ -6,7 +6,6 @@ namespace FileConverter.Upgrade
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
@@ -14,12 +13,13 @@ namespace FileConverter.Upgrade
public static class Helpers
{
#if DEBUG
private const string BaseURI = "https://raw.githubusercontent.com/Tichau/FileConverter/development/";
private const string BaseURI = "http://raw.githubusercontent.com/Tichau/FileConverter/development/";
#else
private const string BaseURI = "https://raw.githubusercontent.com/Tichau/FileConverter/master/";
private const string BaseURI = "http://raw.githubusercontent.com/Tichau/FileConverter/master/";
#endif
private static WebClient webClient = new WebClient();
private static UpgradeVersionDescription currentlyDownloadedVersionDescription;
public delegate void OnUpgradeOperationCompletedEventHandler(UpgradeVersionDescription upgradeVersionDescription);
@@ -87,21 +87,18 @@ namespace FileConverter.Upgrade
return upgradeVersionDescription.ChangeLog;
}
public static void DownloadInstallerAsync(UpgradeVersionDescription upgradeVersionDescription)
public static async Task DownloadInstallerAsync(UpgradeVersionDescription upgradeVersionDescription)
{
Thread thread = new Thread(Helpers.DownloadInstallerAsync);
thread.Start(upgradeVersionDescription);
}
private static void DownloadInstallerAsync(object parameter)
{
UpgradeVersionDescription upgradeVersionDescription = parameter as UpgradeVersionDescription;
if (upgradeVersionDescription == null)
{
throw new ArgumentNullException("upgradeVersionDescription");
}
if (Helpers.currentlyDownloadedVersionDescription != null)
{
throw new Exception("The installer download is currently in progress.");
}
Uri uri = new Uri(upgradeVersionDescription.InstallerURL);
string fileName = "FileConverter-setup.msi";
@@ -121,10 +118,34 @@ namespace FileConverter.Upgrade
upgradeVersionDescription.InstallerPath = installerPath;
upgradeVersionDescription.InstallerDownloadInProgress = true;
upgradeVersionDescription.InstallerDownloadProgress = 0;
Helpers.webClient.DownloadFile(uri, installerPath);
Helpers.currentlyDownloadedVersionDescription = upgradeVersionDescription;
upgradeVersionDescription.InstallerDownloadInProgress = false;
Helpers.webClient.DownloadProgressChanged += Helpers.WebClient_DownloadProgressChanged;
Helpers.webClient.DownloadFileCompleted += Helpers.WebClient_DownloadFileCompleted; ;
Task downloadTask = Helpers.webClient.DownloadFileTaskAsync(uri, installerPath);
}
private static void WebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Helpers.webClient.DownloadProgressChanged -= WebClient_DownloadProgressChanged;
Helpers.webClient.DownloadFileCompleted -= WebClient_DownloadFileCompleted;
if (Helpers.currentlyDownloadedVersionDescription != null)
{
Helpers.currentlyDownloadedVersionDescription.InstallerDownloadProgress = 100;
Helpers.currentlyDownloadedVersionDescription.InstallerDownloadInProgress = false;
Helpers.currentlyDownloadedVersionDescription = null;
}
}
private static void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs eventArgs)
{
if (Helpers.currentlyDownloadedVersionDescription != null)
{
Helpers.currentlyDownloadedVersionDescription.InstallerDownloadProgress = eventArgs.ProgressPercentage;
}
}
}
}
@@ -1,11 +1,19 @@
// <copyright file="UpgradeVersionDescription.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
using System.ComponentModel;
using System.Runtime.CompilerServices;
using FileConverter.Annotations;
namespace FileConverter
{
using System.Xml.Serialization;
public class UpgradeVersionDescription
public class UpgradeVersionDescription : INotifyPropertyChanged
{
private int installerDownloadProgress;
private bool installerDownloadInProgress;
public event PropertyChangedEventHandler PropertyChanged;
[XmlElement("Latest")]
public Version LatestVersion
{
@@ -37,8 +45,53 @@ namespace FileConverter
[XmlIgnore]
public bool InstallerDownloadInProgress
{
get;
set;
get
{
return this.installerDownloadInProgress;
}
set
{
this.installerDownloadInProgress = value;
this.OnPropertyChanged();
this.OnPropertyChanged(nameof(this.InstallerDownloadDone));
this.OnPropertyChanged(nameof(this.InstallerDownloadNotStarted));
}
}
[XmlIgnore]
public int InstallerDownloadProgress
{
get
{
return this.installerDownloadProgress;
}
set
{
this.installerDownloadProgress = value;
this.OnPropertyChanged();
this.OnPropertyChanged(nameof(this.InstallerDownloadDone));
this.OnPropertyChanged(nameof(this.InstallerDownloadNotStarted));
}
}
[XmlIgnore]
public bool InstallerDownloadDone
{
get
{
return !this.InstallerDownloadInProgress && this.InstallerDownloadProgress == 100;
}
}
[XmlIgnore]
public bool InstallerDownloadNotStarted
{
get
{
return !this.InstallerDownloadInProgress && this.InstallerDownloadProgress == 0;
}
}
[XmlIgnore]
@@ -47,5 +100,11 @@ namespace FileConverter
get;
set;
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
@@ -76,6 +76,20 @@ namespace FileConverter
}
}
protected override void OnClosing(CancelEventArgs eventArgs)
{
base.OnClosing(eventArgs);
Application application = Application.Current as Application;
if (application.UpgradeVersionDescription != null && application.UpgradeVersionDescription.NeedToUpgrade && !application.UpgradeVersionDescription.InstallerDownloadDone)
{
eventArgs.Cancel = true;
this.ShowUpgradeWindow();
this.upgradeWindow.VersionDescription = application.UpgradeVersionDescription;
}
}
private void SettingsButton_OnClick(object sender, RoutedEventArgs e)
{
this.ShowSettingsWindow();
@@ -130,16 +144,32 @@ namespace FileConverter
{
return;
}
else
else if (this.upgradeWindow == null)
{
this.upgradeWindow = new UpgradeWindow();
this.upgradeWindow.Closed += UpgradeWindow_Closed;
}
Application application = Application.Current as Application;
application?.CancelAutoExit();
this.upgradeWindow.Show();
}
private void UpgradeWindow_Closed(object sender, System.EventArgs e)
{
Application application = Application.Current as Application;
if (this.IsVisible)
{
return;
}
if (application.UpgradeVersionDescription != null && application.UpgradeVersionDescription.NeedToUpgrade && application.UpgradeVersionDescription.InstallerDownloadDone)
{
this.Close();
}
}
private void Application_OnApplicationTerminate(object sender, ApplicationTerminateArgs eventArgs)
{
if (float.IsNaN(eventArgs.RemainingTimeBeforeTermination))
@@ -86,7 +86,6 @@
<generic:DoubleToTimeString x:Key="DoubleToTimeString"/>
<valueConverters:FileNameConverter x:Key="FileNameConverter"/>
<generic:BoolToVisibility x:Key="BoolToVisibility"/>
<generic:CollectionContainsElementToBool x:Key="CollectionContainsElementToBool"/>
<generic:FormatStringConverter x:Key="FormatStringConverter"/>
@@ -33,7 +33,10 @@
<FlowDocumentScrollViewer Grid.Row="2" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Document="{Binding ReleaseNote, Converter={StaticResource TextToFlowDocumentConverter}, ElementName=window}"/>
<Button Background="WhiteSmoke" Grid.Row="3" Padding="10,5,10,5" Margin="5" HorizontalContentAlignment="Left" Click="OnInstallButtonClick" >
<!-- Download button -->
<Button Background="WhiteSmoke" Grid.Row="3" Padding="10,5,10,5" Margin="5" HorizontalContentAlignment="Left" Command="{Binding DownloadInstallerCommand, ElementName=window}"
Visibility="{Binding VersionDescription.InstallerDownloadNotStarted, Converter={StaticResource BoolToVisibility}, ConverterParameter=Collapsed, ElementName=window}">
<Button.Content>
<Grid>
<Grid.ColumnDefinitions>
@@ -52,5 +55,46 @@
</Grid>
</Button.Content>
</Button>
<!-- Download in progress -->
<Grid Grid.Row="3" Margin="5"
Visibility="{Binding VersionDescription.InstallerDownloadInProgress, Converter={StaticResource BoolToVisibility}, ConverterParameter=Collapsed, ElementName=window}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="/FileConverter;component/Resources/DownloadIcon.png" Stretch="Fill"/>
<Grid Grid.Column="1" Margin="10,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="Upgrade download in progress..." FontSize="14.667"/>
<ProgressBar Grid.Row="1" Margin="5" Height="30" Foreground="#004B82" Minimum="0" Maximum="100"
Value="{Binding VersionDescription.InstallerDownloadProgress, ElementName=window}"/>
</Grid>
</Grid>
<!-- Install button -->
<Button Background="WhiteSmoke" Grid.Row="3" Padding="10,5,10,5" Margin="5" HorizontalContentAlignment="Left" Command="{Binding LaunchInstallerCommand, ElementName=window}"
Visibility="{Binding VersionDescription.InstallerDownloadDone, Converter={StaticResource BoolToVisibility}, ConverterParameter=Collapsed, ElementName=window}">
<Button.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="/FileConverter;component/Resources/ApplicationIcon.png" Stretch="Fill"/>
<Grid Grid.Column="1" Margin="10,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="Install File Converter Now" FontSize="14.667"/>
<TextBlock Grid.Row="1" Text="The update has been downloaded. Exit File Converter and install the update now." TextWrapping="Wrap" FontSize="10.667"/>
</Grid>
</Grid>
</Button.Content>
</Button>
</Grid>
</Window>
@@ -2,12 +2,15 @@
namespace FileConverter.Windows
{
using System;
using System.Windows.Input;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using FileConverter.Annotations;
using FileConverter.Commands;
using FileConverter.Upgrade;
public partial class UpgradeWindow : Window, INotifyPropertyChanged
@@ -15,6 +18,9 @@ namespace FileConverter.Windows
private UpgradeVersionDescription upgradeVersionDescription;
private string releaseNoteContent;
private DelegateCommand downloadInstallerCommand;
private DelegateCommand launchInstallerCommand;
public UpgradeWindow()
{
this.InitializeComponent();
@@ -22,6 +28,32 @@ namespace FileConverter.Windows
public event PropertyChangedEventHandler PropertyChanged;
public ICommand DownloadInstallerCommand
{
get
{
if (this.downloadInstallerCommand == null)
{
this.downloadInstallerCommand = new DelegateCommand(this.ExecuteDownloadInstallerCommand);
}
return this.downloadInstallerCommand;
}
}
public ICommand LaunchInstallerCommand
{
get
{
if (this.launchInstallerCommand == null)
{
this.launchInstallerCommand = new DelegateCommand(this.ExecuteLaunchInstallerCommand);
}
return this.launchInstallerCommand;
}
}
public UpgradeVersionDescription VersionDescription
{
get
@@ -38,7 +70,7 @@ namespace FileConverter.Windows
this.OnPropertyChanged();
}
}
public string ReleaseNote
{
get
@@ -58,7 +90,7 @@ namespace FileConverter.Windows
this.OnPropertyChanged();
}
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
@@ -70,10 +102,15 @@ namespace FileConverter.Windows
this.ReleaseNote = versionDescription.ChangeLog;
}
private void OnInstallButtonClick(object sender, RoutedEventArgs e)
private void ExecuteDownloadInstallerCommand()
{
this.upgradeVersionDescription.NeedToUpgrade = true;
Helpers.DownloadInstallerAsync(this.upgradeVersionDescription);
this.Hide();
}
private void ExecuteLaunchInstallerCommand()
{
this.Close();
}
}