Refactor LoginViewModel to use IAppSettings; improve settings management and update dependency injection.

This commit is contained in:
2025-12-26 13:54:51 +01:00
parent 4d5b093ea0
commit e2ffc24131
3 changed files with 44 additions and 19 deletions

View File

@@ -11,6 +11,7 @@ namespace Jugenddienst_Stunden.ViewModels;
public partial class LoginViewModel : ObservableObject {
private readonly IAuthService _auth;
private readonly IAlertService? _alerts;
private readonly IAppSettings _settings;
private DateTime _lastDetectionTime = DateTime.MinValue;
private readonly TimeSpan _detectionInterval = TimeSpan.FromSeconds(5);
@@ -38,7 +39,7 @@ public partial class LoginViewModel : ObservableObject {
private string? serverLabel;
[ObservableProperty]
private string title = Preferences.Default.Get("name", "Nicht") + " " + Preferences.Default.Get("surname", "eingeloggt");
private string title = "Nicht eingeloggt";
[ObservableProperty]
private string? username;
@@ -58,8 +59,9 @@ public partial class LoginViewModel : ObservableObject {
// Explizite Command-Property für den QR-Scanner-Event, damit das Binding in XAML zuverlässig greift
public IAsyncRelayCommand<object?> QrDetectedCommand { get; }
public LoginViewModel(IAuthService auth) {
public LoginViewModel(IAuthService auth, IAppSettings settings) {
_auth = auth;
_settings = settings;
// gespeicherte Präferenz für Logintyp laden
var lt = Preferences.Default.Get("logintype", "qr");
@@ -68,22 +70,36 @@ public partial class LoginViewModel : ObservableObject {
IsDetecting = !isManualMode;
// Serveranzeige vorbereiten
var apiUrl = Preferences.Default.Get("apiUrl", string.Empty);
if (!string.IsNullOrWhiteSpace(apiUrl)) {
Server = apiUrl.Replace("/appapi", "").Replace("https://", "").Replace("http://", "");
ServerLabel = "Server: " + Server;
}
RefreshSettings();
// Command initialisieren
QrDetectedCommand = new AsyncRelayCommand<object?>(QrDetectedAsync);
}
// DI-Konstruktor: AlertService anbinden und Alerts an VM-Event weiterreichen (analog StundeViewModel)
internal LoginViewModel(IAuthService auth, IAlertService alertService) : this(auth) {
internal LoginViewModel(IAuthService auth, IAlertService alertService,IAppSettings settings) : this(auth,settings) {
_alerts = alertService;
_settings = settings;
if (alertService is not null) {
alertService.AlertRaised += (s, msg) => AlertEvent?.Invoke(this, msg);
}
RefreshSettings();
}
/// <summary>
/// Aktualisiert die Serveranzeige aus den aktuellen AppSettings.
/// </summary>
public void RefreshSettings() {
var apiUrl = _settings.ApiUrl;
if (!string.IsNullOrWhiteSpace(apiUrl)) {
Server = apiUrl.Replace("/appapi", "").Replace("https://", "").Replace("http://", "");
ServerLabel = "Server: " + Server;
} else {
Server = string.Empty;
ServerLabel = "Server: Nicht konfiguriert";
}
Title = $"{_settings.Name} {_settings.Surname}";
}
partial void OnIsManualModeChanged(bool value) {