Files
romrepomgr/RomRepoMgr/ViewModels/SplashWindowViewModel.cs

304 lines
10 KiB
C#
Raw Permalink Normal View History

2020-08-22 02:17:40 +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 02:17:40 +01:00
*******************************************************************************/
2020-08-22 02:23:51 +01:00
using System;
2020-08-22 02:39:44 +01:00
using System.IO;
2020-08-22 13:32:43 +01:00
using System.Linq;
2020-08-22 02:23:51 +01:00
using System.Threading.Tasks;
2025-07-24 11:11:27 +01:00
using System.Windows.Input;
2020-08-22 02:17:40 +01:00
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
2020-08-22 02:23:51 +01:00
using Avalonia.Threading;
2025-07-24 11:11:27 +01:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
2020-08-22 02:39:44 +01:00
using Microsoft.EntityFrameworkCore;
2020-08-22 13:36:39 +01:00
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Models;
2020-08-23 21:21:55 +01:00
using RomRepoMgr.Core.Workers;
2020-08-22 02:39:44 +01:00
using RomRepoMgr.Database;
2025-07-24 16:20:22 +01:00
using Serilog;
using Serilog.Extensions.Logging;
2020-08-22 02:17:40 +01:00
2024-11-09 01:37:59 +00:00
namespace RomRepoMgr.ViewModels;
2025-07-24 11:11:27 +01:00
public sealed partial class SplashWindowViewModel : ViewModelBase
2020-08-22 02:17:40 +01:00
{
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _checkingUnArError;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _checkingUnArOk;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _checkingUnArUnknown;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _exitVisible;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _loadingDatabaseError;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _loadingDatabaseOk;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _loadingDatabaseUnknown;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _loadingRomSetsError;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _loadingRomSetsOk;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _loadingRomSetsUnknown;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _loadingSettingsError;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _loadingSettingsOk;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _loadingSettingsUnknown;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _migratingDatabaseError;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _migratingDatabaseOk;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
2024-11-09 01:37:59 +00:00
bool _migratingDatabaseUnknown;
public SplashWindowViewModel()
2020-08-22 02:17:40 +01:00
{
2025-07-24 11:11:27 +01:00
ExitCommand = new RelayCommand(ExecuteExitCommand);
2024-11-09 01:37:59 +00:00
LoadingSettingsOk = false;
LoadingSettingsError = false;
LoadingSettingsUnknown = true;
CheckingUnArOk = false;
CheckingUnArError = false;
CheckingUnArUnknown = true;
LoadingDatabaseOk = false;
LoadingDatabaseError = false;
LoadingDatabaseUnknown = true;
MigratingDatabaseOk = false;
MigratingDatabaseError = false;
MigratingDatabaseUnknown = true;
LoadingRomSetsOk = false;
LoadingRomSetsError = false;
LoadingRomSetsUnknown = true;
ExitVisible = false;
}
2020-08-22 02:17:40 +01:00
2025-07-24 11:11:27 +01:00
public ICommand ExitCommand { get; }
2024-11-09 01:37:59 +00:00
public string LoadingText => "ROM Repository Manager";
2020-08-22 02:17:40 +01:00
2024-11-09 01:37:59 +00:00
void ExecuteExitCommand() =>
(Application.Current.ApplicationLifetime as ClassicDesktopStyleApplicationLifetime)?.Shutdown();
internal void OnOpened() => Dispatcher.UIThread.Post(LoadSettings);
void LoadSettings()
2024-11-09 01:37:59 +00:00
{
_ = Task.Run(() =>
2020-08-22 02:17:40 +01:00
{
try
{
Settings.Settings.LoadSettings();
2020-08-22 02:17:40 +01:00
Dispatcher.UIThread.Post(CheckUnAr);
}
catch(Exception e)
{
2025-07-24 16:20:22 +01:00
Log.Error(e, "Error loading settings");
Dispatcher.UIThread.Post(FailedLoadingSettings);
}
});
}
2020-08-22 13:32:43 +01:00
2024-11-09 01:37:59 +00:00
void FailedLoadingSettings()
{
LoadingSettingsUnknown = false;
LoadingSettingsError = true;
ExitVisible = true;
}
void CheckUnAr()
2024-11-09 01:37:59 +00:00
{
_ = Task.Run(() =>
2020-08-22 13:32:43 +01:00
{
LoadingSettingsUnknown = false;
LoadingSettingsOk = true;
2020-08-22 13:32:43 +01:00
try
{
var worker = new Compression();
Settings.Settings.CanDecompress = worker.CheckUnAr(Settings.Settings.Current.UnArchiverPath) ||
Settings.Settings.Current.UseInternalDecompressor;
Dispatcher.UIThread.Post(LoadDatabase);
}
catch(Exception e)
{
2025-07-24 16:20:22 +01:00
Log.Error(e, "Error checking unar");
Dispatcher.UIThread.Post(FailedCheckUnAr);
}
});
}
2020-08-22 13:32:43 +01:00
2024-11-09 01:37:59 +00:00
void FailedCheckUnAr()
{
CheckingUnArUnknown = false;
CheckingUnArError = true;
ExitVisible = true;
}
2020-08-22 02:17:40 +01:00
2024-11-09 01:37:59 +00:00
void LoadDatabase()
{
CheckingUnArUnknown = false;
CheckingUnArOk = true;
2020-08-22 02:23:51 +01:00
_ = Task.Run(() =>
2020-08-22 02:23:51 +01:00
{
try
{
2024-11-09 01:37:59 +00:00
string dbPathFolder = Path.GetDirectoryName(Settings.Settings.Current.DatabasePath);
if(!Directory.Exists(dbPathFolder)) Directory.CreateDirectory(dbPathFolder);
2020-08-22 13:32:43 +01:00
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath,
new SerilogLoggerFactory(Log.Logger));
2024-11-09 01:37:59 +00:00
Dispatcher.UIThread.Post(MigrateDatabase);
2020-08-22 02:23:51 +01:00
}
catch(Exception e)
{
2025-07-24 16:20:22 +01:00
Log.Error(e, "Error loading database");
2024-11-09 01:37:59 +00:00
Dispatcher.UIThread.Post(FailedLoadingDatabase);
2020-08-22 02:23:51 +01:00
}
});
2024-11-09 01:37:59 +00:00
}
2020-08-22 02:23:51 +01:00
2024-11-09 01:37:59 +00:00
void FailedLoadingDatabase()
{
LoadingDatabaseUnknown = false;
LoadingDatabaseError = true;
ExitVisible = true;
}
2020-08-22 02:23:51 +01:00
2024-11-09 01:37:59 +00:00
void MigrateDatabase()
{
LoadingDatabaseUnknown = false;
LoadingDatabaseOk = true;
2020-08-22 02:39:44 +01:00
_ = Task.Run(() =>
2024-11-09 01:37:59 +00:00
{
2020-08-23 21:21:55 +01:00
try
{
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath,
new SerilogLoggerFactory(Log.Logger));
2024-11-09 01:37:59 +00:00
ctx.Database.Migrate();
2020-08-23 21:21:55 +01:00
2024-11-09 01:37:59 +00:00
Dispatcher.UIThread.Post(LoadRomSets);
2020-08-23 21:21:55 +01:00
}
catch(Exception e)
{
2025-07-24 16:20:22 +01:00
Log.Error(e, "Error migrating database");
2024-11-09 01:37:59 +00:00
Dispatcher.UIThread.Post(FailedMigratingDatabase);
2020-08-23 21:21:55 +01:00
}
});
2024-11-09 01:37:59 +00:00
}
2020-08-23 21:21:55 +01:00
2024-11-09 01:37:59 +00:00
void FailedMigratingDatabase()
{
MigratingDatabaseUnknown = false;
MigratingDatabaseError = true;
ExitVisible = true;
}
2020-08-22 02:39:44 +01:00
2024-11-09 01:37:59 +00:00
void LoadRomSets()
{
MigratingDatabaseUnknown = false;
MigratingDatabaseOk = true;
2020-08-22 02:39:44 +01:00
_ = Task.Run(() =>
2020-08-22 02:39:44 +01:00
{
2024-11-09 01:37:59 +00:00
try
2020-08-22 02:39:44 +01:00
{
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath,
new SerilogLoggerFactory(Log.Logger));
2024-11-09 01:37:59 +00:00
GotRomSets?.Invoke(this,
new RomSetsEventArgs
{
RomSets = ctx.RomSets.OrderBy(r => r.Name)
.ThenBy(r => r.Version)
.ThenBy(r => r.Date)
.ThenBy(r => r.Description)
.ThenBy(r => r.Comment)
.ThenBy(r => r.Filename)
.Select(r => new RomSetModel
{
Id = r.Id,
Author = r.Author,
Comment = r.Comment,
Date = r.Date,
Description = r.Description,
Filename = r.Filename,
Homepage = r.Homepage,
Name = r.Name,
Sha384 = r.Sha384,
Version = r.Version,
TotalMachines = r.Statistics.TotalMachines,
CompleteMachines = r.Statistics.CompleteMachines,
IncompleteMachines = r.Statistics.IncompleteMachines,
TotalRoms = r.Statistics.TotalRoms,
HaveRoms = r.Statistics.HaveRoms,
MissRoms = r.Statistics.MissRoms,
Category = r.Category
})
.ToList()
});
Dispatcher.UIThread.Post(LoadMainWindow);
}
catch(Exception e)
2020-08-22 13:32:43 +01:00
{
2025-07-24 16:20:22 +01:00
Log.Error(e, "Error loading ROM sets");
2024-11-09 01:37:59 +00:00
Dispatcher.UIThread.Post(FailedLoadingRomSets);
}
});
}
2020-08-22 13:32:43 +01:00
2024-11-09 01:37:59 +00:00
void FailedLoadingRomSets()
{
LoadingRomSetsUnknown = false;
LoadingRomSetsError = true;
ExitVisible = true;
}
2020-08-22 13:32:43 +01:00
2024-11-09 01:37:59 +00:00
void LoadMainWindow()
{
LoadingRomSetsUnknown = false;
LoadingRomSetsOk = true;
2020-08-22 13:32:43 +01:00
2024-11-09 01:37:59 +00:00
WorkFinished?.Invoke(this, EventArgs.Empty);
}
2020-08-22 02:39:44 +01:00
2024-11-09 01:37:59 +00:00
internal event EventHandler WorkFinished;
2020-08-22 13:32:43 +01:00
2024-11-09 01:37:59 +00:00
internal event EventHandler<RomSetsEventArgs> GotRomSets;
2020-08-22 02:17:40 +01:00
}