Skip to content

Commit

Permalink
Refactor update check
Browse files Browse the repository at this point in the history
  • Loading branch information
ckaczor committed Feb 23, 2018
1 parent 8f55046 commit d9d0f33
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 48 deletions.
32 changes: 27 additions & 5 deletions Options/AboutOptionsPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,43 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:windows="clr-namespace:Common.Wpf.Windows;assembly=Common.Wpf"
xmlns:properties="clr-namespace:ProcessCpuUsageStatusWindow.Properties"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="[Application Name]"
Name="ApplicationNameLabel"
VerticalAlignment="Top"
FontWeight="Bold" />
FontWeight="Bold"
Grid.Row="0" />
<TextBlock Text="[Application Version]"
Margin="0,22,0,0"
Margin="0,6,0,0"
Name="VersionLabel"
VerticalAlignment="Top" />
VerticalAlignment="Top"
Grid.Row="1" />
<TextBlock Text="[Company]"
Margin="0,44,0,0"
Margin="0,6,0,0"
Name="CompanyLabel"
VerticalAlignment="Top" />
VerticalAlignment="Top"
Grid.Row="2" />
<StackPanel Grid.Row="3"
Grid.Column="0"
Margin="0,20,0,0"
Orientation="Horizontal">
<Button Content="{x:Static properties:Resources.CheckUpdate}"
HorizontalAlignment="Left"
Padding="6,2"
Click="HandleCheckForUpdateButtonClick"
VerticalContentAlignment="Center" />
<Label Name="UpdateMessage" Content="" VerticalContentAlignment="Center" Padding="6,0" />
</StackPanel>
</Grid>
</windows:CategoryPanel>
9 changes: 7 additions & 2 deletions Options/AboutOptionsPanel.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Common.Update;
using System.Reflection;
using System.Reflection;
using System.Windows;

namespace ProcessCpuUsageStatusWindow.Options
{
Expand Down Expand Up @@ -32,5 +32,10 @@ public override void SavePanel()
}

public override string CategoryName => Properties.Resources.OptionCategory_About;

private async void HandleCheckForUpdateButtonClick(object sender, RoutedEventArgs e)
{
await UpdateCheck.CheckUpdate((status, message) => UpdateMessage.Content = message);
}
}
}
10 changes: 6 additions & 4 deletions Options/GeneralOptionsPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
<Label Grid.Column="0"
Grid.Row="1"
Content="{x:Static properties:Resources.NumberOfProcesses}"
Margin="0,6,6,6"
Padding="0"
VerticalContentAlignment="Center" VerticalAlignment="Center" />
Margin="0,4,6,0"
Padding="0"
VerticalContentAlignment="Center" VerticalAlignment="Center" Target="{x:Reference NumberOfProcesses}" />
<xctk:IntegerUpDown Grid.Column="1"
Grid.Row="1" x:Name="NumberOfProcesses"
Minimum="1" Maximum="20"
TextAlignment="Left" Margin="6"
TextAlignment="Left" Margin="0,12,6,6"
Width="50"
VerticalContentAlignment="Center"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
</Grid>
</windows:CategoryPanel>
1 change: 1 addition & 0 deletions ProcessCpuUsageStatusWindow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="Options\GeneralOptionsPanel.xaml.cs">
<DependentUpon>GeneralOptionsPanel.xaml</DependentUpon>
</Compile>
<Compile Include="UpdateCheck.cs" />
<Compile Include="WindowSource.cs" />
<Page Include="Options\AboutOptionsPanel.xaml">
<Generator>MSBuild:Compile</Generator>
Expand Down
54 changes: 54 additions & 0 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,30 @@
<data name="ApplicationName" xml:space="preserve">
<value>Process CPU Usage</value>
</data>
<data name="CheckingForUpdate" xml:space="preserve">
<value>Checking for update...</value>
</data>
<data name="CheckUpdate" xml:space="preserve">
<value>_Check for Update</value>
</data>
<data name="DownloadingUpdate" xml:space="preserve">
<value>Downloading update...</value>
</data>
<data name="FooterLine" xml:space="preserve">
<value>CPU: {0,4:f1}% - Total</value>
</data>
<data name="HeaderLine" xml:space="preserve">
<value />
</data>
<data name="InstallingUpdate" xml:space="preserve">
<value>Installing update...</value>
</data>
<data name="Loading" xml:space="preserve">
<value>Loading...</value>
</data>
<data name="NoUpdate" xml:space="preserve">
<value>No update found</value>
</data>
<data name="NumberOfProcesses" xml:space="preserve">
<value>_Number of processes:</value>
</data>
Expand Down Expand Up @@ -166,4 +181,7 @@
<data name="Updating" xml:space="preserve">
<value>Updating application...</value>
</data>
<data name="RestartingAfterUpdate" xml:space="preserve">
<value>Restarting application...</value>
</data>
</root>
71 changes: 71 additions & 0 deletions UpdateCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Squirrel;
using System;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;

namespace ProcessCpuUsageStatusWindow
{
public static class UpdateCheck
{
public enum UpdateStatus
{
Checking,
None,
Downloading,
Installing,
Restarting
}

public delegate void UpdateStatusDelegate(UpdateStatus updateStatus, string message);

public static Version LocalVersion => Assembly.GetEntryAssembly().GetName().Version;

public static async Task<bool> CheckUpdate(UpdateStatusDelegate onUpdateStatus)
{
try
{
onUpdateStatus.Invoke(UpdateStatus.Checking, Properties.Resources.CheckingForUpdate);

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

using (var updateManager = await UpdateManager.GitHubUpdateManager(App.UpdateUrl))
{
var updates = await updateManager.CheckForUpdate();

var lastVersion = updates?.ReleasesToApply?.OrderBy(releaseEntry => releaseEntry.Version).LastOrDefault();

if (lastVersion == null)
{
onUpdateStatus.Invoke(UpdateStatus.None, Properties.Resources.NoUpdate);
return false;
}

onUpdateStatus.Invoke(UpdateStatus.Downloading, Properties.Resources.DownloadingUpdate);

Common.Settings.Extensions.BackupSettings();

await updateManager.DownloadReleases(new[] { lastVersion });

onUpdateStatus.Invoke(UpdateStatus.Installing, Properties.Resources.InstallingUpdate);

await updateManager.ApplyReleases(updates);
await updateManager.UpdateApp();
}

onUpdateStatus.Invoke(UpdateStatus.Restarting, Properties.Resources.RestartingAfterUpdate);

UpdateManager.RestartApp();

return true;
}
catch (Exception exception)
{
Console.WriteLine(exception);

return false;
}
}
}
}
44 changes: 7 additions & 37 deletions WindowSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,15 @@ private void StartUpdate(bool updateRequired)

private async Task<bool> UpdateApp()
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

using (var updateManager = await UpdateManager.GitHubUpdateManager(App.UpdateUrl))
{
var updates = await updateManager.CheckForUpdate();

var lastVersion = updates?.ReleasesToApply?.OrderBy(releaseEntry => releaseEntry.Version).LastOrDefault();

if (lastVersion == null)
return false;

_dispatcher.Invoke(() => _floatingStatusWindow.SetText(Resources.Updating));
Thread.Sleep(500);

Common.Settings.Extensions.BackupSettings();

#if !DEBUG
await updateManager.DownloadReleases(new[] { lastVersion });
await updateManager.ApplyReleases(updates);
await updateManager.UpdateApp();
#endif
}

#if !DEBUG
_dispatcher.Invoke(Dispose);

UpdateManager.RestartApp();
#endif
return await UpdateCheck.CheckUpdate(HandleUpdateStatus);
}

return true;
}
catch (Exception exception)
{
Console.WriteLine(exception);
private void HandleUpdateStatus(UpdateCheck.UpdateStatus status, string message)
{
if (status == UpdateCheck.UpdateStatus.None)
message = Resources.Loading;

return false;
}
_dispatcher.Invoke(() => _floatingStatusWindow.SetText(message));
}

public void Dispose()
Expand Down

0 comments on commit d9d0f33

Please sign in to comment.