Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading.Tasks;
- using Avalonia;
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.Markup.Xaml;
- using MathGame.ViewModels;
- using MathGame.Views;
- using MathGame.Services;
- using SplashScreen.ViewModels;
- using SplashScreen;
- namespace MathGame;
- public partial class App : Application
- {
- public override void Initialize()
- {
- AvaloniaXamlLoader.Load(this);
- }
- public override async void OnFrameworkInitializationCompleted()
- {
- if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
- {
- var splashWindow = new Splash();
- var splashViewModel = new SplashViewModel();
- splashWindow.DataContext = splashViewModel;
- desktop.MainWindow = splashWindow;
- // do a tad bit of OOP by setting the delays before the try/catch block
- // so that we can use the variable where needed and have oen place to edit the values
- int delay = 500;
- int errorDelay = 3000;
- try
- {
- // give the system a moment to catch up (I have a slow computer)
- await Task.Delay(errorDelay, cancellationToken: splashViewModel.cancellationToken);
- // display application starting
- splashViewModel.StartupMessage = "Starting Application. . .Please Wait. . .";
- await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
- // display checking for config file
- splashViewModel.StartupMessage = "Checking For Config File. . .Please Wait. . .";
- // make a call to the json service to see if the config file exists
- // the json service will handle the creation of the config file if it doesn't exist
- var configFound = JsonService.CheckConfig();
- // check if the config file was found and successful
- if (!configFound.IsSuccess)
- {
- // if not, display the error message and close the program
- splashViewModel.StartupMessage = configFound.Message + ". . .Closing Program. . .";
- await Task.Delay(errorDelay, cancellationToken: splashViewModel.cancellationToken);
- Environment.Exit(0);
- }
- // if so, display success message and continue with the application
- splashViewModel.StartupMessage = configFound.Message + ". . .Please Wait. . .";
- await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
- // now we're going to check if we can open the file and obtain the information
- splashViewModel.StartupMessage = "Obtaining Config Information. . .Please Wait. . .";
- // set the call equal to a variable
- var configInfo = JsonService.GetAppInfo();
- // check if the call was successful
- if (!configInfo.IsSuccess)
- {
- // if not, display the error message and close the program
- splashViewModel.StartupMessage = $"{configInfo.Message}. . .Closing Program. . .";
- await Task.Delay(errorDelay, cancellationToken: splashViewModel.cancellationToken);
- Environment.Exit(0);
- }
- // if so, display success message and continue with the application
- splashViewModel.StartupMessage = "Config Information Gathered Successfully. . .Please Wait. . .";
- await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
- // display checking if the database exists
- // for now, we have no use for a database so we'll just leave this alone
- splashViewModel.StartupMessage = "Gathering Project Solutions and Workspaces. . .Please Wait. . .";
- await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
- /* Until we have a reason to create more things on application startup,
- * we will just feed remaining foder information to the screen
- */
- splashViewModel.StartupMessage = "Applying Application Styles. . .Please Wait. . .";
- await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
- splashViewModel.StartupMessage = "Launching Application. . .Please Wait. . .";
- await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
- }
- catch (TaskCanceledException)
- {
- splashWindow.Close();
- Environment.Exit(0);
- return;
- }
- var mainWindow = new MainWindow();
- var mainWindowViewModel = new MainWindowViewModel();
- mainWindow.DataContext = mainWindowViewModel;
- desktop.MainWindow = mainWindow;
- mainWindow.Show();
- splashWindow.Close();
- }
- base.OnFrameworkInitializationCompleted();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment