61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
using Avalonia.Controls;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Labs.Lottie;
|
|
using LehrerApp.Desktop.ViewModels.Groups;
|
|
|
|
namespace LehrerApp.Desktop.Views.Groups;
|
|
|
|
public partial class RandomStudentPickerWindow : Window
|
|
{
|
|
private bool _isSpinning;
|
|
private DateTime _spinStartedAt;
|
|
|
|
public RandomStudentPickerWindow() => InitializeComponent();
|
|
|
|
private async void OnDraw(object? sender, RoutedEventArgs e)
|
|
{
|
|
if (DataContext is not RandomStudentPickerViewModel vm ||
|
|
!vm.DrawCommand.CanExecute(null)) return;
|
|
|
|
var card = this.FindControl<Border>("RevealCard");
|
|
var wheel = this.FindControl<Lottie>("WheelAnimation");
|
|
var button = this.FindControl<Button>("DrawButton");
|
|
if (card is null || wheel is null || button is null) return;
|
|
|
|
if (!_isSpinning)
|
|
{
|
|
_isSpinning = true;
|
|
_spinStartedAt = DateTime.UtcNow;
|
|
card.IsVisible = false;
|
|
wheel.IsVisible = true;
|
|
this.FindControl<Expander>("OptionsExpander")!.IsEnabled = false;
|
|
this.FindControl<Button>("ResetButton")!.IsEnabled = false;
|
|
button.Content = "Dreht …";
|
|
// Erst nach dem Layoutwechsel starten, damit das erste Bild sichtbar ist.
|
|
await Task.Yield();
|
|
if (!_isSpinning) return;
|
|
wheel.Start();
|
|
button.Content = "Ergebnis anzeigen";
|
|
return;
|
|
}
|
|
|
|
// Ein Doppelklick soll die Ziehung nicht unbemerkt sofort auflösen.
|
|
if (DateTime.UtcNow - _spinStartedAt < TimeSpan.FromSeconds(1.2)) return;
|
|
wheel.Stop();
|
|
wheel.IsVisible = false;
|
|
vm.DrawCommand.Execute(null);
|
|
card.IsVisible = true;
|
|
this.FindControl<Expander>("OptionsExpander")!.IsEnabled = true;
|
|
this.FindControl<Button>("ResetButton")!.IsEnabled = true;
|
|
button.Content = "🎲 Ziehen";
|
|
_isSpinning = false;
|
|
}
|
|
|
|
protected override void OnClosed(EventArgs e)
|
|
{
|
|
_isSpinning = false;
|
|
this.FindControl<Lottie>("WheelAnimation")?.Stop();
|
|
base.OnClosed(e);
|
|
}
|
|
}
|