[Refactor] Update commands to use asynchronous patterns with ReactiveCommand

This commit is contained in:
2025-07-07 22:41:08 +01:00
parent ca1708a337
commit 9818a8c416
4 changed files with 43 additions and 40 deletions

View File

@@ -117,6 +117,6 @@ public class App : Application
}) })
return; return;
mainWindowViewModel.ExecuteSettingsCommand(); _ = mainWindowViewModel.ExecuteSettingsCommandAsync();
} }
} }

View File

@@ -25,6 +25,7 @@
using System; using System;
using System.Reactive; using System.Reactive;
using System.Threading.Tasks;
using ReactiveUI; using ReactiveUI;
using RomRepoMgr.Core.EventArgs; using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Models; using RomRepoMgr.Core.Models;
@@ -61,7 +62,7 @@ public class EditDatViewModel : ViewModelBase
_date = romSet.Date; _date = romSet.Date;
_description = romSet.Description; _description = romSet.Description;
_homepage = romSet.Homepage; _homepage = romSet.Homepage;
SaveCommand = ReactiveCommand.Create(ExecuteSaveCommand); SaveCommand = ReactiveCommand.CreateFromTask(ExecuteSaveCommandAsync);
CancelCommand = ReactiveCommand.Create(ExecuteCloseCommand); CancelCommand = ReactiveCommand.Create(ExecuteCloseCommand);
CloseCommand = ReactiveCommand.Create(ExecuteCloseCommand); CloseCommand = ReactiveCommand.Create(ExecuteCloseCommand);
} }
@@ -193,9 +194,9 @@ public class EditDatViewModel : ViewModelBase
void ExecuteCloseCommand() => _view.Close(); void ExecuteCloseCommand() => _view.Close();
async void ExecuteSaveCommand() async Task ExecuteSaveCommandAsync()
{ {
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); await using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
RomSet romSetDb = await ctx.RomSets.FindAsync(_romSet.Id); RomSet romSetDb = await ctx.RomSets.FindAsync(_romSet.Id);

View File

@@ -29,6 +29,7 @@ using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reactive; using System.Reactive;
using System.Threading.Tasks;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
@@ -54,18 +55,18 @@ public class MainWindowViewModel : ViewModelBase
{ {
_view = view; _view = view;
ExitCommand = ReactiveCommand.Create(ExecuteExitCommand); ExitCommand = ReactiveCommand.Create(ExecuteExitCommand);
SettingsCommand = ReactiveCommand.Create(ExecuteSettingsCommand); SettingsCommand = ReactiveCommand.CreateFromTask(ExecuteSettingsCommandAsync);
AboutCommand = ReactiveCommand.Create(ExecuteAboutCommand); AboutCommand = ReactiveCommand.Create(ExecuteAboutCommand);
ImportDatCommand = ReactiveCommand.Create(ExecuteImportDatCommand); ImportDatCommand = ReactiveCommand.CreateFromTask(ExecuteImportDatCommandAsync);
ImportDatFolderCommand = ReactiveCommand.Create(ExecuteImportDatFolderCommand); ImportDatFolderCommand = ReactiveCommand.CreateFromTask(ExecuteImportDatFolderCommandAsync);
ImportRomFolderCommand = ReactiveCommand.Create(ExecuteImportRomFolderCommand); ImportRomFolderCommand = ReactiveCommand.CreateFromTask(ExecuteImportRomFolderCommandAsync);
DeleteRomSetCommand = ReactiveCommand.Create(ExecuteDeleteRomSetCommand); DeleteRomSetCommand = ReactiveCommand.CreateFromTask(ExecuteDeleteRomSetCommandAsync);
EditRomSetCommand = ReactiveCommand.Create(ExecuteEditRomSetCommand); EditRomSetCommand = ReactiveCommand.Create(ExecuteEditRomSetCommand);
ExportDatCommand = ReactiveCommand.Create(ExecuteExportDatCommand); ExportDatCommand = ReactiveCommand.CreateFromTask(ExecuteExportDatCommandAsync);
ExportRomsCommand = ReactiveCommand.Create(ExecuteExportRomsCommand); ExportRomsCommand = ReactiveCommand.CreateFromTask(ExecuteExportRomsCommandAsync);
MountCommand = ReactiveCommand.Create(ExecuteMountCommand); MountCommand = ReactiveCommand.CreateFromTask(ExecuteMountCommandAsync);
UmountCommand = ReactiveCommand.Create(ExecuteUmountCommand); UmountCommand = ReactiveCommand.Create(ExecuteUmountCommand);
UpdateStatsCommand = ReactiveCommand.Create(ExecuteUpdateStatsCommand); UpdateStatsCommand = ReactiveCommand.CreateFromTask(ExecuteUpdateStatsCommandAsync);
RomSets = new ObservableCollection<RomSetModel>(romSets); RomSets = new ObservableCollection<RomSetModel>(romSets);
} }
@@ -135,11 +136,12 @@ public class MainWindowViewModel : ViewModelBase
set => this.RaiseAndSetIfChanged(ref _selectedRomSet, value); set => this.RaiseAndSetIfChanged(ref _selectedRomSet, value);
} }
internal async void ExecuteSettingsCommand() internal Task ExecuteSettingsCommandAsync()
{ {
var dialog = new SettingsDialog(); var dialog = new SettingsDialog();
dialog.DataContext = new SettingsViewModel(dialog); dialog.DataContext = new SettingsViewModel(dialog);
await dialog.ShowDialog(_view);
return dialog.ShowDialog(_view);
} }
internal void ExecuteExitCommand() => internal void ExecuteExitCommand() =>
@@ -152,7 +154,7 @@ public class MainWindowViewModel : ViewModelBase
_ = dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
} }
async void ExecuteImportDatCommand() async Task ExecuteImportDatCommandAsync()
{ {
var dlgOpen = new OpenFileDialog var dlgOpen = new OpenFileDialog
{ {
@@ -180,13 +182,13 @@ public class MainWindowViewModel : ViewModelBase
var importDatViewModel = new ImportDatViewModel(dialog, result[0]); var importDatViewModel = new ImportDatViewModel(dialog, result[0]);
importDatViewModel.RomSetAdded += ImportDatViewModelOnRomSetAdded; importDatViewModel.RomSetAdded += ImportDatViewModelOnRomSetAdded;
dialog.DataContext = importDatViewModel; dialog.DataContext = importDatViewModel;
await dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
} }
void ImportDatViewModelOnRomSetAdded(object sender, RomSetEventArgs e) => void ImportDatViewModelOnRomSetAdded(object sender, RomSetEventArgs e) =>
Dispatcher.UIThread.Post(() => RomSets.Add(e.RomSet)); Dispatcher.UIThread.Post(() => RomSets.Add(e.RomSet));
async void ExecuteImportDatFolderCommand() async Task ExecuteImportDatFolderCommandAsync()
{ {
var dlgOpen = new OpenFolderDialog var dlgOpen = new OpenFolderDialog
{ {
@@ -201,10 +203,10 @@ public class MainWindowViewModel : ViewModelBase
var importDatFolderViewModel = new ImportDatFolderViewModel(dialog, result); var importDatFolderViewModel = new ImportDatFolderViewModel(dialog, result);
importDatFolderViewModel.RomSetAdded += ImportDatViewModelOnRomSetAdded; importDatFolderViewModel.RomSetAdded += ImportDatViewModelOnRomSetAdded;
dialog.DataContext = importDatFolderViewModel; dialog.DataContext = importDatFolderViewModel;
await dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
} }
async void ExecuteImportRomFolderCommand() async Task ExecuteImportRomFolderCommandAsync()
{ {
var dlgOpen = new OpenFolderDialog var dlgOpen = new OpenFolderDialog
{ {
@@ -218,10 +220,10 @@ public class MainWindowViewModel : ViewModelBase
var dialog = new ImportRomFolder(); var dialog = new ImportRomFolder();
var importRomFolderViewModel = new ImportRomFolderViewModel(dialog, result); var importRomFolderViewModel = new ImportRomFolderViewModel(dialog, result);
dialog.DataContext = importRomFolderViewModel; dialog.DataContext = importRomFolderViewModel;
await dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
} }
async void ExecuteDeleteRomSetCommand() async Task ExecuteDeleteRomSetCommandAsync()
{ {
if(SelectedRomSet == null) return; if(SelectedRomSet == null) return;
@@ -266,7 +268,7 @@ public class MainWindowViewModel : ViewModelBase
window.Show(); window.Show();
} }
async void ExecuteExportDatCommand() async Task ExecuteExportDatCommandAsync()
{ {
if(SelectedRomSet == null) return; if(SelectedRomSet == null) return;
@@ -282,10 +284,10 @@ public class MainWindowViewModel : ViewModelBase
var dialog = new ExportDat(); var dialog = new ExportDat();
var viewModel = new ExportDatViewModel(dialog, SelectedRomSet.Sha384, result); var viewModel = new ExportDatViewModel(dialog, SelectedRomSet.Sha384, result);
dialog.DataContext = viewModel; dialog.DataContext = viewModel;
await dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
} }
async void ExecuteExportRomsCommand() async Task ExecuteExportRomsCommandAsync()
{ {
var dlgOpen = new OpenFolderDialog var dlgOpen = new OpenFolderDialog
{ {
@@ -299,10 +301,10 @@ public class MainWindowViewModel : ViewModelBase
var dialog = new ExportRoms(); var dialog = new ExportRoms();
var viewModel = new ExportRomsViewModel(dialog, result, SelectedRomSet.Id); var viewModel = new ExportRomsViewModel(dialog, result, SelectedRomSet.Id);
dialog.DataContext = viewModel; dialog.DataContext = viewModel;
await dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
} }
async void ExecuteMountCommand() async Task ExecuteMountCommandAsync()
{ {
if(Vfs != null) return; if(Vfs != null) return;
@@ -333,7 +335,7 @@ public class MainWindowViewModel : ViewModelBase
void ExecuteUmountCommand() => Vfs?.Umount(); void ExecuteUmountCommand() => Vfs?.Umount();
async void ExecuteUpdateStatsCommand() async Task ExecuteUpdateStatsCommandAsync()
{ {
ButtonResult result = await MessageBoxManager ButtonResult result = await MessageBoxManager
.GetMessageBoxStandard(Localization.DatabaseMenuUpdateStatsText, .GetMessageBoxStandard(Localization.DatabaseMenuUpdateStatsText,
@@ -347,6 +349,6 @@ public class MainWindowViewModel : ViewModelBase
var view = new UpdateStats(); var view = new UpdateStats();
var viewModel = new UpdateStatsViewModel(view); var viewModel = new UpdateStatsViewModel(view);
view.DataContext = viewModel; view.DataContext = viewModel;
await view.ShowDialog(_view); _ = view.ShowDialog(_view);
} }
} }

View File

@@ -64,10 +64,10 @@ public sealed class SettingsViewModel : ViewModelBase
_unArChanged = false; _unArChanged = false;
CloseCommand = ReactiveCommand.Create(ExecuteCloseCommand); CloseCommand = ReactiveCommand.Create(ExecuteCloseCommand);
UnArCommand = ReactiveCommand.Create(ExecuteUnArCommand); UnArCommand = ReactiveCommand.CreateFromTask(ExecuteUnArCommandAsync);
TemporaryCommand = ReactiveCommand.Create(ExecuteTemporaryCommand); TemporaryCommand = ReactiveCommand.CreateFromTask(ExecuteTemporaryCommandAsync);
RepositoryCommand = ReactiveCommand.Create(ExecuteRepositoryCommand); RepositoryCommand = ReactiveCommand.CreateFromTask(ExecuteRepositoryCommandAsync);
DatabaseCommand = ReactiveCommand.Create(ExecuteDatabaseCommand); DatabaseCommand = ReactiveCommand.CreateFromTask(ExecuteDatabaseCommandAsync);
SaveCommand = ReactiveCommand.Create(ExecuteSaveCommand); SaveCommand = ReactiveCommand.Create(ExecuteSaveCommand);
DatabasePath = Settings.Settings.Current.DatabasePath; DatabasePath = Settings.Settings.Current.DatabasePath;
@@ -149,12 +149,12 @@ public sealed class SettingsViewModel : ViewModelBase
_ = Task.Run(() => worker.CheckUnAr(UnArPath)); _ = Task.Run(() => worker.CheckUnAr(UnArPath));
} }
async void CheckUnArFailed(object sender, ErrorEventArgs args) void CheckUnArFailed(object sender, ErrorEventArgs args)
{ {
UnArVersion = ""; UnArVersion = "";
UnArPath = ""; UnArPath = "";
await MessageBoxManager.GetMessageBoxStandard(Localization.Error, $"{args.Message}", ButtonEnum.Ok, Icon.Error) _ = MessageBoxManager.GetMessageBoxStandard(Localization.Error, $"{args.Message}", ButtonEnum.Ok, Icon.Error)
.ShowWindowDialogAsync(_view); .ShowWindowDialogAsync(_view);
} }
@@ -166,7 +166,7 @@ public sealed class SettingsViewModel : ViewModelBase
void ExecuteCloseCommand() => _view.Close(); void ExecuteCloseCommand() => _view.Close();
async void ExecuteUnArCommand() async Task ExecuteUnArCommandAsync()
{ {
var dlgFile = new OpenFileDialog var dlgFile = new OpenFileDialog
{ {
@@ -185,7 +185,7 @@ public sealed class SettingsViewModel : ViewModelBase
CheckUnAr(); CheckUnAr();
} }
async void ExecuteTemporaryCommand() async Task ExecuteTemporaryCommandAsync()
{ {
var dlgFolder = new OpenFolderDialog var dlgFolder = new OpenFolderDialog
{ {
@@ -199,7 +199,7 @@ public sealed class SettingsViewModel : ViewModelBase
TemporaryPath = result; TemporaryPath = result;
} }
async void ExecuteRepositoryCommand() async Task ExecuteRepositoryCommandAsync()
{ {
var dlgFolder = new OpenFolderDialog var dlgFolder = new OpenFolderDialog
{ {
@@ -213,7 +213,7 @@ public sealed class SettingsViewModel : ViewModelBase
RepositoryPath = result; RepositoryPath = result;
} }
async void ExecuteDatabaseCommand() async Task ExecuteDatabaseCommandAsync()
{ {
var dlgFile = new SaveFileDialog var dlgFile = new SaveFileDialog
{ {