Files
2026-10-11 22:19:34 +02:00

167 lines
5.1 KiB
C#

using cfs_tracker.Services;
using cfs_tracker.ViewModels;
using System.Diagnostics;
namespace cfs_tracker;
public partial class App : Application
{
private readonly IServiceProvider _serviceProvider;
private readonly AppBootstrapService _bootstrapService;
private readonly LocalizationService _localizationService;
private bool _startupTriggered;
public App(
IServiceProvider serviceProvider,
AppBootstrapService bootstrapService,
LocalizationService localizationService)
{
_serviceProvider = serviceProvider;
_bootstrapService = bootstrapService;
_localizationService = localizationService;
_localizationService.Initialize();
InitializeComponent();
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<AppShell>();
await MainThread.InvokeOnMainThreadAsync(() =>
{
window.Page = shell;
});
var navigator = _serviceProvider.GetRequiredService<NotificationNavigationService>();
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 = LocalizationService.Translate("App_Loading_Title"),
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 = LocalizationService.Translate("App_Loading_Message"),
FontSize = 20,
HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.FromArgb("#DCE7F3")
}
}
}
}
}
};
}
private static Page CreateErrorPage(Exception ex)
{
return new ContentPage
{
Title = LocalizationService.Translate("App_Error_Title"),
BackgroundColor = Color.FromArgb("#0B1A2A"),
Content = new ScrollView
{
Content = new VerticalStackLayout
{
Padding = 24,
Spacing = 16,
Children =
{
new Label
{
Text = LocalizationService.Translate("App_Error_Header"),
FontSize = 24,
FontAttributes = FontAttributes.Bold,
TextColor = Color.FromArgb("#FFB4B4")
},
new Label
{
Text = ex.ToString(),
FontSize = 14,
TextColor = Color.FromArgb("#DCE7F3")
}
}
}
}
};
}
public async Task ReloadShellAsync(string route = "//options", OptionsViewModel.OptionsTab? optionsTab = null)
{
var shell = _serviceProvider.GetRequiredService<AppShell>();
await MainThread.InvokeOnMainThreadAsync(async () =>
{
var window = Windows.FirstOrDefault();
if (window is null)
return;
window.Page = shell;
if (!string.IsNullOrWhiteSpace(route))
await shell.GoToAsync(route);
if (optionsTab.HasValue)
{
await Task.Delay(50);
if (shell.CurrentPage is Views.OptionsPage optionsPage)
optionsPage.ViewModel.SelectedTab = optionsTab.Value;
}
});
}
}