Skip to content

Commit

Permalink
Minor enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Feb 27, 2022
1 parent 88c3822 commit 29d448c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion JL/GUI/PreferencesWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private void MotivationKeyGestureTextBoxButton_Click(object sender, RoutedEventA

private void CheckForJLUpdatesButton_Click(object sender, RoutedEventArgs e)
{
Utils.CheckForJLUpdates();
Utils.CheckForJLUpdates(false);
}
}
}
2 changes: 1 addition & 1 deletion JL/Utilities/MainWindowUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void InitializeMainWindow()

if (ConfigManager.CheckForJLUpdatesOnStartUp)
{
Utils.CheckForJLUpdates();
Utils.CheckForJLUpdates(true);
}
}
}
Expand Down
55 changes: 38 additions & 17 deletions JL/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}

Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 29d448c

Please sign in to comment.