Files
DiscordChatExporter/DiscordChatExporter.Core.Services/UpdateService.cs
2019-04-10 23:45:21 +03:00

79 lines
2.2 KiB
C#

using System;
using System.Threading.Tasks;
using Onova;
using Onova.Exceptions;
using Onova.Services;
namespace DiscordChatExporter.Core.Services
{
public class UpdateService : IDisposable
{
private readonly SettingsService _settingsService;
private readonly IUpdateManager _updateManager = new UpdateManager(
new GithubPackageResolver("Tyrrrz", "DiscordChatExporter", "DiscordChatExporter.zip"),
new ZipPackageExtractor());
private Version _updateVersion;
private bool _updaterLaunched;
public UpdateService(SettingsService settingsService)
{
_settingsService = settingsService;
}
public async Task<Version> CheckPrepareUpdateAsync()
{
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;
}
catch (UpdaterAlreadyLaunchedException)
{
return null;
}
catch (LockFileNotAcquiredException)
{
return null;
}
}
public void FinalizeUpdate(bool needRestart)
{
try
{
// Check if an update is pending
if (_updateVersion == null)
return;
// Check if the updater has already been launched
if (_updaterLaunched)
return;
// Launch the updater
_updateManager.LaunchUpdater(_updateVersion, needRestart);
_updaterLaunched = true;
}
catch (UpdaterAlreadyLaunchedException)
{
}
catch (LockFileNotAcquiredException)
{
}
}
public void Dispose() => _updateManager.Dispose();
}
}