Skip to content

Commit

Permalink
manual updating in about window
Browse files Browse the repository at this point in the history
  • Loading branch information
Tensei committed Jun 1, 2021
1 parent 60e1054 commit 7ae1cda
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 30 deletions.
37 changes: 10 additions & 27 deletions anidow/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
#if RELEASE
using System.IO;
using System.Windows.Threading;
using Onova;
using Onova.Models;
using Onova.Services;

using MessageBox = AdonisUI.Controls.MessageBox;
using MessageBoxImage = AdonisUI.Controls.MessageBoxImage;
Expand All @@ -50,13 +47,15 @@ public class Bootstrapper : Bootstrapper<ShellViewModel>
private ILogger _logger;
private TaskbarIcon _taskBarIcon;
private ShellView _shell;
private UpdateManager _updateManager;

// Configure the IoC container in here
protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
builder.Bind<Assembly>().ToInstance(Assembly.GetExecutingAssembly());
var tracker = InitTracker();
builder.Bind<Tracker>().ToInstance(tracker);


var logViewModel = new LogViewModel();
InitLogger(logViewModel);
Expand All @@ -80,6 +79,10 @@ protected override void ConfigureIoC(IStyletIoCBuilder builder)

_httpClient = InitHttpClient();
builder.Bind<HttpClient>().ToInstance(_httpClient);

_updateManager = new UpdateManager(_httpClient);
builder.Bind<UpdateManager>().ToInstance(_updateManager);

_shell = new ShellView(tracker);
if (Args.Length > 0 && Args[0] == "/autostart")
{
Expand Down Expand Up @@ -208,7 +211,6 @@ protected override async void Configure()
JobManager.JobException += info =>
_logger.Error(info.Exception, "An error just happened with a scheduled job");


var selfContained = false;
#if SELF_CONTAINED && RELEASE
selfContained = true;
Expand All @@ -219,34 +221,15 @@ protected override async void Configure()
#if RELEASE
try
{
var manager = new UpdateManager(
AssemblyMetadata.FromAssembly(
Assembly.GetEntryAssembly(),
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName),
new GithubPackageResolver(
_httpClient,
"MemeLabs",
"Anidow",
selfContained ? "anidow-full.zip" : "anidow.zip"),
new ZipPackageExtractor());

var check = await manager.CheckForUpdatesAsync();
if (!check.CanUpdate || check.LastVersion == null)
var (check, hasUpdate) = await _updateManager.HasUpdate();
if (hasUpdate && check != null)
{
return;
await _updateManager.Update(check);
}

// Prepare the latest update
await manager.PrepareUpdateAsync(check.LastVersion);

// Launch updater and exit
manager.LaunchUpdater(check.LastVersion);

Environment.Exit(0);
}
catch (Exception e)
{
_logger.Error(e, "failed updating the app");
_logger.Error(e, "failed updating anidow");
}
#endif
}
Expand Down
60 changes: 60 additions & 0 deletions anidow/Helpers/UpdateManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Onova;
using Onova.Models;
using Onova.Services;

namespace Anidow.Helpers
{
public class UpdateManager
{
private readonly Onova.UpdateManager _updateManager;

public UpdateManager(HttpClient httpClient)
{
var selfContained = false;
#if SELF_CONTAINED && RELEASE
selfContained = true;
#endif
_updateManager = new Onova.UpdateManager(
AssemblyMetadata.FromAssembly(
Assembly.GetEntryAssembly(),
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName),
new GithubPackageResolver(
httpClient,
"MemeLabs",
"Anidow",
selfContained ? "anidow-full.zip" : "anidow.zip"),
new ZipPackageExtractor());

}

public async Task<(CheckForUpdatesResult, bool)> HasUpdate()
{
var check = await _updateManager.CheckForUpdatesAsync();
if (!check.CanUpdate || check.LastVersion == null)
{
return (null, false);
}

return (check, true);
}

public async Task Update(CheckForUpdatesResult check)
{

// Prepare the latest update
await _updateManager.PrepareUpdateAsync(check.LastVersion);

// Launch updater and exit
_updateManager.LaunchUpdater(check.LastVersion);

Environment.Exit(0);
}
}
}
32 changes: 30 additions & 2 deletions anidow/Pages/AboutView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
mc:Ignorable="d">
<Grid Margin="4">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid>
<DockPanel HorizontalAlignment="Center">
<TextBlock
Margin="0,0,0,16"
Margin="0,0,0,8"
DockPanel.Dock="Bottom"
TextAlignment="Center">
<Run Text="Author: Tensei C." />
Expand All @@ -47,7 +48,34 @@
</DockPanel>
</Grid>

<Grid Grid.Row="1">
<Grid
Grid.Row="1"
Margin="0,0,0,4"
HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<TextBlock Margin="0,0,0,4" Text="{Binding UpdateMessage}"/>
<Button
Visibility="{Binding HasUpdate, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}}"
Grid.Row="1"
HorizontalAlignment="Center"
Command="{s:Action CheckForUpdate,
ActionNotFound=Disable}"
Content="Check for Updates" />
<Button
HorizontalAlignment="Center"
Visibility="{Binding HasUpdate, Converter={x:Static s:BoolToVisibilityConverter.Instance}}"
Grid.Row="1"
Command="{s:Action UpdateNow,
ActionNotFound=Disable}"
Content="Update now" />

</Grid>

<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
Expand Down
50 changes: 49 additions & 1 deletion anidow/Pages/AboutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Anidow.Helpers;
using Anidow.Model;
using Anidow.Utils;
using Humanizer;
using Newtonsoft.Json;
using Onova.Models;
using Serilog;
using Stylet;

namespace Anidow.Pages
{
public class AboutViewModel : Screen
{
private readonly Assembly _assembly;
private readonly UpdateManager _updateManager;
private readonly ILogger _logger;
private CheckForUpdatesResult _lastCheck;

public AboutViewModel(Assembly assembly)
public AboutViewModel(Assembly assembly, UpdateManager updateManager, ILogger logger)
{
_assembly = assembly;
_updateManager = updateManager;
_logger = logger;
DisplayName = "About";
var licenses = assembly.GetManifestResourceNames().Single(p => p.EndsWith("licenses.json"));
using var stream = assembly.GetManifestResourceStream(licenses);
Expand All @@ -30,10 +40,48 @@ public AboutViewModel(Assembly assembly)
public string ProjectUrl => "https://github.com/MemeLabs/Anidow";
public string AssemblyVersionString => $"{_assembly.GetName().Version} {(Environment.Is64BitProcess ? "(x64)" : "(x32)")}";
public string Product => "Anidow";
public string UpdateMessage { get; private set; }
public bool HasUpdate { get; private set; }

protected override async void OnInitialActivate()
{
await CheckForUpdate();
}

public void OpenProjectUrl()
{
LinkUtil.Open(ProjectUrl);
}

public bool CanCheckForUpdate { get; set; } = true;
public async Task CheckForUpdate()
{
CanCheckForUpdate = false;
var (check, hasUpdate) = await _updateManager.HasUpdate();
if (!hasUpdate)
{
UpdateMessage = $"You have the latest version";
}
else if (check is not null)
{
HasUpdate = true;
UpdateMessage = $"New version is out (v{check.LastVersion})";
_lastCheck = check;
}

CanCheckForUpdate = true;
}

public async Task UpdateNow()
{
try
{
await _updateManager.Update(_lastCheck);
}
catch (Exception e)
{
_logger.Error(e, "failed updating anidow");
}
}
}
}

0 comments on commit 7ae1cda

Please sign in to comment.