Files
romrepomgr/RomRepoMgr/ViewModels/MainWindowViewModel.cs

156 lines
6.3 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2020 Natalia Portillo
*******************************************************************************/
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-08-22 03:13:49 +01:00
using System.Reactive;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Threading;
2020-08-22 03:13:49 +01:00
using ReactiveUI;
using RomRepoMgr.Core.EventArgs;
2020-08-22 13:36:39 +01:00
using RomRepoMgr.Core.Models;
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-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);
RomSets = new ObservableCollection<RomSetModel>(romSets);
2020-08-22 03:13:49 +01:00
}
2020-08-22 13:32:43 +01:00
public ObservableCollection<RomSetModel> RomSets { get; }
public string RomSetLabel => "ROM sets";
public string RomSetNameLabel => "Name";
public string RomSetVersionLabel => "Version";
public string RomSetAuthorLabel => "Author";
public string RomSetDateLabel => "Date";
public string RomSetDescriptionLabel => "Description";
public string RomSetCommentLabel => "Comment";
public string RomSetHomepageLabel => "Homepage";
2020-08-22 03:26:43 +01:00
2020-08-21 22:22:24 +01:00
public string Greeting => "Hello World!";
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 03:13:49 +01:00
internal async void ExecuteSettingsCommand()
{
/*var dialog = new SettingsDialog();
dialog.DataContext = new SettingsViewModel(dialog, false);
await dialog.ShowDialog(_view);*/
}
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
internal async void ExecuteImportDatCommand()
{
var dlgOpen = new OpenFileDialog
{
AllowMultiple = false,
Title = "Import DAT file..."
};
2020-08-22 04:40:39 +01:00
dlgOpen.Filters.Add(new FileDialogFilter
{
Extensions = new List<string>
{
"*.dat",
"*.xml"
},
Name = "DAT files"
});
dlgOpen.Filters.Add(new FileDialogFilter
{
Extensions = new List<string>
{
"*.*"
},
Name = "All files"
});
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);
}
void ImportDatViewModelOnRomSetAdded(object sender, RomSetEventArgs e) => Dispatcher.UIThread.Post(() =>
{
RomSets.Add(e.RomSet);
});
2020-08-22 19:28:58 +01:00
internal async void ExecuteImportDatFolderCommand()
{
var dlgOpen = new OpenFolderDialog
{
Title = "Import DATs from folder..."
};
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-21 22:22:24 +01:00
}
}