Files
romrepomgr/RomRepoMgr/ViewModels/MainWindowViewModel.cs

371 lines
15 KiB
C#
Raw Normal View History

2020-08-21 23:34:07 +01:00
/******************************************************************************
// RomRepoMgr - ROM repository manager
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-11-08 19:13:57 +00:00
// Copyright © 2020-2024 Natalia Portillo
2020-08-21 23:34:07 +01:00
*******************************************************************************/
2020-09-01 11:54:16 +01:00
using System;
2020-08-22 04:40:39 +01:00
using System.Collections.Generic;
2020-08-22 03:26:43 +01:00
using System.Collections.ObjectModel;
2020-09-01 11:54:16 +01:00
using System.Diagnostics;
using System.Linq;
2020-08-22 03:13:49 +01:00
using System.Reactive;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Threading;
2020-08-24 02:29:07 +01:00
using MessageBox.Avalonia;
using MessageBox.Avalonia.Enums;
2020-08-22 03:13:49 +01:00
using ReactiveUI;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Filesystem;
2020-08-22 13:36:39 +01:00
using RomRepoMgr.Core.Models;
2020-08-30 03:00:14 +01:00
using RomRepoMgr.Resources;
2020-08-22 03:13:49 +01:00
using RomRepoMgr.Views;
2020-08-21 23:34:07 +01:00
namespace RomRepoMgr.ViewModels
2020-08-21 22:22:24 +01:00
{
public class MainWindowViewModel : ViewModelBase
{
2020-08-22 03:13:49 +01:00
readonly MainWindow _view;
2020-09-01 11:54:16 +01:00
RomSetModel _selectedRomSet;
Vfs _vfs;
2020-08-24 02:29:07 +01:00
2020-08-22 13:32:43 +01:00
public MainWindowViewModel(MainWindow view, List<RomSetModel> romSets)
2020-08-22 03:13:49 +01:00
{
2020-08-22 19:28:58 +01:00
_view = view;
ExitCommand = ReactiveCommand.Create(ExecuteExitCommand);
SettingsCommand = ReactiveCommand.Create(ExecuteSettingsCommand);
AboutCommand = ReactiveCommand.Create(ExecuteAboutCommand);
ImportDatCommand = ReactiveCommand.Create(ExecuteImportDatCommand);
ImportDatFolderCommand = ReactiveCommand.Create(ExecuteImportDatFolderCommand);
2020-08-22 21:57:05 +01:00
ImportRomFolderCommand = ReactiveCommand.Create(ExecuteImportRomFolderCommand);
2020-08-24 02:29:07 +01:00
DeleteRomSetCommand = ReactiveCommand.Create(ExecuteDeleteRomSetCommand);
EditRomSetCommand = ReactiveCommand.Create(ExecuteEditRomSetCommand);
2020-08-24 04:01:55 +01:00
ExportDatCommand = ReactiveCommand.Create(ExecuteExportDatCommand);
ExportRomsCommand = ReactiveCommand.Create(ExecuteExportRomsCommand);
MountCommand = ReactiveCommand.Create(ExecuteMountCommand);
2020-09-01 11:54:16 +01:00
UmountCommand = ReactiveCommand.Create(ExecuteUmountCommand);
UpdateStatsCommand = ReactiveCommand.Create(ExecuteUpdateStatsCommand);
2020-08-22 19:28:58 +01:00
RomSets = new ObservableCollection<RomSetModel>(romSets);
2020-08-22 03:13:49 +01:00
}
2020-08-30 03:00:14 +01:00
public ObservableCollection<RomSetModel> RomSets { get; }
public string RomSetLabel => Localization.RomSets;
public string RomSetNameLabel => Localization.RomSetNameLabel;
public string RomSetVersionLabel => Localization.RomSetVersionLabel;
public string RomSetAuthorLabel => Localization.RomSetAuthorLabel;
2020-09-05 01:52:43 +01:00
public string RomSetCategoryLabel => Localization.RomSetCategoryLabel;
2020-08-30 03:00:14 +01:00
public string RomSetDateLabel => Localization.RomSetDateLabel;
public string RomSetDescriptionLabel => Localization.RomSetDescriptionLabel;
public string RomSetCommentLabel => Localization.RomSetCommentLabel;
public string RomSetTotalMachinesLabel => Localization.RomSetTotalMachinesLabel;
public string RomSetCompleteMachinesLabel => Localization.RomSetCompleteMachinesLabel;
public string RomSetIncompleteMachinesLabel => Localization.RomSetIncompleteMachinesLabel;
public string RomSetTotalRomsLabel => Localization.RomSetTotalRomsLabel;
public string RomSetHaveRomsLabel => Localization.RomSetHaveRomsLabel;
public string RomSetMissRomsLabel => Localization.RomSetMissRomsLabel;
2020-08-30 14:22:41 +01:00
public bool IsVfsAvailable => Vfs.IsAvailable;
2020-08-30 03:00:14 +01:00
public string FileMenuText => Localization.FileMenuText;
public string FileMenuImportDatFileText => Localization.FileMenuImportDatFileText;
public string FileMenuImportDatFolderText => Localization.FileMenuImportDatFolderText;
public string FileMenuSettingsText => Localization.FileMenuSettingsText;
public string FileMenuExitText => Localization.FileMenuExitText;
public string FilesystemMenuText => Localization.FilesystemMenuText;
public string FilesystemMenuMountText => Localization.FilesystemMenuMountText;
public string RomsMenuText => Localization.RomsMenuText;
public string RomsMenuImportText => Localization.RomsMenuImportText;
public string RomSetsMenuText => Localization.RomSetsMenuText;
public string RomSetsMenuSaveRomsText => Localization.RomSetsMenuSaveRomsText;
public string RomSetsMenuSaveDatText => Localization.RomSetsMenuSaveDatText;
public string RomSetsMenuEditText => Localization.RomSetsMenuEditText;
public string RomSetsMenuDeleteText => Localization.RomSetsMenuDeleteText;
public string HelpMenuText => Localization.HelpMenuText;
public string HelpMenuAboutText => Localization.HelpMenuAboutText;
2020-09-01 11:54:16 +01:00
public string FilesystemMenuUmountText => Localization.FilesystemMenuUmountText;
public string DatabaseMenuText => Localization.DatabaseMenuText;
public string DatabaseMenuUpdateStatsText => Localization.DatabaseMenuUpdateStatsText;
2020-08-22 03:26:43 +01:00
2020-08-22 03:13:49 +01:00
public bool NativeMenuSupported =>
NativeMenu.GetIsNativeMenuExported((Application.Current.ApplicationLifetime as
IClassicDesktopStyleApplicationLifetime)?.MainWindow);
2020-08-22 19:28:58 +01:00
public ReactiveCommand<Unit, Unit> AboutCommand { get; }
public ReactiveCommand<Unit, Unit> ExitCommand { get; }
public ReactiveCommand<Unit, Unit> SettingsCommand { get; }
public ReactiveCommand<Unit, Unit> ImportDatCommand { get; }
public ReactiveCommand<Unit, Unit> ImportDatFolderCommand { get; }
2020-08-22 21:57:05 +01:00
public ReactiveCommand<Unit, Unit> ImportRomFolderCommand { get; }
2020-08-24 02:29:07 +01:00
public ReactiveCommand<Unit, Unit> DeleteRomSetCommand { get; }
public ReactiveCommand<Unit, Unit> EditRomSetCommand { get; }
2020-08-24 04:01:55 +01:00
public ReactiveCommand<Unit, Unit> ExportDatCommand { get; }
public ReactiveCommand<Unit, Unit> ExportRomsCommand { get; }
public ReactiveCommand<Unit, Unit> MountCommand { get; }
2020-09-01 11:54:16 +01:00
public ReactiveCommand<Unit, Unit> UmountCommand { get; }
public ReactiveCommand<Unit, Unit> UpdateStatsCommand { get; }
2020-09-01 11:54:16 +01:00
public Vfs Vfs
{
get => _vfs;
set => this.RaiseAndSetIfChanged(ref _vfs, value);
}
2020-08-24 02:29:07 +01:00
public RomSetModel SelectedRomSet
{
get => _selectedRomSet;
set => this.RaiseAndSetIfChanged(ref _selectedRomSet, value);
}
2020-08-22 03:13:49 +01:00
internal async void ExecuteSettingsCommand()
{
2020-08-22 21:19:34 +01:00
var dialog = new SettingsDialog();
dialog.DataContext = new SettingsViewModel(dialog);
await dialog.ShowDialog(_view);
2020-08-22 03:13:49 +01:00
}
internal void ExecuteExitCommand() =>
(Application.Current.ApplicationLifetime as ClassicDesktopStyleApplicationLifetime)?.Shutdown();
internal void ExecuteAboutCommand()
{
var dialog = new About();
dialog.DataContext = new AboutViewModel(dialog);
dialog.ShowDialog(_view);
}
2020-08-22 04:40:39 +01:00
2020-08-30 03:00:14 +01:00
async void ExecuteImportDatCommand()
2020-08-22 04:40:39 +01:00
{
var dlgOpen = new OpenFileDialog
{
AllowMultiple = false,
2020-08-30 03:00:14 +01:00
Title = Localization.ImportDatFileDialogTitle
};
2020-08-22 04:40:39 +01:00
dlgOpen.Filters.Add(new FileDialogFilter
{
Extensions = new List<string>
{
2020-09-03 23:39:06 +01:00
"dat",
"xml"
2020-08-22 04:40:39 +01:00
},
2020-08-30 03:00:14 +01:00
Name = Localization.DatFilesDialogLabel
2020-08-22 04:40:39 +01:00
});
dlgOpen.Filters.Add(new FileDialogFilter
{
Extensions = new List<string>
{
2020-09-03 23:39:06 +01:00
"*"
2020-08-22 04:40:39 +01:00
},
2020-08-30 03:00:14 +01:00
Name = Localization.AllFilesDialogLabel
2020-08-22 04:40:39 +01:00
});
string[] result = await dlgOpen.ShowAsync(_view);
if(result?.Length != 1)
return;
2020-08-22 13:36:39 +01:00
var dialog = new ImportDat();
var importDatViewModel = new ImportDatViewModel(dialog, result[0]);
importDatViewModel.RomSetAdded += ImportDatViewModelOnRomSetAdded;
dialog.DataContext = importDatViewModel;
2020-08-22 04:40:39 +01:00
await dialog.ShowDialog(_view);
}
2020-08-30 03:00:14 +01:00
void ImportDatViewModelOnRomSetAdded(object sender, RomSetEventArgs e) =>
Dispatcher.UIThread.Post(() => RomSets.Add(e.RomSet));
2020-08-22 19:28:58 +01:00
2020-08-30 03:00:14 +01:00
async void ExecuteImportDatFolderCommand()
2020-08-22 19:28:58 +01:00
{
var dlgOpen = new OpenFolderDialog
{
2020-08-30 03:00:14 +01:00
Title = Localization.ImportDatFolderDialogTitle
2020-08-22 19:28:58 +01:00
};
string result = await dlgOpen.ShowAsync(_view);
if(result == null)
return;
var dialog = new ImportDatFolder();
var importDatFolderViewModel = new ImportDatFolderViewModel(dialog, result);
importDatFolderViewModel.RomSetAdded += ImportDatViewModelOnRomSetAdded;
dialog.DataContext = importDatFolderViewModel;
await dialog.ShowDialog(_view);
}
2020-08-22 21:57:05 +01:00
2020-08-30 03:00:14 +01:00
async void ExecuteImportRomFolderCommand()
2020-08-22 21:57:05 +01:00
{
var dlgOpen = new OpenFolderDialog
{
2020-08-30 03:00:14 +01:00
Title = Localization.ImportRomsFolderDialogTitle
2020-08-22 21:57:05 +01:00
};
string result = await dlgOpen.ShowAsync(_view);
if(result == null)
return;
var dialog = new ImportRomFolder();
var importRomFolderViewModel = new ImportRomFolderViewModel(dialog, result);
dialog.DataContext = importRomFolderViewModel;
await dialog.ShowDialog(_view);
}
2020-08-24 02:29:07 +01:00
async void ExecuteDeleteRomSetCommand()
{
if(SelectedRomSet == null)
return;
2020-08-30 03:00:14 +01:00
ButtonResult result = await MessageBoxManager.
GetMessageBoxStandardWindow(Localization.DeleteRomSetMsgBoxTitle,
string.
Format(Localization.DeleteRomSetMsgBoxCaption,
SelectedRomSet.Name), ButtonEnum.YesNo,
Icon.Database).ShowDialog(_view);
2020-08-24 02:29:07 +01:00
if(result == ButtonResult.No)
return;
var dialog = new RemoveDat();
var removeDatViewModel = new RemoveDatViewModel(dialog, SelectedRomSet.Id);
dialog.DataContext = removeDatViewModel;
await dialog.ShowDialog(_view);
RomSets.Remove(SelectedRomSet);
SelectedRomSet = null;
}
void ExecuteEditRomSetCommand()
{
2020-08-24 04:01:55 +01:00
if(SelectedRomSet == null)
return;
var window = new EditDat();
var viewModel = new EditDatViewModel(window, SelectedRomSet);
viewModel.RomSetModified += (sender, args) =>
{
RomSetModel old = RomSets.FirstOrDefault(r => r.Id == args.RomSet.Id);
if(old == null)
return;
RomSets.Remove(old);
RomSets.Add(args.RomSet);
};
window.DataContext = viewModel;
window.Show();
}
2020-08-24 04:01:55 +01:00
async void ExecuteExportDatCommand()
{
if(SelectedRomSet == null)
return;
var dlgSave = new SaveFileDialog
{
InitialFileName = SelectedRomSet.Filename
};
string result = await dlgSave.ShowAsync(_view);
if(result == null)
return;
var dialog = new ExportDat();
var viewModel = new ExportDatViewModel(dialog, SelectedRomSet.Sha384, result);
dialog.DataContext = viewModel;
await dialog.ShowDialog(_view);
}
async void ExecuteExportRomsCommand()
{
var dlgOpen = new OpenFolderDialog
{
2020-08-30 03:00:14 +01:00
Title = Localization.ExportRomsDialogTitle
};
string result = await dlgOpen.ShowAsync(_view);
if(result == null)
return;
var dialog = new ExportRoms();
var viewModel = new ExportRomsViewModel(dialog, result, SelectedRomSet.Id);
dialog.DataContext = viewModel;
await dialog.ShowDialog(_view);
}
async void ExecuteMountCommand()
{
2020-09-01 11:54:16 +01:00
if(Vfs != null)
return;
var dlgOpen = new OpenFolderDialog
{
2020-08-30 03:00:14 +01:00
Title = Localization.SelectMountPointDialogTitle
};
string result = await dlgOpen.ShowAsync(_view);
if(result == null)
return;
2020-09-01 11:54:16 +01:00
try
{
2020-09-01 11:54:16 +01:00
Vfs = new Vfs();
Vfs.Umounted += VfsOnUmounted;
Vfs.MountTo(result);
}
catch(Exception)
{
if(Debugger.IsAttached)
throw;
2020-09-01 11:54:16 +01:00
Vfs = null;
}
}
2020-09-01 11:54:16 +01:00
void VfsOnUmounted(object sender, EventArgs e) => Vfs = null;
void ExecuteUmountCommand() => Vfs?.Umount();
async void ExecuteUpdateStatsCommand()
{
ButtonResult result = await MessageBoxManager.
GetMessageBoxStandardWindow(Localization.DatabaseMenuUpdateStatsText,
Localization.UpdateStatsConfirmationDialogText,
ButtonEnum.YesNo, Icon.Database).ShowDialog(_view);
if(result == ButtonResult.No)
return;
var view = new UpdateStats();
var viewModel = new UpdateStatsViewModel(view);
view.DataContext = viewModel;
await view.ShowDialog(_view);
}
2020-08-21 22:22:24 +01:00
}
}