Add InfoEvent with confirmation
This commit is contained in:
21
Jugenddienst Stunden/Models/ConfirmationEventArgs.cs
Normal file
21
Jugenddienst Stunden/Models/ConfirmationEventArgs.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Jugenddienst_Stunden.Models;
|
||||
public sealed class ConfirmationEventArgs : System.EventArgs {
|
||||
public string Title { get; }
|
||||
public string Message { get; }
|
||||
public string ConfirmText { get; }
|
||||
|
||||
private readonly TaskCompletionSource<bool> _tcs = new();
|
||||
|
||||
public ConfirmationEventArgs(string title, string message, string confirmText = "OK") {
|
||||
Title = title;
|
||||
Message = message;
|
||||
ConfirmText = confirmText;
|
||||
}
|
||||
|
||||
public Task<bool> Task => _tcs.Task;
|
||||
|
||||
public void SetResult(bool result) => _tcs.TrySetResult(result);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Jugenddienst_Stunden.Interfaces;
|
||||
using Jugenddienst_Stunden.Models;
|
||||
|
||||
namespace Jugenddienst_Stunden.ViewModels;
|
||||
|
||||
@@ -15,7 +16,8 @@ public partial class LoginViewModel : ObservableObject {
|
||||
private readonly TimeSpan _detectionInterval = TimeSpan.FromSeconds(5);
|
||||
|
||||
public event EventHandler<string>? AlertEvent;
|
||||
public event EventHandler<string>? InfoEvent;
|
||||
//public event EventHandler<string>? InfoEvent;
|
||||
public event EventHandler<ConfirmationEventArgs>? InfoEvent;
|
||||
|
||||
/// <summary>
|
||||
/// Name der Anwendung
|
||||
@@ -102,9 +104,13 @@ public partial class LoginViewModel : ObservableObject {
|
||||
(Server ?? string.Empty).Trim());
|
||||
|
||||
Title = $"{user.Name} {user.Surname}";
|
||||
InfoEvent?.Invoke(this, "Login erfolgreich");
|
||||
|
||||
// Info zeigen und auf Bestätigung warten
|
||||
var args = new ConfirmationEventArgs("Information:", "Login erfolgreich");
|
||||
InfoEvent?.Invoke(this, args);
|
||||
bool confirmed = await args.Task;
|
||||
if (confirmed) {
|
||||
await Shell.Current.GoToAsync("//StundenPage");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
if (_alerts is not null) {
|
||||
_alerts.Raise(ex.Message);
|
||||
@@ -128,7 +134,13 @@ public partial class LoginViewModel : ObservableObject {
|
||||
var user = await _auth.LoginWithToken(token);
|
||||
Title = $"{user.Name} {user.Surname}";
|
||||
|
||||
// Info zeigen und auf Bestätigung warten
|
||||
var infoArgs = new ConfirmationEventArgs("Information:", "Login erfolgreich");
|
||||
InfoEvent?.Invoke(this, infoArgs);
|
||||
bool confirmed = await infoArgs.Task;
|
||||
if (confirmed) {
|
||||
await Shell.Current.GoToAsync("//StundenPage");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
if (_alerts is not null) {
|
||||
_alerts.Raise(ex.Message);
|
||||
|
||||
@@ -33,7 +33,14 @@ public partial class LoginPage : ContentPage {
|
||||
|
||||
if (BindingContext is LoginViewModel vm) {
|
||||
vm.AlertEvent += async (_, msg) => await DisplayAlert("Fehler:", msg, "OK");
|
||||
vm.InfoEvent += async (_, msg) => await DisplayAlert("Information:", msg, "OK");
|
||||
//vm.InfoEvent += async (_, msg) => await DisplayAlert("Information:", msg, "OK");
|
||||
// Neues InfoEvent: Dialog anzeigen und nach Bestätigung das Result setzen
|
||||
vm.InfoEvent += async (_, infoArgs) => {
|
||||
await MainThread.InvokeOnMainThreadAsync(async () => {
|
||||
await DisplayAlert(infoArgs.Title, infoArgs.Message, infoArgs.ConfirmText);
|
||||
infoArgs.SetResult(true);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
barcodeScannerView.Options =
|
||||
|
||||
Reference in New Issue
Block a user