2021-02-14 09:02:02 -05:00
|
|
|
using System;
|
2020-12-28 09:27:37 -05:00
|
|
|
using System.Threading.Tasks;
|
2021-02-14 09:02:02 -05:00
|
|
|
using System.Windows;
|
2020-12-28 09:27:37 -05:00
|
|
|
using auto_creamapi.Messenger;
|
|
|
|
using auto_creamapi.Services;
|
|
|
|
using auto_creamapi.Utils;
|
2023-12-23 16:55:26 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-12-28 09:27:37 -05:00
|
|
|
using MvvmCross.Navigation;
|
|
|
|
using MvvmCross.Plugin.Messenger;
|
|
|
|
using MvvmCross.ViewModels;
|
|
|
|
|
|
|
|
namespace auto_creamapi.ViewModels
|
|
|
|
{
|
|
|
|
public class DownloadViewModel : MvxNavigationViewModel
|
|
|
|
{
|
|
|
|
private readonly IDownloadCreamApiService _download;
|
|
|
|
private readonly IMvxNavigationService _navigationService;
|
|
|
|
private readonly MvxSubscriptionToken _token;
|
2023-12-23 16:55:26 -05:00
|
|
|
private readonly ILogger<DownloadViewModel> _logger;
|
2020-12-28 09:27:37 -05:00
|
|
|
private string _filename;
|
|
|
|
|
|
|
|
private string _info;
|
|
|
|
private double _progress;
|
|
|
|
|
2023-12-24 07:32:23 -05:00
|
|
|
private readonly Secrets _secrets = new();
|
|
|
|
|
2023-12-23 16:55:26 -05:00
|
|
|
public DownloadViewModel(ILoggerFactory loggerFactory, IMvxNavigationService navigationService,
|
|
|
|
IDownloadCreamApiService download, IMvxMessenger messenger) : base(loggerFactory, navigationService)
|
2020-12-28 09:27:37 -05:00
|
|
|
{
|
|
|
|
_navigationService = navigationService;
|
2023-12-23 16:55:26 -05:00
|
|
|
_logger = loggerFactory.CreateLogger<DownloadViewModel>();
|
2020-12-28 09:27:37 -05:00
|
|
|
_download = download;
|
|
|
|
_token = messenger.Subscribe<ProgressMessage>(OnProgressMessage);
|
2023-12-23 16:55:26 -05:00
|
|
|
_logger.LogDebug("{Count}", messenger.CountSubscriptionsFor<ProgressMessage>());
|
2020-12-28 09:27:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public string InfoLabel
|
|
|
|
{
|
|
|
|
get => _info;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_info = value;
|
|
|
|
RaisePropertyChanged(() => InfoLabel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string FilenameLabel
|
|
|
|
{
|
|
|
|
get => _filename;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_filename = value;
|
|
|
|
RaisePropertyChanged(() => FilenameLabel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public double Progress
|
|
|
|
{
|
|
|
|
get => _progress;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_progress = value;
|
|
|
|
RaisePropertyChanged(() => Progress);
|
|
|
|
RaisePropertyChanged(() => ProgressPercent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ProgressPercent => _progress.ToString("P2");
|
|
|
|
|
2021-02-14 09:02:02 -05:00
|
|
|
public override void Prepare()
|
2020-12-28 09:27:37 -05:00
|
|
|
{
|
|
|
|
InfoLabel = "Please wait...";
|
|
|
|
FilenameLabel = "";
|
|
|
|
Progress = 0.0;
|
2021-02-14 09:02:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task Initialize()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await base.Initialize().ConfigureAwait(false);
|
2023-12-24 07:32:23 -05:00
|
|
|
var download = _download.Download(_secrets.ForumUsername(), _secrets.ForumPassword());
|
2021-02-14 09:02:02 -05:00
|
|
|
var filename = await download.ConfigureAwait(false);
|
|
|
|
var extract = _download.Extract(filename);
|
|
|
|
await extract.ConfigureAwait(false);
|
|
|
|
_token.Dispose();
|
|
|
|
await _navigationService.Close(this).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
MessageBox.Show("Could not download CreamAPI!\nPlease add CreamAPI DLLs manually!\nShutting down...",
|
|
|
|
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
|
_token.Dispose();
|
|
|
|
await _navigationService.Close(this).ConfigureAwait(false);
|
|
|
|
Console.WriteLine(e);
|
|
|
|
throw;
|
|
|
|
}
|
2020-12-28 09:27:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnProgressMessage(ProgressMessage obj)
|
|
|
|
{
|
|
|
|
InfoLabel = obj.Info;
|
|
|
|
FilenameLabel = obj.Filename;
|
|
|
|
Progress = obj.PercentComplete;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|