Add update settings panel

This commit is contained in:
2024-09-30 17:41:54 -04:00
parent 5631bc87be
commit 733a3de573
12 changed files with 224 additions and 15 deletions

View File

@@ -22,6 +22,9 @@
<setting name="TimeZones" serializeAs="String">
<value>[]</value>
</setting>
<setting name="CheckVersionAtStartup" serializeAs="String">
<value>True</value>
</setting>
</WorldClockStatusWindow.Properties.Settings>
</userSettings>
</configuration>

View File

@@ -1,16 +1,12 @@
using Microsoft.Extensions.Logging;
using NuGet.Versioning;
using Serilog;
using System;
using Velopack;
using Velopack.Sources;
namespace WorldClockStatusWindow;
internal class Program
{
private static UpdateManager _updateManager;
[STAThread]
public static void Main(string[] args)
{
@@ -28,8 +24,4 @@ internal class Program
Log.Logger.Information("End");
}
public static UpdateManager UpdateManager => _updateManager ??= new UpdateManager(new GithubSource("https://github.com/ckaczor/WorldClockStatusWindow", null, false));
public static string LocalVersion => (UpdateManager.CurrentVersion ?? new SemanticVersion(0, 0, 0)).ToString();
}

View File

@@ -88,6 +88,24 @@ namespace WorldClockStatusWindow.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Check _Now.
/// </summary>
public static string checkVersionNowButton {
get {
return ResourceManager.GetString("checkVersionNowButton", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to _Check for a new version on startup.
/// </summary>
public static string checkVersionOnStartupCheckBox {
get {
return ResourceManager.GetString("checkVersionOnStartupCheckBox", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Close.
/// </summary>
@@ -115,6 +133,15 @@ namespace WorldClockStatusWindow.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Update.
/// </summary>
public static string optionCategoryUpdate {
get {
return ResourceManager.GetString("optionCategoryUpdate", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Settings.
/// </summary>
@@ -133,6 +160,37 @@ namespace WorldClockStatusWindow.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to You are already running the most recent version.
///
///No updates are available at this time..
/// </summary>
public static string UpdateCheckCurrent {
get {
return ResourceManager.GetString("UpdateCheckCurrent", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Version {1} of {0} is now available.
///
///Would you like to download and install it now?.
/// </summary>
public static string UpdateCheckNewVersion {
get {
return ResourceManager.GetString("UpdateCheckNewVersion", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to {0} Update.
/// </summary>
public static string UpdateCheckTitle {
get {
return ResourceManager.GetString("UpdateCheckTitle", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Version {0}.
/// </summary>

View File

@@ -145,4 +145,26 @@
<data name="Version" xml:space="preserve">
<value>Version {0}</value>
</data>
<data name="optionCategoryUpdate" xml:space="preserve">
<value>Update</value>
</data>
<data name="checkVersionOnStartupCheckBox" xml:space="preserve">
<value>_Check for a new version on startup</value>
</data>
<data name="checkVersionNowButton" xml:space="preserve">
<value>Check _Now</value>
</data>
<data name="UpdateCheckTitle" xml:space="preserve">
<value>{0} Update</value>
</data>
<data name="UpdateCheckCurrent" xml:space="preserve">
<value>You are already running the most recent version.
No updates are available at this time.</value>
</data>
<data name="UpdateCheckNewVersion" xml:space="preserve">
<value>Version {1} of {0} is now available.
Would you like to download and install it now?</value>
</data>
</root>

View File

@@ -70,5 +70,17 @@ namespace WorldClockStatusWindow.Properties {
this["TimeZones"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool CheckVersionAtStartup {
get {
return ((bool)(this["CheckVersionAtStartup"]));
}
set {
this["CheckVersionAtStartup"] = value;
}
}
}
}

View File

@@ -14,5 +14,8 @@
<Setting Name="TimeZones" Type="System.String" Scope="User">
<Value Profile="(Default)">[]</Value>
</Setting>
<Setting Name="CheckVersionAtStartup" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@@ -13,7 +13,7 @@
<StackPanel windows:Spacing.Vertical="10">
<TextBlock Text="{x:Static properties:Resources.ApplicationName}"
FontWeight="Bold" />
<TextBlock Text="{Binding Source={x:Static worldClockStatusWindow:Program.LocalVersion}, StringFormat={x:Static properties:Resources.Version}}"
<TextBlock Text="{Binding Source={x:Static worldClockStatusWindow:UpdateCheck.LocalVersion}, StringFormat={x:Static properties:Resources.Version}}"
Name="VersionLabel" />
<TextBlock Text="Chris Kaczor" />
</StackPanel>

View File

@@ -0,0 +1,22 @@
<windows:CategoryPanelBase x:Class="WorldClockStatusWindow.SettingsWindow.UpdateSettingsPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:properties="clr-namespace:WorldClockStatusWindow.Properties"
xmlns:windows="clr-namespace:ChrisKaczor.Wpf.Windows;assembly=ChrisKaczor.Wpf.Windows.CategoryWindow"
xmlns:worldClockStatusWindow="clr-namespace:WorldClockStatusWindow"
mc:Ignorable="d"
d:DesignHeight="150"
d:DesignWidth="250">
<StackPanel windows:Spacing.Vertical="10">
<CheckBox Content="{x:Static properties:Resources.checkVersionOnStartupCheckBox}"
Name="CheckVersionOnStartupCheckBox"
IsChecked="{Binding Source={x:Static properties:Settings.Default}, Path=CheckVersionAtStartup}"
Click="OnSaveSettings" />
<Button Content="{x:Static properties:Resources.checkVersionNowButton}"
IsEnabled="{Binding Source={x:Static worldClockStatusWindow:UpdateCheck.IsInstalled}}"
HorizontalAlignment="Left"
Click="HandleCheckVersionNowButtonClick" />
</StackPanel>
</windows:CategoryPanelBase>

View File

@@ -0,0 +1,31 @@
using System.Windows;
using WorldClockStatusWindow.Properties;
namespace WorldClockStatusWindow.SettingsWindow;
public partial class UpdateSettingsPanel
{
public UpdateSettingsPanel()
{
InitializeComponent();
}
public override string CategoryName => Properties.Resources.optionCategoryUpdate;
private void HandleCheckVersionNowButtonClick(object sender, RoutedEventArgs e)
{
UpdateCheck.DisplayUpdateInformation(true);
}
private void OnSaveSettings(object sender, RoutedEventArgs e)
{
SaveSettings();
}
private void SaveSettings()
{
if (!HasLoaded) return;
Settings.Default.Save();
}
}

60
UpdateCheck.cs Normal file
View File

@@ -0,0 +1,60 @@
using NuGet.Versioning;
using Serilog;
using System.Windows;
using Velopack;
using Velopack.Sources;
namespace WorldClockStatusWindow
{
internal static class UpdateCheck
{
private static UpdateManager _updateManager;
public static UpdateManager UpdateManager => _updateManager ??= new UpdateManager(new GithubSource("https://github.com/ckaczor/WorldClockStatusWindow", null, false));
public static string LocalVersion => (UpdateManager.CurrentVersion ?? new SemanticVersion(0, 0, 0)).ToString();
public static bool IsInstalled => UpdateManager.IsInstalled;
public static async void DisplayUpdateInformation(bool showIfCurrent)
{
UpdateInfo newVersion = null;
if (IsInstalled)
{
newVersion = await UpdateManager.CheckForUpdatesAsync();
}
if (newVersion != null)
{
// Format the check title
var updateCheckTitle = string.Format(Properties.Resources.UpdateCheckTitle, Properties.Resources.ApplicationName);
// Format the message
var updateCheckMessage = string.Format(Properties.Resources.UpdateCheckNewVersion, Properties.Resources.ApplicationName, newVersion.TargetFullRelease.Version);
// Ask the user to update
if (MessageBox.Show(updateCheckMessage, updateCheckTitle, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
return;
Log.Logger.Information("Downloading update");
await UpdateManager.DownloadUpdatesAsync(newVersion);
Log.Logger.Information("Installing update");
UpdateManager.ApplyUpdatesAndRestart(newVersion);
}
else if (showIfCurrent)
{
// Format the check title
var updateCheckTitle = string.Format(Properties.Resources.UpdateCheckTitle, Properties.Resources.ApplicationName);
// Format the message
var updateCheckMessage = string.Format(Properties.Resources.UpdateCheckCurrent, Properties.Resources.ApplicationName);
MessageBox.Show(updateCheckMessage, updateCheckTitle, MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
}

View File

@@ -1,4 +1,5 @@
using ChrisKaczor.Wpf.Windows.FloatingStatusWindow;
using ChrisKaczor.Wpf.Windows;
using ChrisKaczor.Wpf.Windows.FloatingStatusWindow;
using Serilog;
using System;
using System.Collections.Generic;
@@ -9,7 +10,6 @@ using System.Text.Json;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Threading;
using ChrisKaczor.Wpf.Windows;
using WorldClockStatusWindow.SettingsWindow;
namespace WorldClockStatusWindow;
@@ -38,14 +38,17 @@ internal class WindowSource : IWindowSource, IDisposable
{
try
{
if (!Program.UpdateManager.IsInstalled)
if (!UpdateCheck.IsInstalled)
return false;
if (!Properties.Settings.Default.CheckVersionAtStartup)
return false;
Log.Logger.Information("Checking for update");
await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText("Checking for update..."));
var newVersion = await Program.UpdateManager.CheckForUpdatesAsync();
var newVersion = await UpdateCheck.UpdateManager.CheckForUpdatesAsync();
if (newVersion == null)
return false;
@@ -54,13 +57,13 @@ internal class WindowSource : IWindowSource, IDisposable
await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText("Downloading update..."));
await Program.UpdateManager.DownloadUpdatesAsync(newVersion);
await UpdateCheck.UpdateManager.DownloadUpdatesAsync(newVersion);
Log.Logger.Information("Installing update");
await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText("Installing update..."));
Program.UpdateManager.ApplyUpdatesAndRestart(newVersion);
UpdateCheck.UpdateManager.ApplyUpdatesAndRestart(newVersion);
}
catch (Exception e)
{
@@ -158,6 +161,7 @@ internal class WindowSource : IWindowSource, IDisposable
var categoryPanels = new List<CategoryPanelBase>
{
new GeneralSettingsPanel(),
new UpdateSettingsPanel(),
new AboutSettingsPanel()
};

View File

@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Kaczor/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>