Files
velopack/samples/CSharpUno/UnoSample/Presentation/MainViewModel.cs
Caelan 3128d34b14 Rework solution structure to allow more types of library projects (#196)
* Rename avalonia & wpf samples

* Rename rest of samples

* fix sample readme

* Fix compat util tests

* rename / move all src projects

* remove package lock files and move libC# again

* Add rust lib and cargo workspace

* Replace locksmith lib with new filelocksmith-rs library

* Deprecated type

* fix setup compile

* Use thiserror for error handling

* Rename some enums and formatting

* Add missing SHA256

* wip c++ library

* cpp wip

* alphabetize readme

* Try to get build working again

* Fix some conditionally compiled bits

* cross config should be in workspace root

* Fix PathHelper for new rust target dir

* Missed one old path to velopack.csproj

* remove obsolete coverage code

* testawareapp.exe no longer exists
2024-08-06 22:56:40 +01:00

73 lines
2.1 KiB
C#

using Velopack;
using Velopack.Sources;
namespace UnoSample.Presentation;
public partial class MainViewModel : ObservableObject
{
private readonly UpdateManager _updateManager;
public bool IsInstalled => _updateManager.IsInstalled;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(HasUpdate))]
private UpdateInfo? _latestUpdate;
public bool HasUpdate => LatestUpdate != null;
public string CurrentVersion => _updateManager.CurrentVersion?.ToFullString() ?? "";
[ObservableProperty]
private string? _status;
public MainViewModel(
IStringLocalizer localizer,
IOptions<AppConfig> appInfo)
{
Title = "Main";
Title += $" - {localizer["ApplicationName"]}";
Title += $" - {appInfo?.Value?.Environment}";
_updateManager = new UpdateManager(new VelopackFlowUpdateSource());
}
public string? Title { get; }
[RelayCommand(CanExecute = nameof(IsInstalled))]
private async Task CheckForUpdates()
{
if (_updateManager.IsInstalled)
{
try
{
Status = "Checking for updates...";
LatestUpdate = await _updateManager.CheckForUpdatesAsync();
Status = LatestUpdate is null ? "No updates available" : $"{LatestUpdate.TargetFullRelease.Version} - Update available";
}
catch (Exception ex)
{
Status = ex.Message;
}
}
}
[RelayCommand(CanExecute = nameof(IsInstalled))]
private async Task DownloadUpdates()
{
if (_updateManager.IsInstalled && LatestUpdate is { } latestUpdate)
{
try
{
Status = $"Downloading {0:p}";
await _updateManager.DownloadUpdatesAsync(latestUpdate, progress => Status = $"Downloading {progress / 100.0:p}");
Status = "Restarting...";
_updateManager.ApplyUpdatesAndRestart(latestUpdate.TargetFullRelease);
}
catch(Exception ex)
{
Status = ex.Message;
}
}
}
}