diff --git a/Aaru.Gui/App.xaml.cs b/Aaru.Gui/App.xaml.cs index 8eaa8d4c9..cacf26e44 100644 --- a/Aaru.Gui/App.xaml.cs +++ b/Aaru.Gui/App.xaml.cs @@ -1,6 +1,8 @@ -using Aaru.Gui.ViewModels; +using System; +using Aaru.Gui.ViewModels; using Aaru.Gui.Views; using Avalonia; +using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; @@ -14,13 +16,39 @@ namespace Aaru.Gui { if(ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { + var swvm = new SplashWindowViewModel(); + swvm.WorkFinished += OnSplashFinished; + desktop.MainWindow = new SplashWindow { - DataContext = new SplashWindowViewModel() + DataContext = swvm }; } 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 + { + 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/Aaru.Gui/ViewModels/MainWindowViewModel.cs b/Aaru.Gui/ViewModels/MainWindowViewModel.cs index 60c82c2df..fc94ec010 100644 --- a/Aaru.Gui/ViewModels/MainWindowViewModel.cs +++ b/Aaru.Gui/ViewModels/MainWindowViewModel.cs @@ -2,6 +2,6 @@ { public class MainWindowViewModel : ViewModelBase { - public string Greeting => "Welcome to Avalonia!"; + public string Greeting => "Welcome to Aaru!"; } -} +} \ No newline at end of file diff --git a/Aaru.Gui/ViewModels/SplashWindowViewModel.cs b/Aaru.Gui/ViewModels/SplashWindowViewModel.cs index b81de68e4..c79ac6fca 100644 --- a/Aaru.Gui/ViewModels/SplashWindowViewModel.cs +++ b/Aaru.Gui/ViewModels/SplashWindowViewModel.cs @@ -1,4 +1,14 @@ -using Avalonia.Threading; +using System; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Aaru.Console; +using Aaru.Core; +using Aaru.Database; +using Aaru.Settings; +using Avalonia.Threading; +using Microsoft.EntityFrameworkCore; using ReactiveUI; namespace Aaru.Gui.ViewModels @@ -30,7 +40,7 @@ namespace Aaru.Gui.ViewModels internal void OnOpened() { Message = "Welcome to Aaru!"; - MaxProgress = 2; + MaxProgress = 9; CurrentProgress = 0; Dispatcher.UIThread.Post(InitializeConsole); @@ -40,6 +50,147 @@ namespace Aaru.Gui.ViewModels { CurrentProgress++; Message = "Initializing console..."; + + Task.Run(() => + { + ConsoleHandler.Init(); + + Dispatcher.UIThread.Post(LoadSettings); + }); } + + void LoadSettings() + { + CurrentProgress++; + Message = "Loading settings..."; + + Task.Run(() => + { + // TODO: Detect there are no settings yet + Settings.Settings.LoadSettings(); + + Dispatcher.UIThread.Post(MigrateLocalDatabase); + }); + } + + void MigrateLocalDatabase() + { + CurrentProgress++; + Message = "Migrating local database..."; + + Task.Run(() => + { + var ctx = AaruContext.Create(Settings.Settings.LocalDbPath); + ctx.Database.Migrate(); + ctx.SaveChanges(); + + Dispatcher.UIThread.Post(UpdateMasterDatabase); + }); + } + + void UpdateMasterDatabase() + { + CurrentProgress++; + Message = "Updating master database..."; + + Task.Run(() => + { + bool masterDbUpdate = false; + + if(!File.Exists(Settings.Settings.MasterDbPath)) + { + masterDbUpdate = true; + + // TODO: Update database + } + + var masterContext = AaruContext.Create(Settings.Settings.MasterDbPath); + + if(masterContext.Database.GetPendingMigrations().Any()) + { + AaruConsole.WriteLine("New database version, updating..."); + + try + { + File.Delete(Settings.Settings.MasterDbPath); + } + catch(Exception) + { + AaruConsole. + ErrorWriteLine("Exception trying to remove old database version, cannot continue..."); + + AaruConsole.ErrorWriteLine("Please manually remove file at {0}", + Settings.Settings.MasterDbPath); + } + + // TODO: Update database + } + + Dispatcher.UIThread.Post(CheckGdprCompliance); + }); + } + + void CheckGdprCompliance() + { + CurrentProgress++; + Message = "Checking GDPR compliance..."; + + Task.Run(() => + { + // TODO: Settings window + if(Settings.Settings.Current.GdprCompliance < DicSettings.GdprLevel) + AaruConsole.ErrorWriteLine("Settings window not yet implemented"); + + Dispatcher.UIThread.Post(LoadStatistics); + }); + } + + void LoadStatistics() + { + CurrentProgress++; + Message = "Loading statistics..."; + + Task.Run(() => + { + Statistics.LoadStats(); + + Dispatcher.UIThread.Post(RegisterEncodings); + }); + } + + void RegisterEncodings() + { + CurrentProgress++; + Message = "Registering encodings..."; + + Task.Run(() => + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + Dispatcher.UIThread.Post(SaveStatistics); + }); + } + + void SaveStatistics() + { + CurrentProgress++; + Message = "Saving statistics..."; + + Task.Run(() => + { + Statistics.SaveStats(); + + Dispatcher.UIThread.Post(LoadMainWindow); + }); + } + + void LoadMainWindow() + { + CurrentProgress++; + Message = "Loading main window..."; + WorkFinished?.Invoke(this, EventArgs.Empty); + } + + internal event EventHandler WorkFinished; } } \ No newline at end of file