Comments etc.

This commit is contained in:
2024-10-13 12:28:41 +02:00
parent 15f1961ab2
commit c305bc63b7
6 changed files with 116 additions and 55 deletions

View File

@@ -1,3 +1,4 @@
using System;
using Jugenddienst_Stunden.Models;
using Jugenddienst_Stunden.ViewModels;
using Microsoft.Maui.Controls;
@@ -6,15 +7,26 @@ using ZXing.Net.Maui;
namespace Jugenddienst_Stunden.Views;
/// <summary>
/// Die Loginseite mit dem Barcodescanner
/// </summary>
public partial class LoginPage : ContentPage {
private DateTime _lastDetectionTime;
private readonly TimeSpan _detectionInterval = TimeSpan.FromSeconds(5);
private readonly LoginViewModel _loginViewModel = new LoginViewModel();
/// <summary>
/// CTOR
/// </summary>
public LoginPage() {
InitializeComponent();
//if (BindingContext is LoginViewModel vm) {
// vm.AlertEvent += Vm_AlertEvent;
// vm.InfoEvent += Vm_InfoEvent;
// vm.MsgEvent += Vm_MsgEvent;
//}
barcodeScannerView.Options = new ZXing.Net.Maui.BarcodeReaderOptions {
Formats = ZXing.Net.Maui.BarcodeFormat.QrCode,
AutoRotate = true,
@@ -22,40 +34,34 @@ public partial class LoginPage : ContentPage {
};
}
//public event EventHandler<string> AlertEvent;
private void BarcodesDetected(object sender, ZXing.Net.Maui.BarcodeDetectionEventArgs e) {
//var first = e.Results?.FirstOrDefault();
var currentTime = DateTime.Now;
if ((currentTime - _lastDetectionTime) > _detectionInterval) {
_lastDetectionTime = currentTime;
foreach (var barcode in e.Results) {
if (Stunde.apiKey != barcode.Value) {
MainThread.InvokeOnMainThreadAsync(async () => {
_ = MainThread.InvokeOnMainThreadAsync(async () => {
//await DisplayAlert("Barcode erkannt", $"Barcode: {barcode.Format} - {barcode.Value}", "OK");
try {
var tokendata = new TokenData(barcode.Value);
var op = await Models.Operator.LoadData(barcode.Value);
Models.Stunde.apiKey = barcode.Value;
Models.Stunde.name = op.name;
Models.Stunde.surname = op.surname;
Models.Stunde.EmployeeId = int.Parse(op.id);
Title = op.name + " " + op.surname;
ServerLabel.Text = "Server: " + tokendata.url.Replace("/appapi", "").Replace("https://", "").Replace("http://", "");
Preferences.Default.Set("apiKey", barcode.Value);
Preferences.Default.Set("name", op.name);
Preferences.Default.Set("surname", op.surname);
Preferences.Default.Set("EmployeeId", int.Parse(op.id));
//Preferences.Default.Set("apiUrl", Pre);
_loginViewModel.Server = Preferences.Default.Get("apiUrl", "");
await DisplayAlert("Login erfolgreich", op.name + " " + op.surname, "OK");
} catch (Exception e) {
//AlertEvent?.Invoke(this, e.Message);
await DisplayAlert("Fehler",
e.Message,
"OK");
await DisplayAlert("Fehler", e.Message, "OK");
}
});
@@ -84,4 +90,14 @@ public partial class LoginPage : ContentPage {
barcodeScannerView.IsDetecting = true;
barcodeScannerView.CameraLocation = CameraLocation.Rear;
}
//private void Vm_AlertEvent(object? sender, string e) {
// DisplayAlert("Fehler:", e, "OK");
//}
//private void Vm_InfoEvent(object? sender, string e) {
// DisplayAlert("Information:", e, "OK");
//}
//private async Task Vm_MsgEvent(string title, string message) {
// await DisplayAlert(title, message, "OK");
//}
}