From 29d448c5e67a24e8252a21ab8b2d6e4590f50ab8 Mon Sep 17 00:00:00 2001 From: rampaa Date: Mon, 28 Feb 2022 00:03:57 +0300 Subject: [PATCH] Minor enhancements --- JL/GUI/PreferencesWindow.xaml.cs | 2 +- JL/Utilities/MainWindowUtilities.cs | 2 +- JL/Utilities/Utils.cs | 55 ++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/JL/GUI/PreferencesWindow.xaml.cs b/JL/GUI/PreferencesWindow.xaml.cs index d578cd22..3ee9316d 100644 --- a/JL/GUI/PreferencesWindow.xaml.cs +++ b/JL/GUI/PreferencesWindow.xaml.cs @@ -372,7 +372,7 @@ private void MotivationKeyGestureTextBoxButton_Click(object sender, RoutedEventA private void CheckForJLUpdatesButton_Click(object sender, RoutedEventArgs e) { - Utils.CheckForJLUpdates(); + Utils.CheckForJLUpdates(false); } } } diff --git a/JL/Utilities/MainWindowUtilities.cs b/JL/Utilities/MainWindowUtilities.cs index 3b2a151e..909e44f8 100644 --- a/JL/Utilities/MainWindowUtilities.cs +++ b/JL/Utilities/MainWindowUtilities.cs @@ -80,7 +80,7 @@ public static void InitializeMainWindow() if (ConfigManager.CheckForJLUpdatesOnStartUp) { - Utils.CheckForJLUpdates(); + Utils.CheckForJLUpdates(true); } } } diff --git a/JL/Utilities/Utils.cs b/JL/Utilities/Utils.cs index 6386778d..aa184f95 100644 --- a/JL/Utilities/Utils.cs +++ b/JL/Utilities/Utils.cs @@ -401,26 +401,39 @@ public static void Motivate(string motivationFolder) } } - public static async void CheckForJLUpdates() + public static async void CheckForJLUpdates(bool isAutoCheck) { - HttpResponseMessage response = await Storage.Client.GetAsync(Storage.RepoUrl + "releases/latest"); - string responseUri = response.RequestMessage.RequestUri.ToString(); - Version latestVersion = new(responseUri[(responseUri.LastIndexOf("/") + 1)..]); - if (latestVersion > Storage.Version) + try { - if (MessageBox.Show("A new version of JL is available. Would you like to download it now?", "", - MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes, - MessageBoxOptions.DefaultDesktopOnly) == MessageBoxResult.Yes) + HttpResponseMessage response = await Storage.Client.GetAsync(Storage.RepoUrl + "releases/latest"); + string responseUri = response.RequestMessage.RequestUri.ToString(); + Version latestVersion = new(responseUri[(responseUri.LastIndexOf("/") + 1)..]); + if (latestVersion > Storage.Version) { - await UpdateJL(latestVersion).ConfigureAwait(false); + if (MessageBox.Show("A new version of JL is available. Would you like to download it now?", "", + MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes, + MessageBoxOptions.DefaultDesktopOnly) == MessageBoxResult.Yes) + { + MessageBox.Show( + $"This may take a while. Please don't manually shut down the program until it's updated.", + "", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK, + MessageBoxOptions.DefaultDesktopOnly); + + await UpdateJL(latestVersion).ConfigureAwait(false); + } } - } - else + else if (!isAutoCheck) + { + MessageBox.Show("JL is up to date", "", + MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.Yes, + MessageBoxOptions.DefaultDesktopOnly); + } + } + catch { - MessageBox.Show("JL is up to date", "", - MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.Yes, - MessageBoxOptions.DefaultDesktopOnly); + Logger.Warning("Couldn't update JL."); + Alert(AlertLevel.Warning, "Couldn't update JL."); } } @@ -435,10 +448,18 @@ public static async Task UpdateJL(Version latestVersion) { Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); ZipArchive archive = new(responseStream); - Directory.CreateDirectory(Path.Join(Storage.ApplicationPath, "tmp")); - archive.ExtractToDirectory(Path.Join(Storage.ApplicationPath, "tmp")); + + string tmpDirectory = Path.Join(Storage.ApplicationPath, "tmp"); + + if (Directory.Exists(tmpDirectory)) + { + Directory.Delete(tmpDirectory, true); + } + + Directory.CreateDirectory(tmpDirectory); + archive.ExtractToDirectory(tmpDirectory); Process.Start(new ProcessStartInfo("cmd", $"/c start {Path.Join(Storage.ApplicationPath, "update-helper.cmd")}") { CreateNoWindow = true }); - Application.Current.Shutdown(); + Environment.Exit(0); } } }