diff --git a/RomRepoMgr.Database/Context.cs b/RomRepoMgr.Database/Context.cs index 0852a12..d3848fd 100644 --- a/RomRepoMgr.Database/Context.cs +++ b/RomRepoMgr.Database/Context.cs @@ -23,6 +23,7 @@ // Copyright © 2020 Natalia Portillo *******************************************************************************/ +using System; using Microsoft.EntityFrameworkCore; using RomRepoMgr.Database.Models; @@ -30,8 +31,27 @@ namespace RomRepoMgr.Database { public sealed class Context : DbContext { + static Context _singleton; + 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 Files { get; set; } public static Context Create(string dbPath) diff --git a/RomRepoMgr.Database/RomRepoMgr.Database.csproj b/RomRepoMgr.Database/RomRepoMgr.Database.csproj index f205303..214db0c 100644 --- a/RomRepoMgr.Database/RomRepoMgr.Database.csproj +++ b/RomRepoMgr.Database/RomRepoMgr.Database.csproj @@ -18,4 +18,8 @@ + + + + diff --git a/RomRepoMgr/App.xaml.cs b/RomRepoMgr/App.xaml.cs index b2ea4fb..14a7810 100644 --- a/RomRepoMgr/App.xaml.cs +++ b/RomRepoMgr/App.xaml.cs @@ -1,4 +1,31 @@ +/****************************************************************************** +// RomRepoMgr - ROM repository manager +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2020 Natalia Portillo +*******************************************************************************/ + +using System; using Avalonia; +using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; using RomRepoMgr.ViewModels; @@ -13,12 +40,35 @@ namespace RomRepoMgr public override void OnFrameworkInitializationCompleted() { if(ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) - desktop.MainWindow = new SplashWindow - { - DataContext = new SplashWindowViewModel() - }; + { + var splashWindow = new SplashWindow(); + var swvm = new SplashWindowViewModel(); + swvm.WorkFinished += OnSplashFinished; + splashWindow.DataContext = swvm; + desktop.MainWindow = splashWindow; + } 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; + } } } \ No newline at end of file diff --git a/RomRepoMgr/RomRepoMgr.csproj b/RomRepoMgr/RomRepoMgr.csproj index b6a6623..dee2f2e 100644 --- a/RomRepoMgr/RomRepoMgr.csproj +++ b/RomRepoMgr/RomRepoMgr.csproj @@ -20,6 +20,7 @@ + diff --git a/RomRepoMgr/ViewModels/SplashWindowViewModel.cs b/RomRepoMgr/ViewModels/SplashWindowViewModel.cs index 2b06847..aa3f51f 100644 --- a/RomRepoMgr/ViewModels/SplashWindowViewModel.cs +++ b/RomRepoMgr/ViewModels/SplashWindowViewModel.cs @@ -24,12 +24,15 @@ *******************************************************************************/ using System; +using System.IO; using System.Reactive; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Threading; +using Microsoft.EntityFrameworkCore; using ReactiveUI; +using RomRepoMgr.Database; namespace RomRepoMgr.ViewModels { @@ -201,6 +204,71 @@ namespace RomRepoMgr.ViewModels { LoadingSettingsUnknown = false; 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; } } \ No newline at end of file