Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MathGame.ViewModels;
- using System;
- using System.Threading;
- using CommunityToolkit.Mvvm.ComponentModel;
- using System.Threading.Tasks;
- using MathGame.Services;
- using System.Collections.Generic;
- using System.IO;
- namespace SplashScreen.ViewModels;
- public partial class SplashViewModel : ViewModelBase
- {
- [ObservableProperty]
- private string _startupMessage = "Starting Application. . .Please Wait. . .";
- [ObservableProperty]
- private double _progress = 0;
- private CancellationTokenSource _cts = new CancellationTokenSource();
- public CancellationToken cancellationToken => _cts.Token;
- public void Cancel()
- {
- _cts.Cancel();
- }
- /* Loading Screen Control Flow
- *
- * - Check for application config.json file, required file for application, call function to create if not exists
- * - Check for application database file, required file for application, call function to create if not exists
- * - If neither of those files exists, launch create new user screen (handled in MainWindowViewModel).
- *
- */
- public async Task<bool> LoadApplication()
- {
- var timer = new FunctionTimer();
- double elapsedTime = 0;
- int sleep = 500;
- try
- {
- // Give the application's system a moment to catch up
- StartupMessage = "Starting Application. . .Please Wait. . .";
- await Task.Delay(sleep, cancellationToken);
- StartupMessage = "Checking For Application Config File. . .Please Wait. . .";
- timer.Start();
- bool configExists = await FileCheckService.FileExistsAsync("config.json");
- if (!configExists)
- {
- var configResults = JsonService.CheckConfig();
- if (!configResults.IsSuccess)
- {
- timer.Stop();
- StartupMessage = $"Error: {configResults.Message}";
- await Task.Delay(sleep, cancellationToken);
- Environment.Exit(0);
- return false;
- }
- }
- StartupMessage = "Application Config File Found. . .Please Wait. . .";
- timer.Stop();
- elapsedTime = timer.GetElapsedTimeInSeconds();
- UpdateProgress(elapsedTime);
- timer.Clear();
- StartupMessage = "Checking For Application Database. . .Please Wait. . .";
- timer.Start();
- bool databaseExists = await FileCheckService.FileExistsAsync("main.db");
- if (!databaseExists)
- {
- var dbResults = Database.CreateDatabase();
- if (!dbResults.IsSuccess)
- {
- timer.Stop();
- StartupMessage = $"Error: {dbResults.Message}";
- await Task.Delay(500, cancellationToken);
- Environment.Exit(0);
- return false;
- }
- }
- StartupMessage = "Application Database Found. . .Please Wait. . .";
- timer.Stop();
- elapsedTime = timer.GetElapsedTimeInSeconds();
- UpdateProgress(elapsedTime);
- timer.Clear();
- return true;
- }
- catch (TaskCanceledException)
- {
- Environment.Exit(0);
- return false;
- }
- }
- private void UpdateProgress(double progress)
- {
- Progress = Math.Min(progress, 1.0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment