mekasu0124

Untitled

Aug 7th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.17 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3.  
  4. using Avalonia;
  5. using Avalonia.Controls.ApplicationLifetimes;
  6. using Avalonia.Markup.Xaml;
  7.  
  8. using MathGame.ViewModels;
  9. using MathGame.Views;
  10. using MathGame.Services;
  11.  
  12. using SplashScreen.ViewModels;
  13. using SplashScreen;
  14.  
  15. namespace MathGame;
  16. public partial class App : Application
  17. {
  18.     public override void Initialize()
  19.     {
  20.         AvaloniaXamlLoader.Load(this);
  21.     }
  22.  
  23.     public override async void OnFrameworkInitializationCompleted()
  24.     {
  25.         if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  26.         {
  27.             var splashWindow = new Splash();
  28.             var splashViewModel = new SplashViewModel();
  29.  
  30.             splashWindow.DataContext = splashViewModel;
  31.             desktop.MainWindow = splashWindow;
  32.  
  33.             // do a tad bit of OOP by setting the delays before the try/catch block
  34.             // so that we can use the variable where needed and have oen place to edit the values
  35.             int delay = 500;
  36.             int errorDelay = 3000;
  37.  
  38.             try
  39.             {
  40.                 // give the system a moment to catch up (I have a slow computer)
  41.                 await Task.Delay(errorDelay, cancellationToken: splashViewModel.cancellationToken);
  42.  
  43.                 // display application starting
  44.                 splashViewModel.StartupMessage = "Starting Application. . .Please Wait. . .";
  45.                 await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
  46.  
  47.                 // display checking for config file
  48.                 splashViewModel.StartupMessage = "Checking For Config File. . .Please Wait. . .";
  49.  
  50.                 // make a call to the json service to see if the config file exists
  51.                 // the json service will handle the creation of the config file if it doesn't exist
  52.                 var configFound = JsonService.CheckConfig();
  53.  
  54.                 // check if the config file was found and successful
  55.                 if (!configFound.IsSuccess)
  56.                 {
  57.                     // if not, display the error message and close the program
  58.                     splashViewModel.StartupMessage = configFound.Message + ". . .Closing Program. . .";
  59.                     await Task.Delay(errorDelay, cancellationToken: splashViewModel.cancellationToken);
  60.                     Environment.Exit(0);
  61.                 }
  62.  
  63.                 // if so, display success message and continue with the application
  64.                 splashViewModel.StartupMessage = configFound.Message + ". . .Please Wait. . .";
  65.                 await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
  66.  
  67.                 // now we're going to check if we can open the file and obtain the information
  68.                 splashViewModel.StartupMessage = "Obtaining Config Information. . .Please Wait. . .";
  69.  
  70.                 // set the call equal to a variable
  71.                 var configInfo = JsonService.GetAppInfo();
  72.  
  73.                 // check if the call was successful
  74.                 if (!configInfo.IsSuccess)
  75.                 {
  76.                     // if not, display the error message and close the program
  77.                     splashViewModel.StartupMessage = $"{configInfo.Message}. . .Closing Program. . .";
  78.                     await Task.Delay(errorDelay, cancellationToken: splashViewModel.cancellationToken);
  79.                     Environment.Exit(0);
  80.                 }
  81.  
  82.                 // if so, display success message and continue with the application
  83.                 splashViewModel.StartupMessage = "Config Information Gathered Successfully. . .Please Wait. . .";
  84.                 await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
  85.  
  86.                 // display checking if the database exists
  87.                 // for now, we have no use for a database so we'll just leave this alone
  88.                 splashViewModel.StartupMessage = "Gathering Project Solutions and Workspaces. . .Please Wait. . .";
  89.                 await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
  90.  
  91.                 /* Until we have a reason to create more things on application startup,
  92.                  * we will just feed remaining foder information to the screen
  93.                  */
  94.  
  95.                 splashViewModel.StartupMessage = "Applying Application Styles. . .Please Wait. . .";
  96.                 await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
  97.  
  98.                 splashViewModel.StartupMessage = "Launching Application. . .Please Wait. . .";
  99.                 await Task.Delay(delay, cancellationToken: splashViewModel.cancellationToken);
  100.             }
  101.             catch (TaskCanceledException)
  102.             {
  103.                 splashWindow.Close();
  104.                 Environment.Exit(0);
  105.                 return;
  106.             }
  107.  
  108.             var mainWindow = new MainWindow();
  109.             var mainWindowViewModel = new MainWindowViewModel();
  110.  
  111.             mainWindow.DataContext = mainWindowViewModel;
  112.             desktop.MainWindow = mainWindow;
  113.  
  114.             mainWindow.Show();
  115.             splashWindow.Close();
  116.         }
  117.  
  118.         base.OnFrameworkInitializationCompleted();
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment