using System; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Data.Core.Plugins; using Avalonia.Markup.Xaml; using Avalonia.Threading; using BetterRaid.Services; using BetterRaid.Services.Implementations; using BetterRaid.ViewModels; using BetterRaid.Views; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace BetterRaid; public class App : Application { private ServiceProvider? _serviceProvider; private ILogger? _logger; public override void Initialize() { _serviceProvider = InitializeServices(); _logger = _serviceProvider.GetRequiredService>(); if (TryLoadDatabase() == false) { _logger?.LogError("Failed to load or initialize database"); Environment.Exit(1); } AvaloniaXamlLoader.Load(_serviceProvider, this); } private bool TryLoadDatabase() { if (_serviceProvider == null) { throw new FieldAccessException($"\"{nameof(_serviceProvider)}\" was null"); } var db = _serviceProvider.GetRequiredService(); try { db.LoadOrCreate(); Task.Run(db.UpdateLoadedChannels); return true; } catch (Exception e) { _logger?.LogError(e, "Failed to load database"); return false; } } private ServiceProvider InitializeServices() { var services = new ServiceCollection(); services.AddLogging(logging => { logging.SetMinimumLevel(LogLevel.Debug); logging.AddConsole(); }); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(_ => new DispatcherService(Dispatcher.UIThread)); services.AddTransient(); return services.BuildServiceProvider(); } public override void OnFrameworkInitializationCompleted() { BindingPlugins.DataValidators.RemoveAt(0); if(_serviceProvider == null) { throw new FieldAccessException($"\"{nameof(_serviceProvider)}\" was null"); } var mainWindow = new MainWindow { DataContext = _serviceProvider.GetRequiredService() }; switch (ApplicationLifetime) { case IClassicDesktopStyleApplicationLifetime desktop: desktop.MainWindow = mainWindow; desktop.Exit += OnDesktopOnExit; break; case ISingleViewApplicationLifetime singleViewPlatform: singleViewPlatform.MainView = mainWindow; break; } base.OnFrameworkInitializationCompleted(); } private void OnDesktopOnExit(object? o, ControlledApplicationLifetimeExitEventArgs controlledApplicationLifetimeExitEventArgs) { if (_serviceProvider == null) { throw new FieldAccessException($"\"{nameof(_serviceProvider)}\" was null"); } try { var db = _serviceProvider.GetRequiredService(); db.Save(); } catch (Exception e) { _logger?.LogError(e, "Failed to save database"); } } }