using CommunityToolkit.Maui; using Jugenddienst_Stunden.Models; using Jugenddienst_Stunden.Interfaces; using Jugenddienst_Stunden.Repositories; using Jugenddienst_Stunden.Services; using Jugenddienst_Stunden.Infrastructure; using Jugenddienst_Stunden.Validators; using Microsoft.Extensions.Logging; using ZXing.Net.Maui.Controls; using System.Net.Http; namespace Jugenddienst_Stunden; /// /// Das Hauptprogramm. /// public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp() // Initialize the .NET MAUI Community Toolkit by adding the below line of code .UseMauiCommunityToolkit(options => { options.SetShouldEnableSnackbarOnWindows(true); }) .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }) //.UseBarcodeScanning(); .UseBarcodeReader(); //#if DEBUG // if (string.IsNullOrWhiteSpace(GlobalVar.ApiKey)) { // GlobalVar.ApiKey = Preferences.Default.Get("apiKey", // "MTQxfHNkdFptQkNZTXlPT3ZyMHNBZDl0UnVxNExMRXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk="); // GlobalVar.Name = Preferences.Default.Get("name", "Testserver: Isabell"); // GlobalVar.Surname = Preferences.Default.Get("surname", "Biasi"); // GlobalVar.EmployeeId = Preferences.Default.Get("EmployeeId", 141); // GlobalVar.ApiUrl = Preferences.Default.Get("apiUrl", "https://hours.dauni.mine.nu/appapi"); // } // builder.Logging.AddDebug(); //#endif // DI: AlertService für globale Alerts (z. B. leere ApiUrl) builder.Services.AddSingleton(); // DI: Settings aus Preferences (Single Source of Truth bleibt Preferences) builder.Services.AddSingleton(); // DI: ApiOptions IMMER aus aktuellen Settings erzeugen (nicht beim Start einfrieren) builder.Services.AddTransient(sp => new ApiOptions { BaseUrl = sp.GetRequiredService().ApiUrl, Timeout = TimeSpan.FromSeconds(15) }); // Token Provider soll ebenfalls aus Settings/Preferences lesen builder.Services.AddSingleton(); // HttpClient + ApiClient builder.Services.AddSingleton(_ => new HttpClient()); builder.Services.AddSingleton(sp => { var alert = sp.GetRequiredService(); try { return new ApiClient( sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService()); } catch (Exception e) { // Alert an UI/VM weiterreichen alert.Raise(e.Message); // Fallback: NullApiClient liefert beim Aufruf aussagekräftige Exception return new NullApiClient(e.Message); } }); // DI: Infrastruktur //builder.Services.AddSingleton(new ApiOptions { BaseUrl = GlobalVar.ApiUrl, Timeout = TimeSpan.FromSeconds(15) }); //builder.Services.AddSingleton(); //builder.Services.AddSingleton(_ => new HttpClient()); //builder.Services.AddSingleton(sp => new ApiClient( // sp.GetRequiredService(), // sp.GetRequiredService(), // sp.GetRequiredService())); // DI: Validatoren builder.Services.AddSingleton(); // DI: Services & Repositories builder.Services.AddSingleton(); builder.Services.AddSingleton(); // DI: Views/ViewModels builder.Services.AddTransient(); builder.Services.AddTransient(); return builder.Build(); } }