124 lines
5.0 KiB
C#
124 lines
5.0 KiB
C#
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;
|
|
using Jugenddienst_Stunden.ViewModels;
|
|
using System.Net;
|
|
|
|
namespace Jugenddienst_Stunden;
|
|
|
|
/// <summary>
|
|
/// Das Hauptprogramm.
|
|
/// </summary>
|
|
public static class MauiProgram {
|
|
public static MauiApp CreateMauiApp() {
|
|
var builder = MauiApp.CreateBuilder();
|
|
builder
|
|
.UseMauiApp<App>()
|
|
// 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",
|
|
// "MTQxfHNkdFptQkNZTXlPT3ZyMHxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk=");
|
|
// 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
|
|
|
|
// ApiClient registrieren: SocketsHttpHandler als Primary Handler (vermeidet AndroidMessageHandler-Castfehler)
|
|
var apiOptions = new Infrastructure.ApiOptions { BaseUrl = GlobalVar.ApiUrl, Timeout = TimeSpan.FromSeconds(15) };
|
|
builder.Services.AddApiHttpClient(apiOptions);
|
|
|
|
// DI: AlertService für globale Alerts (z. B. leere ApiUrl)
|
|
builder.Services.AddSingleton<IAlertService, AlertService>();
|
|
|
|
// DI: Settings aus Preferences (Single Source of Truth bleibt Preferences)
|
|
builder.Services.AddSingleton<IAppSettings, PreferencesAppSettings>();
|
|
|
|
// DI: ApiOptions IMMER aus aktuellen Settings erzeugen (nicht beim Start einfrieren)
|
|
builder.Services.AddTransient(sp => new ApiOptions {
|
|
BaseUrl = sp.GetRequiredService<IAppSettings>().ApiUrl,
|
|
Timeout = TimeSpan.FromSeconds(15)
|
|
});
|
|
|
|
// Token Provider soll ebenfalls aus Settings/Preferences lesen
|
|
builder.Services.AddSingleton<ITokenProvider, SettingsTokenProvider>();
|
|
|
|
// HttpClient + ApiClient
|
|
// Configure HttpClient with SocketsHttpHandler (managed) and RequestLoggingHandler
|
|
builder.Services.AddTransient<RequestLoggingHandler>();
|
|
builder.Services.AddSingleton<HttpClient>(sp => {
|
|
var nativeHandler = new SocketsHttpHandler {
|
|
AllowAutoRedirect = false,
|
|
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
|
|
PooledConnectionLifetime = TimeSpan.FromMinutes(5),
|
|
ConnectTimeout = TimeSpan.FromSeconds(10)
|
|
};
|
|
var logging = sp.GetRequiredService<RequestLoggingHandler>();
|
|
logging.InnerHandler = nativeHandler;
|
|
// HttpClient.Timeout will be adjusted by ApiClient if needed
|
|
return new HttpClient(logging, disposeHandler: true);
|
|
});
|
|
|
|
builder.Services.AddSingleton<IApiClient>(sp => {
|
|
var alert = sp.GetRequiredService<IAlertService>();
|
|
try {
|
|
return new ApiClient(
|
|
sp.GetRequiredService<HttpClient>(),
|
|
sp.GetRequiredService<ApiOptions>(),
|
|
sp.GetRequiredService<ITokenProvider>(),
|
|
sp.GetRequiredService<IAppSettings>());
|
|
} 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<ITokenProvider, GlobalVarTokenProvider>();
|
|
//builder.Services.AddSingleton<HttpClient>(_ => new HttpClient());
|
|
//builder.Services.AddSingleton<IApiClient>(sp => new ApiClient(
|
|
// sp.GetRequiredService<HttpClient>(),
|
|
// sp.GetRequiredService<ApiOptions>(),
|
|
// sp.GetRequiredService<ITokenProvider>()));
|
|
|
|
// DI: Validatoren
|
|
builder.Services.AddSingleton<IHoursValidator, HoursValidator>();
|
|
|
|
// DI: Services & Repositories
|
|
builder.Services.AddSingleton<IHoursRepository, HoursRepository>();
|
|
builder.Services.AddSingleton<IHoursService, HoursService>();
|
|
builder.Services.AddSingleton<IAuthService, AuthService>();
|
|
|
|
// DI: Views/ViewModels
|
|
builder.Services.AddTransient<ViewModels.StundenViewModel>();
|
|
builder.Services.AddTransient<Views.StundenPage>();
|
|
builder.Services.AddTransient<ViewModels.StundeViewModel>();
|
|
builder.Services.AddTransient<Views.StundePage>();
|
|
builder.Services.AddTransient<ViewModels.LoginViewModel>();
|
|
builder.Services.AddTransient<Views.LoginPage>();
|
|
|
|
return builder.Build();
|
|
}
|
|
} |