Skip to content

Commit

Permalink
Streamline auto-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Apr 23, 2024
1 parent d1d9db2 commit e767bc4
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 55 deletions.
14 changes: 2 additions & 12 deletions LightBulb/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,11 @@ public override void OnFrameworkInitializationCompleted()
);
}

private void ShowMainWindow()
{
if (ApplicationLifetime?.TryGetMainWindow() is { } window)
{
window.Show();
window.Activate();
window.Focus();
}
}

private void TrayIcon_OnClicked(object? sender, EventArgs args) => ShowMainWindow();
private void TrayIcon_OnClicked(object? sender, EventArgs args) => this.TryFocusMainWindow();

private void ShowSettingsMenuItem_OnClick(object? sender, EventArgs args)
{
ShowMainWindow();
this.TryFocusMainWindow();
_mainViewModel.ShowSettingsCommand.Execute(null);
}

Expand Down
52 changes: 29 additions & 23 deletions LightBulb/Services/UpdateService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using LightBulb.Utils.Extensions;
using Onova;
using Onova.Exceptions;
using Onova.Services;
Expand All @@ -16,29 +14,47 @@ public class UpdateService(SettingsService settingsService) : IDisposable
new ZipPackageExtractor()
);

private Version? TryGetLastPreparedUpdate() => _updateManager.GetPreparedUpdates().Max();
public async Task<Version?> CheckForUpdatesAsync()
{
if (!settingsService.IsAutoUpdateEnabled)
return null;

var check = await _updateManager.CheckForUpdatesAsync();
return check.CanUpdate ? check.LastVersion : null;
}

public Version? TryGetLastPreparedUpdate()
{
var version = _updateManager.GetPreparedUpdates().Max();
if (version <= _updateManager.Updatee.Version)
return null;

public async Task CheckPrepareUpdateAsync()
return version;
}

public async Task PrepareUpdateAsync(Version version)
{
if (!settingsService.IsAutoUpdateEnabled)
return;

try
{
var check = await _updateManager.CheckForUpdatesAsync();
if (!check.CanUpdate || check.LastVersion is null)
if (version == TryGetLastPreparedUpdate())
return;

if (check.LastVersion != TryGetLastPreparedUpdate())
await _updateManager.PrepareUpdateAsync(check.LastVersion);

await _updateManager.PrepareUpdateAsync(version);
}
catch
catch (UpdaterAlreadyLaunchedException)
{
// Failure to check for updates shouldn't crash the app
// Ignore race conditions
}
catch (LockFileNotAcquiredException)
{
// Ignore race conditions
}
}

public void FinalizePendingUpdates()
public void FinalizeUpdate(Version version)
{
if (!settingsService.IsAutoUpdateEnabled)
return;
Expand All @@ -49,17 +65,7 @@ public void FinalizePendingUpdates()

try
{
var lastPreparedUpdate = TryGetLastPreparedUpdate();
if (lastPreparedUpdate is null)
return;

if (lastPreparedUpdate <= _updateManager.Updatee.Version)
return;

_updateManager.LaunchUpdater(lastPreparedUpdate);

if (Application.Current?.ApplicationLifetime?.TryShutdown(2) != true)
Environment.Exit(2);
_updateManager.LaunchUpdater(version);
}
catch (UpdaterAlreadyLaunchedException)
{
Expand Down
13 changes: 12 additions & 1 deletion LightBulb/Utils/Extensions/AvaloniaExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;

namespace LightBulb.Utils.Extensions;
Expand All @@ -25,4 +26,14 @@ public static bool TryShutdown(this IApplicationLifetime lifetime, int exitCode

return false;
}

public static void TryFocusMainWindow(this Application application)
{
if (application.ApplicationLifetime?.TryGetMainWindow() is { } window)
{
window.Show();
window.Activate();
window.Focus();
}
}
}
71 changes: 54 additions & 17 deletions LightBulb/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Threading.Tasks;
using Avalonia;
using CommunityToolkit.Mvvm.Input;
using LightBulb.Framework;
using LightBulb.PlatformInterop;
using LightBulb.Services;
using LightBulb.Utils;
using LightBulb.Utils.Extensions;
using LightBulb.ViewModels.Components;
using LightBulb.ViewModels.Components.Settings;

Expand All @@ -18,7 +20,15 @@ UpdateService updateService
) : ViewModelBase
{
private readonly Timer _checkForUpdatesTimer =
new(TimeSpan.FromHours(3), async () => await updateService.CheckPrepareUpdateAsync());
new(
TimeSpan.FromHours(3),
async () =>
{
var updateVersion = await updateService.CheckForUpdatesAsync();
if (updateVersion is not null)
await updateService.PrepareUpdateAsync(updateVersion);
}
);

public DashboardViewModel Dashboard { get; } = viewModelManager.CreateDashboardViewModel();

Expand All @@ -39,11 +49,11 @@ Press FIX to unlock the gamma range. Administrator privileges may be required.
"CLOSE"
);

if (await dialogManager.ShowDialogAsync(dialog) == true)
{
settingsService.IsExtendedGammaRangeUnlocked = true;
settingsService.Save();
}
if (await dialogManager.ShowDialogAsync(dialog) != true)
return;

settingsService.IsExtendedGammaRangeUnlocked = true;
settingsService.Save();
}

private async Task ShowFirstTimeExperienceMessageAsync()
Expand All @@ -68,13 +78,13 @@ Press OK to open settings.
settingsService.IsAutoStartEnabled = true;
settingsService.Save();

if (await dialogManager.ShowDialogAsync(dialog) == true)
{
var settingsDialog = viewModelManager.CreateSettingsViewModel();
settingsDialog.ActivateTab<LocationSettingsTabViewModel>();
if (await dialogManager.ShowDialogAsync(dialog) != true)
return;

await dialogManager.ShowDialogAsync(settingsDialog);
}
var settingsDialog = viewModelManager.CreateSettingsViewModel();
settingsDialog.ActivateTab<LocationSettingsTabViewModel>();

await dialogManager.ShowDialogAsync(settingsDialog);
}

private async Task ShowUkraineSupportMessageAsync()
Expand All @@ -97,22 +107,49 @@ Click LEARN MORE to find ways that you can help.
settingsService.IsUkraineSupportMessageEnabled = false;
settingsService.Save();

if (await dialogManager.ShowDialogAsync(dialog) == true)
ProcessEx.StartShellExecute("https://tyrrrz.me/ukraine?source=lightbulb");
if (await dialogManager.ShowDialogAsync(dialog) != true)
return;

ProcessEx.StartShellExecute("https://tyrrrz.me/ukraine?source=lightbulb");
}

private async Task FinalizePendingUpdateAsync()
{
var updateVersion = updateService.TryGetLastPreparedUpdate();
if (updateVersion is null)
return;

var dialog = viewModelManager.CreateMessageBoxViewModel(
"Update available",
$"""
Update to {Program.Name} v{updateVersion} has been downloaded.
Do you want to install it now?
""",
"INSTALL",
"CANCEL"
);

Application.Current?.TryFocusMainWindow();

if (await dialogManager.ShowDialogAsync(dialog) != true)
return;

updateService.FinalizeUpdate(updateVersion);

if (Application.Current?.ApplicationLifetime?.TryShutdown(2) != true)
Environment.Exit(2);
}

[RelayCommand]
private async Task InitializeAsync()
{
// Finalize pending updates (and restart if necessary)
updateService.FinalizePendingUpdates();

// Load settings
settingsService.Load();

await ShowGammaRangePromptAsync();
await ShowFirstTimeExperienceMessageAsync();
await ShowUkraineSupportMessageAsync();
await FinalizePendingUpdateAsync();

_checkForUpdatesTimer.Start();
}
Expand Down
3 changes: 1 addition & 2 deletions LightBulb/Views/Dialogs/MessageBoxView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
Columns="{Binding ButtonsCount}">
<!-- OK -->
<Button
Margin="8,0"
Command="{Binding CloseCommand}"
Content="{Binding DefaultButtonText}"
IsDefault="True"
Expand All @@ -54,7 +53,7 @@

<!-- Cancel -->
<Button
Margin="8,0"
Margin="16,0,0,0"
HorizontalAlignment="Stretch"
Command="{Binding CloseCommand}"
Content="{Binding CancelButtonText}"
Expand Down

0 comments on commit e767bc4

Please sign in to comment.