Files
romrepomgr/RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs

290 lines
9.7 KiB
C#
Raw Normal View History

2020-08-22 21:57:05 +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-22 21:57:05 +01:00
*******************************************************************************/
using System;
using System.Collections.ObjectModel;
using System.Reactive;
2020-08-23 20:10:38 +01:00
using System.Threading.Tasks;
2020-08-22 21:57:05 +01:00
using Avalonia.Threading;
using ReactiveUI;
using RomRepoMgr.Core.EventArgs;
2020-08-23 22:17:49 +01:00
using RomRepoMgr.Core.Models;
2020-08-23 20:10:38 +01:00
using RomRepoMgr.Core.Workers;
2020-08-30 03:00:14 +01:00
using RomRepoMgr.Resources;
2020-08-22 21:57:05 +01:00
using RomRepoMgr.Views;
2024-11-09 01:37:59 +00:00
namespace RomRepoMgr.ViewModels;
public sealed class ImportRomFolderViewModel : ViewModelBase
2020-08-22 21:57:05 +01:00
{
2024-11-09 01:37:59 +00:00
readonly ImportRomFolder _view;
bool _canClose;
bool _canStart;
bool _isImporting;
bool _isReady;
bool _knownOnlyChecked;
bool _progress2IsIndeterminate;
double _progress2Maximum;
double _progress2Minimum;
double _progress2Value;
bool _progress2Visible;
bool _progressIsIndeterminate;
double _progressMaximum;
double _progressMinimum;
double _progressValue;
bool _progressVisible;
bool _recurseArchivesChecked;
bool _removeFilesChecked;
bool _removeFilesEnabled;
string _status2Message;
string _statusMessage;
// Mock
public ImportRomFolderViewModel()
{
#pragma warning disable PH2080
FolderPath = "C:\\ROMs";
#pragma warning restore PH2080
}
2024-11-09 01:37:59 +00:00
public ImportRomFolderViewModel(ImportRomFolder view, string folderPath)
2020-08-22 21:57:05 +01:00
{
2024-11-09 01:37:59 +00:00
_view = view;
FolderPath = folderPath;
_removeFilesChecked = false;
_knownOnlyChecked = true;
_recurseArchivesChecked = Settings.Settings.UnArUsable;
2024-11-12 06:39:06 +00:00
ImportResults = [];
2024-11-09 01:37:59 +00:00
CloseCommand = ReactiveCommand.Create(ExecuteCloseCommand);
StartCommand = ReactiveCommand.Create(ExecuteStartCommand);
IsReady = true;
CanStart = true;
CanClose = true;
_removeFilesEnabled = false;
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public string FolderPath { get; }
public bool RecurseArchivesEnabled => Settings.Settings.UnArUsable;
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool RemoveFilesChecked
{
get => _removeFilesChecked;
set => this.RaiseAndSetIfChanged(ref _removeFilesChecked, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool KnownOnlyChecked
{
get => _knownOnlyChecked;
set => this.RaiseAndSetIfChanged(ref _knownOnlyChecked, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool RemoveFilesEnabled
{
get => _removeFilesEnabled;
set => this.RaiseAndSetIfChanged(ref _removeFilesEnabled, value);
}
2020-08-23 20:10:38 +01:00
2024-11-09 01:37:59 +00:00
public bool RecurseArchivesChecked
{
get => _recurseArchivesChecked;
set
2020-08-22 21:57:05 +01:00
{
2024-11-09 01:37:59 +00:00
if(value) RemoveFilesChecked = false;
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
RemoveFilesEnabled = !value;
this.RaiseAndSetIfChanged(ref _recurseArchivesChecked, value);
2020-08-22 21:57:05 +01:00
}
2024-11-09 01:37:59 +00:00
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool IsReady
{
get => _isReady;
set => this.RaiseAndSetIfChanged(ref _isReady, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool ProgressVisible
{
get => _progressVisible;
set => this.RaiseAndSetIfChanged(ref _progressVisible, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public string StatusMessage
{
get => _statusMessage;
set => this.RaiseAndSetIfChanged(ref _statusMessage, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public double ProgressMinimum
{
get => _progressMinimum;
set => this.RaiseAndSetIfChanged(ref _progressMinimum, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public double ProgressMaximum
{
get => _progressMaximum;
set => this.RaiseAndSetIfChanged(ref _progressMaximum, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public double ProgressValue
{
get => _progressValue;
set => this.RaiseAndSetIfChanged(ref _progressValue, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool ProgressIsIndeterminate
{
get => _progressIsIndeterminate;
set => this.RaiseAndSetIfChanged(ref _progressIsIndeterminate, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool Progress2Visible
{
get => _progress2Visible;
set => this.RaiseAndSetIfChanged(ref _progress2Visible, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public string Status2Message
{
get => _status2Message;
set => this.RaiseAndSetIfChanged(ref _status2Message, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public double Progress2Minimum
{
get => _progress2Minimum;
set => this.RaiseAndSetIfChanged(ref _progress2Minimum, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public double Progress2Maximum
{
get => _progress2Maximum;
set => this.RaiseAndSetIfChanged(ref _progress2Maximum, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public double Progress2Value
{
get => _progress2Value;
set => this.RaiseAndSetIfChanged(ref _progress2Value, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool Progress2IsIndeterminate
{
get => _progress2IsIndeterminate;
set => this.RaiseAndSetIfChanged(ref _progress2IsIndeterminate, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool IsImporting
{
get => _isImporting;
set => this.RaiseAndSetIfChanged(ref _isImporting, value);
}
2020-08-22 21:57:05 +01:00
public ObservableCollection<ImportRomItem> ImportResults { get; }
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool CanClose
{
get => _canClose;
set => this.RaiseAndSetIfChanged(ref _canClose, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public bool CanStart
{
get => _canStart;
set => this.RaiseAndSetIfChanged(ref _canStart, value);
}
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
public ReactiveCommand<Unit, Unit> CloseCommand { get; }
public ReactiveCommand<Unit, Unit> StartCommand { get; }
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
void ExecuteCloseCommand() => _view.Close();
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
void ExecuteStartCommand()
{
IsReady = false;
ProgressVisible = true;
IsImporting = true;
CanStart = false;
CanClose = false;
Progress2Visible = true;
var worker = new FileImporter(KnownOnlyChecked, RemoveFilesChecked);
worker.SetIndeterminateProgress += OnWorkerOnSetIndeterminateProgress;
worker.SetMessage += OnWorkerOnSetMessage;
worker.SetProgress += OnWorkerOnSetProgress;
worker.SetProgressBounds += OnWorkerOnSetProgressBounds;
worker.SetIndeterminateProgress2 += OnWorkerOnSetIndeterminateProgress2;
worker.SetMessage2 += OnWorkerOnSetMessage2;
worker.SetProgress2 += OnWorkerOnSetProgress2;
worker.SetProgressBounds2 += OnWorkerOnSetProgressBounds2;
worker.Finished += OnWorkerOnFinished;
worker.ImportedRom += OnWorkerOnImportedRom;
_ = Task.Run(() => worker.ProcessPath(FolderPath, true, RecurseArchivesChecked));
2024-11-09 01:37:59 +00:00
}
2020-08-23 22:17:49 +01:00
2024-11-09 01:37:59 +00:00
void OnWorkerOnImportedRom(object sender, ImportedRomItemEventArgs args) =>
Dispatcher.UIThread.Post(() => ImportResults.Add(args.Item));
2020-08-23 22:17:49 +01:00
2024-11-09 01:37:59 +00:00
void OnWorkerOnFinished(object sender, EventArgs args) => Dispatcher.UIThread.Post(() =>
{
ProgressVisible = false;
StatusMessage = Localization.Finished;
CanClose = true;
Progress2Visible = false;
});
2020-08-23 22:17:49 +01:00
2024-11-09 01:37:59 +00:00
void OnWorkerOnSetProgressBounds(object sender, ProgressBoundsEventArgs args) => Dispatcher.UIThread.Post(() =>
{
ProgressIsIndeterminate = false;
ProgressMaximum = args.Maximum;
ProgressMinimum = args.Minimum;
});
2020-08-23 22:17:49 +01:00
2024-11-09 01:37:59 +00:00
void OnWorkerOnSetProgress(object sender, ProgressEventArgs args) =>
Dispatcher.UIThread.Post(() => ProgressValue = args.Value);
2020-08-23 22:17:49 +01:00
2024-11-09 01:37:59 +00:00
void OnWorkerOnSetMessage(object sender, MessageEventArgs args) =>
Dispatcher.UIThread.Post(() => StatusMessage = args.Message);
2020-08-23 22:17:49 +01:00
2024-11-09 01:37:59 +00:00
void OnWorkerOnSetIndeterminateProgress(object sender, EventArgs args) =>
Dispatcher.UIThread.Post(() => ProgressIsIndeterminate = true);
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
void OnWorkerOnSetProgressBounds2(object sender, ProgressBoundsEventArgs args) => Dispatcher.UIThread.Post(() =>
{
Progress2IsIndeterminate = false;
Progress2Maximum = args.Maximum;
Progress2Minimum = args.Minimum;
});
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
void OnWorkerOnSetProgress2(object sender, ProgressEventArgs args) =>
Dispatcher.UIThread.Post(() => Progress2Value = args.Value);
2020-08-22 21:57:05 +01:00
2024-11-09 01:37:59 +00:00
void OnWorkerOnSetMessage2(object sender, MessageEventArgs args) =>
Dispatcher.UIThread.Post(() => Status2Message = args.Message);
void OnWorkerOnSetIndeterminateProgress2(object sender, EventArgs args) =>
Dispatcher.UIThread.Post(() => Progress2IsIndeterminate = true);
2020-08-22 21:57:05 +01:00
}