Refactor Api-Client
Add Exceptionhandler, AlertService JSON-Converter AppSettings via DI Reformat Code
This commit is contained in:
@@ -3,8 +3,11 @@ 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;
|
||||
|
||||
@@ -12,39 +15,84 @@ namespace Jugenddienst_Stunden;
|
||||
/// 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();
|
||||
|
||||
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",
|
||||
// "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");
|
||||
// }
|
||||
|
||||
#if DEBUG
|
||||
if (GlobalVar.ApiKey == null) {
|
||||
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
|
||||
// builder.Logging.AddDebug();
|
||||
//#endif
|
||||
|
||||
// DI: Services & Repositories
|
||||
builder.Services.AddSingleton<IHoursRepository, HoursRepository>();
|
||||
builder.Services.AddSingleton<IHoursService, HoursService>();
|
||||
// DI: AlertService für globale Alerts (z. B. leere ApiUrl)
|
||||
builder.Services.AddSingleton<IAlertService, AlertService>();
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
// 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
|
||||
builder.Services.AddSingleton<HttpClient>(_ => new HttpClient());
|
||||
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>();
|
||||
|
||||
// DI: Views/ViewModels
|
||||
builder.Services.AddTransient<ViewModels.StundenViewModel>();
|
||||
builder.Services.AddTransient<Views.StundenPage>();
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user