wabbajack/Wabbajack.App/Controls/FileSelectionBoxViewModel.cs

61 lines
2.0 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
2021-10-23 16:51:17 +00:00
using System.Collections.Generic;
2021-09-27 12:42:46 +00:00
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Threading.Tasks;
using Avalonia.Controls;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.App.ViewModels;
using Wabbajack.Paths;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.App.Controls;
public class FileSelectionBoxViewModel : ViewModelBase
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public FileSelectionBoxViewModel()
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
Activator = new ViewModelActivator();
this.WhenActivated(disposables =>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
BrowseCommand = ReactiveCommand.Create(async () =>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
if (SelectFolder)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var dialog = new OpenFolderDialog
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
Title = "Select a folder"
};
var result = await dialog.ShowAsync(App.MainWindow);
if (result != null)
Path = result.ToAbsolutePath();
}
else
{
var extensions = Extensions.Select(e => e.ToString()[1..]).ToList();
var dialog = new OpenFileDialog
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
AllowMultiple = false,
Title = "Select a file",
Filters = new List<FileDialogFilter>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
new FileDialogFilter {Extensions = extensions, Name = "*"}
}
};
var results = await dialog.ShowAsync(App.MainWindow);
if (results != null)
Path = results!.First().ToAbsolutePath();
}
}).DisposeWith(disposables);
});
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
[Reactive] public AbsolutePath Path { get; set; }
[Reactive] public Extension[] Extensions { get; set; } = Array.Empty<Extension>();
[Reactive] public bool SelectFolder { get; set; }
[Reactive] public ReactiveCommand<Unit, Task> BrowseCommand { get; set; } = null!;
2021-09-27 12:42:46 +00:00
}