diff --git a/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml.cs b/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml.cs index 89d1e4c..8a8d723 100644 --- a/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml.cs +++ b/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml.cs @@ -12,7 +12,10 @@ namespace Bloxstrap.UI.Elements.Settings.Pages public FastFlagEditorWarningPage() { - DataContext = new FastFlagEditorWarningViewModel(this); + var vm = new FastFlagEditorWarningViewModel(this); + DataContext = vm; + vm.StartCountdown(); + InitializeComponent(); } @@ -26,7 +29,7 @@ namespace Bloxstrap.UI.Elements.Settings.Pages return; } - DataContext = new FastFlagEditorWarningViewModel(this); + ((FastFlagEditorWarningViewModel)DataContext).StartCountdown(); } } } diff --git a/Bloxstrap/UI/ViewModels/Settings/FastFlagEditorWarningViewModel.cs b/Bloxstrap/UI/ViewModels/Settings/FastFlagEditorWarningViewModel.cs index c335d35..08a18fe 100644 --- a/Bloxstrap/UI/ViewModels/Settings/FastFlagEditorWarningViewModel.cs +++ b/Bloxstrap/UI/ViewModels/Settings/FastFlagEditorWarningViewModel.cs @@ -13,6 +13,8 @@ namespace Bloxstrap.UI.ViewModels.Settings { private Page _page; + private CancellationTokenSource? _cancellationTokenSource; + public string ContinueButtonText { get; set; } = ""; public bool CanContinue { get; set; } = false; @@ -24,17 +26,34 @@ namespace Bloxstrap.UI.ViewModels.Settings public FastFlagEditorWarningViewModel(Page page) { _page = page; - DoCountdown(); } - private async void DoCountdown() + public void StartCountdown() { + _cancellationTokenSource?.Cancel(); + + _cancellationTokenSource = new CancellationTokenSource(); + DoCountdown(_cancellationTokenSource.Token); + } + + private async void DoCountdown(CancellationToken token) + { + CanContinue = false; + OnPropertyChanged(nameof(CanContinue)); + for (int i = 10; i > 0; i--) { ContinueButtonText = $"({i}) {Strings.Menu_FastFlagEditor_Warning_Continue}"; OnPropertyChanged(nameof(ContinueButtonText)); - await Task.Delay(1000); + try + { + await Task.Delay(1000, token); + } + catch (TaskCanceledException) + { + return; + } } ContinueButtonText = Strings.Menu_FastFlagEditor_Warning_Continue; @@ -42,9 +61,6 @@ namespace Bloxstrap.UI.ViewModels.Settings CanContinue = true; OnPropertyChanged(nameof(CanContinue)); - - App.State.Prop.ShowFFlagEditorWarning = false; - App.State.Save(); } private void Continue() @@ -52,6 +68,9 @@ namespace Bloxstrap.UI.ViewModels.Settings if (!CanContinue) return; + App.State.Prop.ShowFFlagEditorWarning = false; + App.State.Save(); // should we be force saving here? + if (Window.GetWindow(_page) is INavigationWindow window) window.Navigate(typeof(FastFlagEditorPage)); }