Refactor Api-Client
Add Exceptionhandler, AlertService JSON-Converter AppSettings via DI Reformat Code
This commit is contained in:
@@ -9,197 +9,195 @@ namespace Jugenddienst_Stunden.Views;
|
||||
/// Die Loginseite mit dem Barcodescanner
|
||||
/// </summary>
|
||||
public partial class LoginPage : ContentPage {
|
||||
|
||||
private DateTime _lastDetectionTime;
|
||||
private readonly TimeSpan _detectionInterval = TimeSpan.FromSeconds(5);
|
||||
private DateTime _lastDetectionTime;
|
||||
private readonly TimeSpan _detectionInterval = TimeSpan.FromSeconds(5);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// CTOR
|
||||
/// </summary>
|
||||
public LoginPage() {
|
||||
InitializeComponent();
|
||||
/// <summary>
|
||||
/// CTOR
|
||||
/// </summary>
|
||||
public LoginPage() {
|
||||
InitializeComponent();
|
||||
|
||||
barcodeScannerView.Options = new BarcodeReaderOptions {
|
||||
Formats = BarcodeFormat.QrCode,
|
||||
AutoRotate = true,
|
||||
Multiple = false
|
||||
};
|
||||
barcodeScannerView.Options =
|
||||
new BarcodeReaderOptions { Formats = BarcodeFormat.QrCode, AutoRotate = true, Multiple = false };
|
||||
|
||||
//if (BindingContext is LoginViewModel vm) {
|
||||
// vm.AlertEvent += Vm_AlertEvent;
|
||||
// vm.InfoEvent += Vm_InfoEvent;
|
||||
// vm.MsgEvent += Vm_MsgEvent;
|
||||
//}
|
||||
//if (BindingContext is LoginViewModel vm) {
|
||||
// vm.AlertEvent += Vm_AlertEvent;
|
||||
// vm.InfoEvent += Vm_InfoEvent;
|
||||
// vm.MsgEvent += Vm_MsgEvent;
|
||||
//}
|
||||
|
||||
//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;
|
||||
}
|
||||
//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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Nach der Erkennung des Barcodes wird der Benutzer eingeloggt
|
||||
/// ZXing.Net.Maui.Controls 0.4.4
|
||||
/// </summary>
|
||||
private void BarcodesDetected(object sender, BarcodeDetectionEventArgs e) {
|
||||
/// <summary>
|
||||
/// Nach der Erkennung des Barcodes wird der Benutzer eingeloggt
|
||||
/// ZXing.Net.Maui.Controls 0.4.4
|
||||
/// </summary>
|
||||
private void BarcodesDetected(object sender, BarcodeDetectionEventArgs e) {
|
||||
var currentTime = DateTime.Now;
|
||||
if ((currentTime - _lastDetectionTime) > _detectionInterval) {
|
||||
_lastDetectionTime = currentTime;
|
||||
foreach (var barcode in e.Results) {
|
||||
if (GlobalVar.ApiKey != barcode.Value) {
|
||||
_ = MainThread.InvokeOnMainThreadAsync(async () => {
|
||||
//await DisplayAlert("Barcode erkannt", $"Barcode: {barcode.Format} - {barcode.Value}", "OK");
|
||||
|
||||
var currentTime = DateTime.Now;
|
||||
if ((currentTime - _lastDetectionTime) > _detectionInterval) {
|
||||
_lastDetectionTime = currentTime;
|
||||
foreach (var barcode in e.Results) {
|
||||
if (GlobalVar.ApiKey != barcode.Value) {
|
||||
_ = MainThread.InvokeOnMainThreadAsync(async () => {
|
||||
//await DisplayAlert("Barcode erkannt", $"Barcode: {barcode.Format} - {barcode.Value}", "OK");
|
||||
try {
|
||||
var tokendata = new TokenData(barcode.Value);
|
||||
GlobalVar.ApiUrl = tokendata.Url;
|
||||
User user = await HoursBase.LoadUser(barcode.Value);
|
||||
|
||||
try {
|
||||
var tokendata = new TokenData(barcode.Value);
|
||||
GlobalVar.ApiUrl = tokendata.Url;
|
||||
User user = await HoursBase.LoadUser(barcode.Value);
|
||||
GlobalVar.ApiKey = barcode.Value;
|
||||
GlobalVar.Name = user.Name;
|
||||
GlobalVar.Surname = user.Surname;
|
||||
GlobalVar.EmployeeId = user.Id;
|
||||
|
||||
GlobalVar.ApiKey = barcode.Value;
|
||||
GlobalVar.Name = user.Name;
|
||||
GlobalVar.Surname = user.Surname;
|
||||
GlobalVar.EmployeeId = user.Id;
|
||||
|
||||
Title = user.Name + " " + user.Surname;
|
||||
//Auf der Loginseite wird der Server als Info ohne Protokoll und ohne /appapi angezeigt
|
||||
ServerLabel.Text = "Server: " + tokendata.Url.Replace("/appapi", "").Replace("https://", "").Replace("http://", "");
|
||||
Title = user.Name + " " + user.Surname;
|
||||
//Auf der Loginseite wird der Server als Info ohne Protokoll und ohne /appapi angezeigt
|
||||
ServerLabel.Text = "Server: " + tokendata.Url.Replace("/appapi", "").Replace("https://", "")
|
||||
.Replace("http://", "");
|
||||
|
||||
|
||||
await DisplayAlert("Login erfolgreich", user.Name + " " + user.Surname, "OK");
|
||||
if (Navigation.NavigationStack.Count > 1) {
|
||||
//Beim ersten Start ohne Login, wird man automatisch auf die Loginseite geleitet. Danach in der History zur<75>ck
|
||||
await Navigation.PopAsync();
|
||||
} else {
|
||||
//Beim manuellen Wechsel auf die Loginseite leiten wir nach erfolgreichem Login auf die Stunden<65>bersicht
|
||||
await Shell.Current.GoToAsync($"//StundenPage");
|
||||
}
|
||||
await DisplayAlert("Login erfolgreich", user.Name + " " + user.Surname, "OK");
|
||||
if (Navigation.NavigationStack.Count > 1) {
|
||||
//Beim ersten Start ohne Login, wird man automatisch auf die Loginseite geleitet. Danach in der History zur<75>ck
|
||||
await Navigation.PopAsync();
|
||||
} else {
|
||||
//Beim manuellen Wechsel auf die Loginseite leiten wir nach erfolgreichem Login auf die Stunden<65>bersicht
|
||||
await Shell.Current.GoToAsync($"//StundenPage");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
await DisplayAlert("Fehler", e.Message, "OK");
|
||||
}
|
||||
});
|
||||
} else {
|
||||
MainThread.InvokeOnMainThreadAsync(() => {
|
||||
DisplayAlert("Bereits eingeloggt",
|
||||
Preferences.Default.Get("name", "") + " " + Preferences.Default.Get("surname", ""),
|
||||
"OK");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
await DisplayAlert("Fehler", e.Message, "OK");
|
||||
}
|
||||
protected override void OnDisappearing() {
|
||||
base.OnDisappearing();
|
||||
|
||||
});
|
||||
} else {
|
||||
MainThread.InvokeOnMainThreadAsync(() => {
|
||||
DisplayAlert("Bereits eingeloggt",
|
||||
Preferences.Default.Get("name", "") + " " + Preferences.Default.Get("surname", ""),
|
||||
"OK");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
barcodeScannerView.CameraLocation = CameraLocation.Front;
|
||||
barcodeScannerView.IsDetecting = false;
|
||||
}
|
||||
|
||||
}
|
||||
protected override void OnAppearing() {
|
||||
base.OnAppearing();
|
||||
|
||||
protected override void OnDisappearing() {
|
||||
base.OnDisappearing();
|
||||
barcodeScannerView.IsDetecting = true;
|
||||
barcodeScannerView.CameraLocation = CameraLocation.Rear;
|
||||
}
|
||||
|
||||
barcodeScannerView.CameraLocation = CameraLocation.Front;
|
||||
barcodeScannerView.IsDetecting = false;
|
||||
}
|
||||
public bool IsCameraAvailable() {
|
||||
var status = Permissions.CheckStatusAsync<Permissions.Camera>().Result;
|
||||
if (status != PermissionStatus.Granted) {
|
||||
status = Permissions.RequestAsync<Permissions.Camera>().Result;
|
||||
}
|
||||
|
||||
protected override void OnAppearing() {
|
||||
base.OnAppearing();
|
||||
return status != PermissionStatus.Granted;
|
||||
}
|
||||
|
||||
barcodeScannerView.IsDetecting = true;
|
||||
barcodeScannerView.CameraLocation = CameraLocation.Rear;
|
||||
}
|
||||
|
||||
public bool IsCameraAvailable() {
|
||||
var status = Permissions.CheckStatusAsync<Permissions.Camera>().Result;
|
||||
if (status != PermissionStatus.Granted) {
|
||||
status = Permissions.RequestAsync<Permissions.Camera>().Result;
|
||||
}
|
||||
return status != PermissionStatus.Granted;
|
||||
}
|
||||
|
||||
private async void OnLoginButtonClicked(object sender, EventArgs e) {
|
||||
var username = UsernameEntry.Text;
|
||||
var password = PasswordEntry.Text;
|
||||
var server = ServerEntry.Text;
|
||||
private async void OnLoginButtonClicked(object sender, EventArgs e) {
|
||||
var username = UsernameEntry.Text;
|
||||
var password = PasswordEntry.Text;
|
||||
var server = ServerEntry.Text;
|
||||
|
||||
|
||||
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(server)) {
|
||||
await DisplayAlert("Fehler", "Bitte alle Felder ausf<73>llen", "OK");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Uri uri = new Uri(InputUrlWithSchema(server));
|
||||
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(server)) {
|
||||
await DisplayAlert("Fehler", "Bitte alle Felder ausf<73>llen", "OK");
|
||||
return;
|
||||
}
|
||||
|
||||
Types.User response = await BaseFunc.AuthUserPass(username, password, uri.Scheme + "://" + uri.Authority + "/appapi");
|
||||
try {
|
||||
Uri uri = new Uri(InputUrlWithSchema(server));
|
||||
|
||||
GlobalVar.ApiKey = response.Token;
|
||||
GlobalVar.Name = response.Name;
|
||||
GlobalVar.Surname = response.Surname;
|
||||
GlobalVar.EmployeeId = response.Id;
|
||||
GlobalVar.ApiUrl = uri.Scheme + "://" + uri.Authority + "/appapi";
|
||||
Types.User response =
|
||||
await BaseFunc.AuthUserPass(username, password, uri.Scheme + "://" + uri.Authority + "/appapi");
|
||||
|
||||
Title = response.Name + " " + response.Surname;
|
||||
//ServerLabel.Text = "Server: " + server.Replace("/appapi", "").Replace("https://", "").Replace("http://", "");
|
||||
ServerLabel.Text = "Server: " + uri.Authority;
|
||||
GlobalVar.ApiKey = response.Token;
|
||||
GlobalVar.Name = response.Name;
|
||||
GlobalVar.Surname = response.Surname;
|
||||
GlobalVar.EmployeeId = response.Id;
|
||||
GlobalVar.ApiUrl = uri.Scheme + "://" + uri.Authority + "/appapi";
|
||||
|
||||
await DisplayAlert("Login erfolgreich", response.Name + " " + response.Surname, "OK");
|
||||
if (Navigation.NavigationStack.Count > 1)
|
||||
await Navigation.PopAsync();
|
||||
else {
|
||||
await Shell.Current.GoToAsync($"//StundenPage");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
await DisplayAlert("Fehler", ex.Message, "OK");
|
||||
}
|
||||
}
|
||||
Title = response.Name + " " + response.Surname;
|
||||
//ServerLabel.Text = "Server: " + server.Replace("/appapi", "").Replace("https://", "").Replace("http://", "");
|
||||
ServerLabel.Text = "Server: " + uri.Authority;
|
||||
|
||||
/// <summary>
|
||||
/// Aus einer URL ohne Schema eine URL mit Schema machen
|
||||
/// </summary>
|
||||
private static string InputUrlWithSchema(string url) {
|
||||
if (!url.StartsWith("http://") && !url.StartsWith("https://")) {
|
||||
url = "https://" + url;
|
||||
}
|
||||
if (url.StartsWith("http://")) {
|
||||
url = url.Replace("http://", "https://");
|
||||
}
|
||||
return url;
|
||||
}
|
||||
await DisplayAlert("Login erfolgreich", response.Name + " " + response.Surname, "OK");
|
||||
if (Navigation.NavigationStack.Count > 1)
|
||||
await Navigation.PopAsync();
|
||||
else {
|
||||
await Shell.Current.GoToAsync($"//StundenPage");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
await DisplayAlert("Fehler", ex.Message, "OK");
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
/// <summary>
|
||||
/// Aus einer URL ohne Schema eine URL mit Schema machen
|
||||
/// </summary>
|
||||
private static string InputUrlWithSchema(string url) {
|
||||
if (!url.StartsWith("http://") && !url.StartsWith("https://")) {
|
||||
url = "https://" + url;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
if (url.StartsWith("http://")) {
|
||||
url = url.Replace("http://", "https://");
|
||||
}
|
||||
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
//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");
|
||||
//}
|
||||
}
|
||||
//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");
|
||||
}
|
||||
}
|
||||
|
||||
//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");
|
||||
//}
|
||||
}
|
||||
Reference in New Issue
Block a user