Implement splash screen.

This commit is contained in:
2020-04-09 04:18:29 +01:00
parent 87b3ac704c
commit 8c1ebaec86
3 changed files with 185 additions and 6 deletions

View File

@@ -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;
}
}
}

View File

@@ -2,6 +2,6 @@
{
public class MainWindowViewModel : ViewModelBase
{
public string Greeting => "Welcome to Avalonia!";
public string Greeting => "Welcome to Aaru!";
}
}

View File

@@ -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;
}
}