2020-01-18 22:09:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
|
using System.Reactive.Subjects;
|
|
|
|
|
using System.Windows;
|
2020-01-29 23:41:53 +00:00
|
|
|
|
using System.Windows.Input;
|
2020-01-18 22:09:32 +00:00
|
|
|
|
using ReactiveUI;
|
|
|
|
|
using ReactiveUI.Fody.Helpers;
|
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
using Wabbajack.Lib.FileUploader;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
|
|
|
|
public class AuthorFilesVM : BackNavigatingVM
|
|
|
|
|
{
|
2020-01-29 23:41:53 +00:00
|
|
|
|
private readonly ObservableAsPropertyHelper<Visibility> _isVisible;
|
|
|
|
|
public Visibility IsVisible => _isVisible.Value;
|
2020-01-18 22:09:32 +00:00
|
|
|
|
|
2020-01-29 23:41:53 +00:00
|
|
|
|
|
|
|
|
|
private readonly ObservableAsPropertyHelper<string> _selectedFile;
|
|
|
|
|
|
|
|
|
|
public ICommand SelectFile { get; }
|
|
|
|
|
public ICommand HyperlinkCommand { get; }
|
2020-01-18 22:09:32 +00:00
|
|
|
|
public IReactiveCommand Upload { get; }
|
2020-01-29 23:41:53 +00:00
|
|
|
|
|
|
|
|
|
[Reactive] public double UploadProgress { get; set; }
|
|
|
|
|
[Reactive] public string FinalUrl { get; set; }
|
2020-01-18 22:09:32 +00:00
|
|
|
|
|
|
|
|
|
private WorkQueue Queue = new WorkQueue(1);
|
|
|
|
|
|
2020-01-29 23:41:53 +00:00
|
|
|
|
public FilePickerVM Picker { get;}
|
|
|
|
|
|
|
|
|
|
private Subject<bool> _isUploading = new Subject<bool>();
|
|
|
|
|
private IObservable<bool> IsUploading { get; }
|
|
|
|
|
|
2020-01-18 22:09:32 +00:00
|
|
|
|
public AuthorFilesVM(SettingsVM vm) : base(vm.MWVM)
|
|
|
|
|
{
|
2020-01-29 23:41:53 +00:00
|
|
|
|
IsUploading = _isUploading;
|
|
|
|
|
Picker = new FilePickerVM(this);
|
2020-01-18 22:09:32 +00:00
|
|
|
|
|
2020-01-29 23:41:53 +00:00
|
|
|
|
_isVisible = AuthorAPI.HaveAuthorAPIKey.Select(h => h ? Visibility.Visible : Visibility.Collapsed)
|
|
|
|
|
.ToProperty(this, x => x.IsVisible);
|
|
|
|
|
|
|
|
|
|
SelectFile = Picker.ConstructTypicalPickerCommand(IsUploading.StartWith(false).Select(u => !u));
|
|
|
|
|
|
|
|
|
|
HyperlinkCommand = ReactiveCommand.Create(() => Clipboard.SetText(FinalUrl));
|
|
|
|
|
|
2020-01-18 22:09:32 +00:00
|
|
|
|
Upload = ReactiveCommand.Create(async () =>
|
|
|
|
|
{
|
2020-01-29 23:41:53 +00:00
|
|
|
|
_isUploading.OnNext(true);
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-30 20:38:46 +00:00
|
|
|
|
FinalUrl = await AuthorAPI.UploadFile(Picker.TargetPath,
|
2020-01-30 04:29:20 +00:00
|
|
|
|
progress => UploadProgress = progress);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
FinalUrl = ex.ToString();
|
2020-01-29 23:41:53 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2020-04-26 12:02:03 +00:00
|
|
|
|
FinalUrl = FinalUrl.Replace(" ", "%20");
|
2020-01-29 23:41:53 +00:00
|
|
|
|
_isUploading.OnNext(false);
|
|
|
|
|
}
|
|
|
|
|
}, IsUploading.StartWith(false).Select(u => !u)
|
|
|
|
|
.CombineLatest(Picker.WhenAnyValue(t => t.TargetPath).Select(f => f != null),
|
|
|
|
|
(a, b) => a && b));
|
2020-01-18 22:09:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|