105 lines
No EOL
2.3 KiB
C#
105 lines
No EOL
2.3 KiB
C#
using Avalonia.Threading;
|
|
using Discovery.Loader.Models;
|
|
using Discovery.Loader.Services;
|
|
using ReactiveUI;
|
|
|
|
namespace Discovery.Loader.ViewModels;
|
|
|
|
public class MainViewModel : ViewModelBase
|
|
{
|
|
public string? Text
|
|
{
|
|
get => _text;
|
|
set => this.RaiseAndSetIfChanged(ref _text, value);
|
|
}
|
|
|
|
private string? _text;
|
|
|
|
public double Progress
|
|
{
|
|
get => _progress;
|
|
set => this.RaiseAndSetIfChanged(ref _progress, value);
|
|
}
|
|
|
|
private double _progress;
|
|
|
|
public async Task InstallAsync(UpdateService updater, Update update)
|
|
{
|
|
if (!updater.AppDir.Exists)
|
|
updater.AppDir.Create();
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
{
|
|
Text = "Install";
|
|
});
|
|
|
|
var download = await updater.DownloadAsync(update, (p) => Task.Run(() =>
|
|
{
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
{
|
|
Progress = p;
|
|
}, DispatcherPriority.Default, default);
|
|
}, default));
|
|
|
|
if (!download)
|
|
{
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
{
|
|
Text = "Failed to install";
|
|
});
|
|
|
|
await Task.Delay(3000);
|
|
return;
|
|
}
|
|
|
|
updater.Extract();
|
|
|
|
await Task.Delay(1000);
|
|
}
|
|
|
|
public async Task UpdateAsync(UpdateService updater, Update update)
|
|
{
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
{
|
|
Text = "Download Update";
|
|
});
|
|
|
|
var download = await updater.DownloadAsync(update, (p) => Task.Run(() =>
|
|
{
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
{
|
|
Progress = p;
|
|
}, DispatcherPriority.Default, default);
|
|
}, default));
|
|
|
|
if (!download)
|
|
{
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
{
|
|
Text = "Failed to download Update!";
|
|
});
|
|
|
|
await Task.Delay(3000);
|
|
return;
|
|
}
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
{
|
|
Text = "Extract Update";
|
|
});
|
|
|
|
updater.Extract();
|
|
|
|
await Task.Delay(1000);
|
|
}
|
|
|
|
public async Task ErrorAsync(string message)
|
|
{
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
{
|
|
Text = message;
|
|
});
|
|
|
|
await Task.Delay(3000);
|
|
}
|
|
} |