Streamline auto-update process and refactor

This commit is contained in:
Alexey Golub
2019-09-15 14:11:06 +03:00
parent 82af36c90d
commit 12a5091d73
4 changed files with 56 additions and 59 deletions

View File

@@ -9,8 +9,6 @@ namespace DiscordChatExporter.Gui.Services
{
public class UpdateService : IDisposable
{
private readonly SettingsService _settingsService;
private readonly IUpdateManager _updateManager = new UpdateManager(
new GithubPackageResolver("Tyrrrz", "DiscordChatExporter", "DiscordChatExporter.zip"),
new ZipPackageExtractor());
@@ -18,36 +16,25 @@ namespace DiscordChatExporter.Gui.Services
private Version _updateVersion;
private bool _updaterLaunched;
public UpdateService(SettingsService settingsService)
public async Task<Version> CheckForUpdatesAsync()
{
_settingsService = settingsService;
var check = await _updateManager.CheckForUpdatesAsync();
return check.CanUpdate ? check.LastVersion : null;
}
public async Task<Version> CheckPrepareUpdateAsync()
public async Task PrepareUpdateAsync(Version version)
{
try
{
// If auto-update is disabled - don't check for updates
if (!_settingsService.IsAutoUpdateEnabled)
return null;
// Check for updates
var check = await _updateManager.CheckForUpdatesAsync();
if (!check.CanUpdate)
return null;
// Prepare the update
await _updateManager.PrepareUpdateAsync(check.LastVersion);
return _updateVersion = check.LastVersion;
await _updateManager.PrepareUpdateAsync(_updateVersion = version);
}
catch (UpdaterAlreadyLaunchedException)
{
return null;
// Ignore race conditions
}
catch (LockFileNotAcquiredException)
{
return null;
// Ignore race conditions
}
}
@@ -55,23 +42,20 @@ namespace DiscordChatExporter.Gui.Services
{
try
{
// Check if an update is pending
if (_updateVersion == null)
if (_updateVersion == null || _updaterLaunched)
return;
// Check if the updater has already been launched
if (_updaterLaunched)
return;
// Launch the updater
_updateManager.LaunchUpdater(_updateVersion, needRestart);
_updaterLaunched = true;
}
catch (UpdaterAlreadyLaunchedException)
{
// Ignore race conditions
}
catch (LockFileNotAcquiredException)
{
// Ignore race conditions
}
}