Refactor LoginPage to MVVM

This commit is contained in:
2025-12-17 17:25:42 +01:00
parent bb5aac2944
commit c11b361655
11 changed files with 442 additions and 134 deletions

View File

@@ -1,5 +1,6 @@
using Jugenddienst_Stunden.Models;
using Jugenddienst_Stunden.Types;
using Jugenddienst_Stunden.ViewModels;
using ZXing.Net.Maui;
@@ -19,9 +20,38 @@ public partial class LoginPage : ContentPage {
public LoginPage() {
InitializeComponent();
// BindingContext via DI beziehen, falls nicht bereits gesetzt
try {
if (BindingContext is null) {
var sp = Application.Current?.Handler?.MauiContext?.Services
?? throw new InvalidOperationException("DI container ist nicht verfügbar.");
BindingContext = sp.GetRequiredService<LoginViewModel>();
}
} catch (Exception) {
// Ignorieren: Fallback bleibt leerer BindingContext
}
if (BindingContext is LoginViewModel vm) {
vm.AlertEvent += async (_, msg) => await DisplayAlert("Fehler:", msg, "OK");
vm.InfoEvent += async (_, msg) => await DisplayAlert("Information:", msg, "OK");
}
barcodeScannerView.Options =
new BarcodeReaderOptions { Formats = BarcodeFormat.QrCode, AutoRotate = true, Multiple = false };
// Fallback-Verkabelung: Falls das EventToCommandBehavior in XAML nicht greift,
// leiten wir das Kamera-Event manuell an das ViewModel-Command weiter.
barcodeScannerView.BarcodesDetected += (s, e) => {
if (BindingContext is LoginViewModel vm && vm.QrDetectedCommand is not null) {
// Sicherstellen, dass die Command-Ausführung im UI-Thread erfolgt
MainThread.BeginInvokeOnMainThread(async () => {
if (vm.QrDetectedCommand.CanExecute(e)) {
await vm.QrDetectedCommand.ExecuteAsync(e);
}
});
}
};
//if (BindingContext is LoginViewModel vm) {
// vm.AlertEvent += Vm_AlertEvent;
// vm.InfoEvent += Vm_InfoEvent;
@@ -29,20 +59,7 @@ public partial class LoginPage : ContentPage {
//}
//Zwischen manuellem und automatischem Login (mit QR-Code) umschalten und die Schalterstellung merken
bool sqr = true;
bool sma = false;
if (Preferences.Default.Get("logintype", "") == "manual") {
sqr = false;
sma = true;
LoginSwitch.IsToggled = true;
Message.IsVisible = false;
} else {
LoginSwitch.IsToggled = false;
Message.IsVisible = true;
}
LoginQR.IsVisible = sqr;
LoginManual.IsVisible = sma;
// MVVM übernimmt Umschalten über IsManualMode im ViewModel; keine Code-Behind-Umschaltung mehr
}
@@ -102,13 +119,13 @@ public partial class LoginPage : ContentPage {
base.OnDisappearing();
barcodeScannerView.CameraLocation = CameraLocation.Front;
barcodeScannerView.IsDetecting = false;
// IsDetecting wird via Binding vom ViewModel gesteuert
}
protected override void OnAppearing() {
base.OnAppearing();
barcodeScannerView.IsDetecting = true;
// IsDetecting wird via Binding vom ViewModel gesteuert
barcodeScannerView.CameraLocation = CameraLocation.Rear;
}
@@ -175,21 +192,7 @@ public partial class LoginPage : ContentPage {
}
//Zwischen manuellem und automatischem Login (mit QR-Code) umschalten und die Schalterstellung merken
private void Switch_Toggled(object sender, ToggledEventArgs e) {
var switcher = (Switch)sender;
if (switcher.IsToggled) {
LoginQR.IsVisible = false;
LoginManual.IsVisible = true;
Message.IsVisible = false;
Preferences.Default.Set("logintype", "manual");
} else {
LoginQR.IsVisible = true;
LoginManual.IsVisible = false;
Message.IsVisible = true;
Preferences.Default.Set("logintype", "qr");
}
}
// Umschalt-Logik erfolgt über Binding an IsManualMode im ViewModel
//private void Vm_AlertEvent(object? sender, string e) {
// DisplayAlert("Fehler:", e, "OK");