using cfs_tracker.Services; using System.Diagnostics; namespace cfs_tracker; public partial class App : Application { private readonly IServiceProvider _serviceProvider; private readonly AppBootstrapService _bootstrapService; private bool _startupTriggered; public App(IServiceProvider serviceProvider, AppBootstrapService bootstrapService) { InitializeComponent(); _serviceProvider = serviceProvider; _bootstrapService = bootstrapService; UserAppTheme = AppTheme.Dark; } protected override Window CreateWindow(IActivationState? activationState) { var window = new Window(CreateLoadingPage()); if (!_startupTriggered) { _startupTriggered = true; _ = InitializeAppAsync(window); } return window; } private async Task InitializeAppAsync(Window window) { try { await _bootstrapService.InitializeAsync(); var shell = _serviceProvider.GetRequiredService(); await MainThread.InvokeOnMainThreadAsync(() => { window.Page = shell; }); var navigator = _serviceProvider.GetRequiredService(); await navigator.FlushAsync(); } catch (Exception ex) { Debug.WriteLine($"App bootstrap failed: {ex}"); await MainThread.InvokeOnMainThreadAsync(() => { window.Page = CreateErrorPage(ex); }); } } private static Page CreateLoadingPage() { return new ContentPage { Title = "Laden", BackgroundColor = Color.FromArgb("#0B1A2A"), Content = new Grid { Padding = 24, Children = { new VerticalStackLayout { Spacing = 16, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center, Children = { new ActivityIndicator { IsRunning = true, WidthRequest = 48, HeightRequest = 48, Color = Color.FromArgb("#DCE7F3") }, new Label { Text = "App wird geladen ...", FontSize = 20, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.FromArgb("#DCE7F3") } } } } } }; } private static Page CreateErrorPage(Exception ex) { return new ContentPage { Title = "Startfehler", BackgroundColor = Color.FromArgb("#0B1A2A"), Content = new ScrollView { Content = new VerticalStackLayout { Padding = 24, Spacing = 16, Children = { new Label { Text = "Fehler beim Starten der App", FontSize = 24, FontAttributes = FontAttributes.Bold, TextColor = Color.FromArgb("#FFB4B4") }, new Label { Text = ex.ToString(), FontSize = 14, TextColor = Color.FromArgb("#DCE7F3") } } } } }; } }