mekasu0124

Untitled

Aug 7th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using MathGame.ViewModels;
  2.  
  3. using System;
  4. using System.Threading;
  5.  
  6. using CommunityToolkit.Mvvm.ComponentModel;
  7. using System.Threading.Tasks;
  8. using MathGame.Services;
  9. using System.Collections.Generic;
  10. using System.IO;
  11.  
  12. namespace SplashScreen.ViewModels;
  13.  
  14. public partial class SplashViewModel : ViewModelBase
  15. {
  16.     [ObservableProperty]
  17.     private string _startupMessage = "Starting Application. . .Please Wait. . .";
  18.  
  19.     [ObservableProperty]
  20.     private double _progress = 0;
  21.  
  22.     private CancellationTokenSource _cts = new CancellationTokenSource();
  23.     public CancellationToken cancellationToken => _cts.Token;
  24.  
  25.     public void Cancel()
  26.     {
  27.         _cts.Cancel();
  28.     }
  29.  
  30.     /* Loading Screen Control Flow
  31.      *
  32.      * - Check for application config.json file, required file for application, call function to create if not exists
  33.      * - Check for application database file, required file for application, call function to create if not exists
  34.      * - If neither of those files exists, launch create new user screen (handled in MainWindowViewModel).
  35.      *
  36.      */
  37.  
  38.     public async Task<bool> LoadApplication()
  39.     {
  40.         var timer = new FunctionTimer();
  41.         double elapsedTime = 0;
  42.  
  43.         int sleep = 500;
  44.  
  45.         try
  46.         {
  47.             // Give the application's system a moment to catch up
  48.             StartupMessage = "Starting Application. . .Please Wait. . .";
  49.             await Task.Delay(sleep, cancellationToken);
  50.  
  51.             StartupMessage = "Checking For Application Config File. . .Please Wait. . .";
  52.             timer.Start();
  53.  
  54.             bool configExists = await FileCheckService.FileExistsAsync("config.json");
  55.  
  56.             if (!configExists)
  57.             {
  58.                 var configResults = JsonService.CheckConfig();
  59.  
  60.                 if (!configResults.IsSuccess)
  61.                 {
  62.                     timer.Stop();
  63.                     StartupMessage = $"Error: {configResults.Message}";
  64.                     await Task.Delay(sleep, cancellationToken);
  65.                     Environment.Exit(0);
  66.                     return false;
  67.                 }
  68.             }
  69.  
  70.             StartupMessage = "Application Config File Found. . .Please Wait. . .";
  71.             timer.Stop();
  72.             elapsedTime = timer.GetElapsedTimeInSeconds();
  73.             UpdateProgress(elapsedTime);
  74.             timer.Clear();
  75.  
  76.             StartupMessage = "Checking For Application Database. . .Please Wait. . .";
  77.             timer.Start();
  78.  
  79.             bool databaseExists = await FileCheckService.FileExistsAsync("main.db");
  80.  
  81.             if (!databaseExists)
  82.             {
  83.                 var dbResults = Database.CreateDatabase();
  84.  
  85.                 if (!dbResults.IsSuccess)
  86.                 {
  87.                     timer.Stop();
  88.                     StartupMessage = $"Error: {dbResults.Message}";
  89.                     await Task.Delay(500, cancellationToken);
  90.                     Environment.Exit(0);
  91.                     return false;
  92.                 }
  93.             }
  94.  
  95.             StartupMessage = "Application Database Found. . .Please Wait. . .";
  96.             timer.Stop();
  97.             elapsedTime = timer.GetElapsedTimeInSeconds();
  98.             UpdateProgress(elapsedTime);
  99.             timer.Clear();
  100.             return true;
  101.         }
  102.         catch (TaskCanceledException)
  103.         {
  104.             Environment.Exit(0);
  105.             return false;
  106.         }
  107.     }
  108.  
  109.     private void UpdateProgress(double progress)
  110.     {
  111.         Progress = Math.Min(progress, 1.0);
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment