Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
Implement updater
Browse files Browse the repository at this point in the history
  • Loading branch information
remiX- committed Apr 21, 2018
1 parent 4b59bb8 commit 7473513
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 46 deletions.
8 changes: 3 additions & 5 deletions YouTubeTool/Services/IUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ namespace YouTubeTool.Services
{
public interface IUpdateService
{
Task<Version> CheckForUpdatesAsync();
bool NeedRestart { get; set; }

Task ApplyUpdate();
Task<Version> CheckPrepareUpdateAsync();

Task PrepareUpdateAsync();

void ApplyUpdateAsync(bool restart = true);
void FinalizeUpdate();
}
}
64 changes: 25 additions & 39 deletions YouTubeTool/Services/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,53 @@ namespace YouTubeTool.Services
{
public class UpdateService : IUpdateService
{
private readonly UpdateManager _updateManager;
private readonly IUpdateManager _manager;

private Version _lastVersion;
private bool _applied;
private Version _updateVersion;
private bool _updateFinalized;

public bool NeedRestart { get; set; }

public UpdateService()
{
_updateManager = new UpdateManager(new GithubPackageResolver("remiX-", "YouTube-tool", "youtube.tool.zip"), new ZipPackageExtractor());
_manager = new UpdateManager(new GithubPackageResolver("remiX-", "YouTube-tool", "YouTubeTool.zip"), new ZipPackageExtractor());
}

public async Task<Version> CheckForUpdatesAsync()
public async Task<Version> CheckPrepareUpdateAsync()
{
#if DEBUG
// Never update in DEBUG mode
return null;
//return null;
#endif

// Remove some junk left over from last update
_updateManager.Cleanup();
// Cleanup leftover files
_manager.Cleanup();

// Check for updates
var check = await _updateManager.CheckForUpdatesAsync();

// Return latest version or null if running latest version already
return check.CanUpdate ? _lastVersion = check.LastVersion : null;
}

public async Task ApplyUpdate()
{
// Prepare an update so it can be applied later
// (supports optional progress reporting and cancellation)
await _updateManager.PrepareUpdateAsync(_lastVersion);
var check = await _manager.CheckForUpdatesAsync();
if (!check.CanUpdate)
return null;

// Launch an executable that will apply the update
// (can optionally restart application on completion)
_updateManager.LaunchUpdater(_lastVersion);
// Prepare the update
if (!_manager.IsUpdatePrepared(check.LastVersion))
await _manager.PrepareUpdateAsync(check.LastVersion);

// External updater will wait until the application exits
Environment.Exit(0);
return _updateVersion = check.LastVersion;
}

public async Task PrepareUpdateAsync()
public void FinalizeUpdate()
{
if (_lastVersion == null)
// Check if an update is pending
if (_updateVersion == null)
return;

// Download and prepare update
await _updateManager.PrepareUpdateAsync(_lastVersion);
}

public void ApplyUpdateAsync(bool restart = true)
{
if (_lastVersion == null)
// Check if the update has already been finalized
if (_updateFinalized)
return;
if (_applied)
return;

// Enqueue an update
_updateManager.LaunchUpdater(_lastVersion, restart);

_applied = true;
// Launch the updater
_manager.LaunchUpdater(_updateVersion, NeedRestart);
_updateFinalized = true;
}
}
}
29 changes: 27 additions & 2 deletions YouTubeTool/Windows/WindowMain.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
using System.Windows;
using YouTubeTool.Core;
using YouTubeTool.Services;
using YouTubeTool.ViewModels;

namespace YouTubeTool.Windows
{
public partial class WindowMain : Window
{
#region Vars
MainViewModel MyViewModel;
private MainViewModel MyViewModel;

private readonly IUpdateService _updateService = new UpdateService();
#endregion

public WindowMain()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
MyViewModel = DataContext as MainViewModel;
MyViewModel.Query = "https://www.youtube.com/playlist?list=PLyiJecar_vAhAQNqZtbSfCLH-LpUeBnxh";
Expand All @@ -26,6 +29,25 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
Top = (SystemParameters.PrimaryScreenHeight - Height) / 2;

WindowState = AppGlobal.Settings.Windows["Main"].Maximized ? WindowState.Maximized : WindowState.Normal;

// Check and prepare update
try
{
var updateVersion = await _updateService.CheckPrepareUpdateAsync();
if (updateVersion != null)
{
MainSnackbar.MessageQueue.Enqueue($"Update to DiscordChatExporter v{updateVersion} will be installed when you exit", "INSTALL NOW",
() =>
{
_updateService.NeedRestart = true;
Application.Current.Shutdown();
});
}
}
catch
{
MainSnackbar.MessageQueue.Enqueue("Failed to perform application auto-update");
}
}

private void Window_Closed(object sender, System.EventArgs e)
Expand All @@ -38,6 +60,9 @@ private void Window_Closed(object sender, System.EventArgs e)

AppGlobal.Settings.Windows["Main"].Maximized = WindowState == WindowState.Maximized;
AppGlobal.Settings.Save();

// Finalize updates if available
_updateService.FinalizeUpdate();
}
}
}

0 comments on commit 7473513

Please sign in to comment.