mirror of
https://github.com/claunia/romrepomgr.git
synced 2025-12-16 19:24:51 +00:00
Load database in splash.
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
// Copyright © 2020 Natalia Portillo
|
// Copyright © 2020 Natalia Portillo
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
|
using System;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using RomRepoMgr.Database.Models;
|
using RomRepoMgr.Database.Models;
|
||||||
|
|
||||||
@@ -30,8 +31,27 @@ namespace RomRepoMgr.Database
|
|||||||
{
|
{
|
||||||
public sealed class Context : DbContext
|
public sealed class Context : DbContext
|
||||||
{
|
{
|
||||||
|
static Context _singleton;
|
||||||
|
|
||||||
public Context(DbContextOptions options) : base(options) {}
|
public Context(DbContextOptions options) : base(options) {}
|
||||||
|
|
||||||
|
public static Context Singleton
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if(_singleton != null)
|
||||||
|
return _singleton;
|
||||||
|
|
||||||
|
if(Settings.Settings.Current?.DatabasePath is null)
|
||||||
|
throw new ArgumentNullException(nameof(Settings.Settings.Current.DatabasePath),
|
||||||
|
"Settings are not initialized!");
|
||||||
|
|
||||||
|
_singleton = Create(Settings.Settings.Current.DatabasePath);
|
||||||
|
|
||||||
|
return _singleton;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public DbSet<DbFile> Files { get; set; }
|
public DbSet<DbFile> Files { get; set; }
|
||||||
|
|
||||||
public static Context Create(string dbPath)
|
public static Context Create(string dbPath)
|
||||||
|
|||||||
@@ -18,4 +18,8 @@
|
|||||||
<Folder Include="Migrations" />
|
<Folder Include="Migrations" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\RomRepoMgr.Settings\RomRepoMgr.Settings.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,4 +1,31 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
// 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
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
using System;
|
||||||
using Avalonia;
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.ApplicationLifetimes;
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
using Avalonia.Markup.Xaml;
|
using Avalonia.Markup.Xaml;
|
||||||
using RomRepoMgr.ViewModels;
|
using RomRepoMgr.ViewModels;
|
||||||
@@ -13,12 +40,35 @@ namespace RomRepoMgr
|
|||||||
public override void OnFrameworkInitializationCompleted()
|
public override void OnFrameworkInitializationCompleted()
|
||||||
{
|
{
|
||||||
if(ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
if(ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||||
desktop.MainWindow = new SplashWindow
|
{
|
||||||
{
|
var splashWindow = new SplashWindow();
|
||||||
DataContext = new SplashWindowViewModel()
|
var swvm = new SplashWindowViewModel();
|
||||||
};
|
swvm.WorkFinished += OnSplashFinished;
|
||||||
|
splashWindow.DataContext = swvm;
|
||||||
|
desktop.MainWindow = splashWindow;
|
||||||
|
}
|
||||||
|
|
||||||
base.OnFrameworkInitializationCompleted();
|
base.OnFrameworkInitializationCompleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OnSplashFinished(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if(!(ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Ensure not exit
|
||||||
|
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
|
||||||
|
|
||||||
|
// Close splash window
|
||||||
|
desktop.MainWindow.Close();
|
||||||
|
|
||||||
|
// Create and show main window
|
||||||
|
desktop.MainWindow = new MainWindow();
|
||||||
|
desktop.MainWindow.DataContext = new MainWindowViewModel();
|
||||||
|
desktop.MainWindow.Show();
|
||||||
|
|
||||||
|
// Now can close when all windows are closed
|
||||||
|
desktop.ShutdownMode = ShutdownMode.OnLastWindowClose;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,7 @@
|
|||||||
<PackageReference Include="Svg.Skia.Avalonia" Version="0.10.0-preview3" />
|
<PackageReference Include="Svg.Skia.Avalonia" Version="0.10.0-preview3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\RomRepoMgr.Database\RomRepoMgr.Database.csproj" />
|
||||||
<ProjectReference Include="..\RomRepoMgr.Settings\RomRepoMgr.Settings.csproj" />
|
<ProjectReference Include="..\RomRepoMgr.Settings\RomRepoMgr.Settings.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -24,12 +24,15 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Reactive;
|
using System.Reactive;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Avalonia;
|
using Avalonia;
|
||||||
using Avalonia.Controls.ApplicationLifetimes;
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using ReactiveUI;
|
using ReactiveUI;
|
||||||
|
using RomRepoMgr.Database;
|
||||||
|
|
||||||
namespace RomRepoMgr.ViewModels
|
namespace RomRepoMgr.ViewModels
|
||||||
{
|
{
|
||||||
@@ -201,6 +204,71 @@ namespace RomRepoMgr.ViewModels
|
|||||||
{
|
{
|
||||||
LoadingSettingsUnknown = false;
|
LoadingSettingsUnknown = false;
|
||||||
LoadingSettingsOk = true;
|
LoadingSettingsOk = true;
|
||||||
|
|
||||||
|
Task.Run(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string dbPathFolder = Path.GetDirectoryName(Settings.Settings.Current.DatabasePath);
|
||||||
|
|
||||||
|
if(!Directory.Exists(dbPathFolder))
|
||||||
|
Directory.CreateDirectory(dbPathFolder);
|
||||||
|
|
||||||
|
_ = Context.Singleton;
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
// TODO: Log error
|
||||||
|
Dispatcher.UIThread.Post(FailedLoadingDatabase);
|
||||||
|
}
|
||||||
|
|
||||||
|
Dispatcher.UIThread.Post(MigrateDatabase);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FailedLoadingDatabase()
|
||||||
|
{
|
||||||
|
LoadingDatabaseUnknown = false;
|
||||||
|
LoadingDatabaseError = true;
|
||||||
|
ExitVisible = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MigrateDatabase()
|
||||||
|
{
|
||||||
|
LoadingDatabaseUnknown = false;
|
||||||
|
LoadingDatabaseOk = true;
|
||||||
|
|
||||||
|
Task.Run(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Context.Singleton.Database.Migrate();
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
// TODO: Log error
|
||||||
|
Dispatcher.UIThread.Post(FailedMigratingDatabase);
|
||||||
|
}
|
||||||
|
|
||||||
|
Dispatcher.UIThread.Post(LoadMainWindow);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void FailedMigratingDatabase()
|
||||||
|
{
|
||||||
|
MigratingDatabaseUnknown = false;
|
||||||
|
MigratingDatabaseError = true;
|
||||||
|
ExitVisible = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadMainWindow()
|
||||||
|
{
|
||||||
|
MigratingDatabaseUnknown = false;
|
||||||
|
MigratingDatabaseOk = true;
|
||||||
|
|
||||||
|
WorkFinished?.Invoke(this, EventArgs.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal event EventHandler WorkFinished;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user