Move UpdateService to GUI project

This commit is contained in:
Alexey Golub
2019-04-16 23:08:38 +03:00
parent afb9f52221
commit ab0ce478db
3 changed files with 4 additions and 1 deletions

View File

@@ -63,6 +63,7 @@
<Compile Include="Converters\DateTimeOffsetToDateTimeConverter.cs" />
<Compile Include="Converters\ExportFormatToStringConverter.cs" />
<Compile Include="Converters\InverseBoolConverter.cs" />
<Compile Include="Services\UpdateService.cs" />
<Compile Include="ViewModels\Components\ChannelViewModel.cs" />
<Compile Include="ViewModels\Components\GuildViewModel.cs" />
<Compile Include="ViewModels\Dialogs\ExportSetupViewModel.cs" />

View File

@@ -0,0 +1,80 @@
using System;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Services;
using Onova;
using Onova.Exceptions;
using Onova.Services;
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());
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();
}
}

View File

@@ -8,6 +8,7 @@ using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Services;
using DiscordChatExporter.Core.Services.Exceptions;
using DiscordChatExporter.Core.Services.Helpers;
using DiscordChatExporter.Gui.Services;
using DiscordChatExporter.Gui.ViewModels.Components;
using DiscordChatExporter.Gui.ViewModels.Framework;
using Gress;