Refactor Api-Client
Add Exceptionhandler, AlertService JSON-Converter AppSettings via DI Reformat Code
This commit is contained in:
@@ -9,60 +9,80 @@ namespace Jugenddienst_Stunden.Views;
|
||||
/// Code-Behind f<>r die Stunden-<2D>bersicht
|
||||
/// </summary>
|
||||
public partial class StundenPage : ContentPage {
|
||||
/// <summary>
|
||||
/// CTOR (f<>r Shell/XAML DataTemplate erforderlich)
|
||||
/// </summary>
|
||||
public StundenPage() : this(
|
||||
(Application.Current?.Handler?.MauiContext?.Services
|
||||
?? throw new InvalidOperationException("DI container ist nicht verf<72>gbar."))
|
||||
.GetRequiredService<StundenViewModel>()) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// CTOR
|
||||
/// </summary>
|
||||
public StundenPage() {
|
||||
InitializeComponent();
|
||||
/// <summary>
|
||||
/// CTOR (DI)
|
||||
/// </summary>
|
||||
public StundenPage(StundenViewModel vm) {
|
||||
InitializeComponent();
|
||||
BindingContext = vm;
|
||||
|
||||
if (BindingContext is StundenViewModel vm) {
|
||||
vm.AlertEvent += Vm_AlertEvent;
|
||||
vm.InfoEvent += Vm_InfoEvent;
|
||||
}
|
||||
if (!CheckLogin()) {
|
||||
NavigateToTargetPage();
|
||||
}
|
||||
vm.AlertEvent += Vm_AlertEvent;
|
||||
vm.InfoEvent += Vm_InfoEvent;
|
||||
|
||||
}
|
||||
// Navigation NICHT im CTOR ausf<73>hren (Shell/Navigation-Stack ist hier oft noch nicht ?ready?)
|
||||
// if (!CheckLogin()) {
|
||||
// NavigateToTargetPage();
|
||||
// }
|
||||
}
|
||||
|
||||
private void Vm_AlertEvent(object? sender, string e) {
|
||||
MainThread.BeginInvokeOnMainThread(async () => {
|
||||
await DisplayAlert("Fehler:", e, "OK");
|
||||
});
|
||||
}
|
||||
//private void Vm_InfoEvent(object? sender, string e) {
|
||||
// DisplayAlert("Information:", e, "OK");
|
||||
//}
|
||||
//private void Vm_InfoEvent(object? sender, string e) {
|
||||
// MainThread.BeginInvokeOnMainThread(async () => {
|
||||
// await DisplayAlert("Information:", e, "OK");
|
||||
// });
|
||||
//}
|
||||
private void Vm_InfoEvent(object? sender, string e) {
|
||||
MainThread.BeginInvokeOnMainThread(async () => {
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
ToastDuration duration = ToastDuration.Short;
|
||||
double fontSize = 16;
|
||||
var toast = Toast.Make(e, duration, fontSize);
|
||||
await toast.Show(cts.Token);
|
||||
});
|
||||
}
|
||||
private void Vm_AlertEvent(object? sender, string e) {
|
||||
MainThread.BeginInvokeOnMainThread(async () => { await DisplayAlert("Fehler:", e, "OK"); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Beim Laden der Seite den Titel setzen
|
||||
/// </summary>
|
||||
protected override void OnAppearing() {
|
||||
base.OnAppearing();
|
||||
Title = Preferences.Default.Get("name", "Nicht") + " " + Preferences.Default.Get("surname", "eingeloggt");
|
||||
}
|
||||
//private void Vm_InfoEvent(object? sender, string e) {
|
||||
// DisplayAlert("Information:", e, "OK");
|
||||
//}
|
||||
//private void Vm_InfoEvent(object? sender, string e) {
|
||||
// MainThread.BeginInvokeOnMainThread(async () => {
|
||||
// await DisplayAlert("Information:", e, "OK");
|
||||
// });
|
||||
//}
|
||||
private void Vm_InfoEvent(object? sender, string e) {
|
||||
MainThread.BeginInvokeOnMainThread(async () => {
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
ToastDuration duration = ToastDuration.Short;
|
||||
double fontSize = 16;
|
||||
var toast = Toast.Make(e, duration, fontSize);
|
||||
await toast.Show(cts.Token);
|
||||
});
|
||||
}
|
||||
|
||||
private bool CheckLogin() {
|
||||
return Preferences.Default.Get("apiKey", "") != "";
|
||||
}
|
||||
/// <summary>
|
||||
/// Beim Laden der Seite den Titel setzen
|
||||
/// </summary>
|
||||
protected override async void OnAppearing() {
|
||||
base.OnAppearing();
|
||||
Title = Preferences.Default.Get("name", "Nicht") + " " + Preferences.Default.Get("surname", "eingeloggt");
|
||||
|
||||
private async void NavigateToTargetPage() {
|
||||
await Navigation.PushAsync(new LoginPage());
|
||||
}
|
||||
if (!CheckLogin()) {
|
||||
try {
|
||||
await NavigateToTargetPage();
|
||||
} catch (Exception ex) {
|
||||
await DisplayAlert("Fehler:", ex.Message, "OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool CheckLogin() {
|
||||
return Preferences.Default.Get("apiKey", "") != "";
|
||||
}
|
||||
|
||||
// private async void NavigateToTargetPage() {
|
||||
// await Navigation.PushAsync(new LoginPage());
|
||||
// }
|
||||
|
||||
private Task NavigateToTargetPage() {
|
||||
// Shell-Navigation statt Navigation.PushAsync
|
||||
// Voraussetzung: LoginPage-Route ist in AppShell registriert (Routing.RegisterRoute(...))
|
||||
return Shell.Current.GoToAsync(nameof(Views.LoginPage));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user