[Refactor] Replace OpenFolderDialog with StorageProvider for folder selection

This commit is contained in:
2025-07-07 23:45:03 +01:00
parent a88c9000ed
commit ad27c2a9ab
2 changed files with 46 additions and 48 deletions

View File

@@ -33,6 +33,7 @@ using System.Threading.Tasks;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Platform.Storage;
using Avalonia.Threading; using Avalonia.Threading;
using MsBox.Avalonia; using MsBox.Avalonia;
using MsBox.Avalonia.Enums; using MsBox.Avalonia.Enums;
@@ -190,17 +191,16 @@ public class MainWindowViewModel : ViewModelBase
async Task ExecuteImportDatFolderCommandAsync() async Task ExecuteImportDatFolderCommandAsync()
{ {
var dlgOpen = new OpenFolderDialog IReadOnlyList<IStorageFolder> result =
await _view.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{ {
Title = Localization.ImportDatFolderDialogTitle Title = Localization.ImportDatFolderDialogTitle
}; });
string result = await dlgOpen.ShowAsync(_view); if(result.Count < 1) return;
if(result == null) return;
var dialog = new ImportDatFolder(); var dialog = new ImportDatFolder();
var importDatFolderViewModel = new ImportDatFolderViewModel(dialog, result); var importDatFolderViewModel = new ImportDatFolderViewModel(dialog, result[0].Path.LocalPath);
importDatFolderViewModel.RomSetAdded += ImportDatViewModelOnRomSetAdded; importDatFolderViewModel.RomSetAdded += ImportDatViewModelOnRomSetAdded;
dialog.DataContext = importDatFolderViewModel; dialog.DataContext = importDatFolderViewModel;
_ = dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
@@ -208,17 +208,16 @@ public class MainWindowViewModel : ViewModelBase
async Task ExecuteImportRomFolderCommandAsync() async Task ExecuteImportRomFolderCommandAsync()
{ {
var dlgOpen = new OpenFolderDialog IReadOnlyList<IStorageFolder> result =
await _view.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{ {
Title = Localization.ImportRomsFolderDialogTitle Title = Localization.ImportRomsFolderDialogTitle
}; });
string result = await dlgOpen.ShowAsync(_view); if(result.Count < 1) return;
if(result == null) return;
var dialog = new ImportRomFolder(); var dialog = new ImportRomFolder();
var importRomFolderViewModel = new ImportRomFolderViewModel(dialog, result); var importRomFolderViewModel = new ImportRomFolderViewModel(dialog, result[0].Path.LocalPath);
dialog.DataContext = importRomFolderViewModel; dialog.DataContext = importRomFolderViewModel;
_ = dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
} }
@@ -289,17 +288,16 @@ public class MainWindowViewModel : ViewModelBase
async Task ExecuteExportRomsCommandAsync() async Task ExecuteExportRomsCommandAsync()
{ {
var dlgOpen = new OpenFolderDialog IReadOnlyList<IStorageFolder> result =
await _view.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{ {
Title = Localization.ExportRomsDialogTitle Title = Localization.ExportRomsDialogTitle
}; });
string result = await dlgOpen.ShowAsync(_view); if(result.Count < 1) return;
if(result == null) return;
var dialog = new ExportRoms(); var dialog = new ExportRoms();
var viewModel = new ExportRomsViewModel(dialog, result, SelectedRomSet.Id); var viewModel = new ExportRomsViewModel(dialog, result[0].Path.LocalPath, SelectedRomSet.Id);
dialog.DataContext = viewModel; dialog.DataContext = viewModel;
_ = dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
} }
@@ -308,20 +306,19 @@ public class MainWindowViewModel : ViewModelBase
{ {
if(Vfs != null) return; if(Vfs != null) return;
var dlgOpen = new OpenFolderDialog IReadOnlyList<IStorageFolder> result =
await _view.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{ {
Title = Localization.SelectMountPointDialogTitle Title = Localization.SelectMountPointDialogTitle
}; });
string result = await dlgOpen.ShowAsync(_view); if(result.Count < 1) return;
if(result == null) return;
try try
{ {
Vfs = new Vfs(); Vfs = new Vfs();
Vfs.Umounted += VfsOnUmounted; Vfs.Umounted += VfsOnUmounted;
Vfs.MountTo(result); Vfs.MountTo(result[0].Path.LocalPath);
} }
catch(Exception) catch(Exception)
{ {

View File

@@ -24,10 +24,12 @@
*******************************************************************************/ *******************************************************************************/
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Reactive; using System.Reactive;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Avalonia.Threading; using Avalonia.Threading;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using MsBox.Avalonia; using MsBox.Avalonia;
@@ -187,30 +189,29 @@ public sealed class SettingsViewModel : ViewModelBase
async Task ExecuteTemporaryCommandAsync() async Task ExecuteTemporaryCommandAsync()
{ {
var dlgFolder = new OpenFolderDialog IReadOnlyList<IStorageFolder> result =
await _view.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{ {
Title = Localization.ChooseTemporaryFolder Title = Localization.ChooseTemporaryFolder
}; });
string result = await dlgFolder.ShowAsync(_view); if(result.Count < 1) return;
if(result == null) return; TemporaryPath = result[0].Path.LocalPath;
TemporaryPath = result;
} }
async Task ExecuteRepositoryCommandAsync() async Task ExecuteRepositoryCommandAsync()
{ {
var dlgFolder = new OpenFolderDialog IReadOnlyList<IStorageFolder> result =
await _view.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{ {
Title = Localization.ChooseRepositoryFolder Title = Localization.ChooseRepositoryFolder,
}; AllowMultiple = false
});
string result = await dlgFolder.ShowAsync(_view); if(result.Count < 1) return;
if(result == null) return; RepositoryPath = result[0].Path.LocalPath;
RepositoryPath = result;
} }
async Task ExecuteDatabaseCommandAsync() async Task ExecuteDatabaseCommandAsync()