From 2c70430dfa312762cb6aa10fd74486cd00773419 Mon Sep 17 00:00:00 2001 From: Matt <97983689+bluepilledgreat@users.noreply.github.com> Date: Tue, 29 Oct 2024 21:55:34 +0000 Subject: [PATCH] improve the flag editor warning viewmodel (#3532) * improve the flag editor warning viewmodel - no longer creates a new viewmodel every page reload - fixes an oversight * stop countdown on unload * move the viewmodel to a variable makes everything look cleaner * remove initialload check --- .../Pages/FastFlagEditorWarningPage.xaml | 1 + .../Pages/FastFlagEditorWarningPage.xaml.cs | 19 +++++----- .../FastFlagEditorWarningViewModel.cs | 37 ++++++++++++++++--- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml b/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml index 9a2b938..55b0d71 100644 --- a/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml +++ b/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml @@ -10,6 +10,7 @@ d:DesignHeight="450" d:DesignWidth="800" Scrollable="True" Loaded="Page_Loaded" + Unloaded="Page_Unloaded" Title="FastFlagEditorWarningPage"> diff --git a/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml.cs b/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml.cs index 89d1e4c..9b3b08a 100644 --- a/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml.cs +++ b/Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorWarningPage.xaml.cs @@ -8,25 +8,24 @@ namespace Bloxstrap.UI.Elements.Settings.Pages /// public partial class FastFlagEditorWarningPage { - private bool _initialLoad = false; + private FastFlagEditorWarningViewModel _viewModel; public FastFlagEditorWarningPage() { - DataContext = new FastFlagEditorWarningViewModel(this); + _viewModel = new FastFlagEditorWarningViewModel(this); + DataContext = _viewModel; + InitializeComponent(); } private void Page_Loaded(object sender, RoutedEventArgs e) { - // refresh datacontext on page load to reset timer + _viewModel.StartCountdown(); + } - if (!_initialLoad) - { - _initialLoad = true; - return; - } - - DataContext = new FastFlagEditorWarningViewModel(this); + private void Page_Unloaded(object sender, RoutedEventArgs e) + { + _viewModel.StopCountdown(); } } } diff --git a/Bloxstrap/UI/ViewModels/Settings/FastFlagEditorWarningViewModel.cs b/Bloxstrap/UI/ViewModels/Settings/FastFlagEditorWarningViewModel.cs index c335d35..957f62b 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,40 @@ namespace Bloxstrap.UI.ViewModels.Settings public FastFlagEditorWarningViewModel(Page page) { _page = page; - DoCountdown(); } - private async void DoCountdown() + public void StopCountdown() { + _cancellationTokenSource?.Cancel(); + _cancellationTokenSource = null; + } + + public void StartCountdown() + { + StopCountdown(); + + _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 +67,6 @@ namespace Bloxstrap.UI.ViewModels.Settings CanContinue = true; OnPropertyChanged(nameof(CanContinue)); - - App.State.Prop.ShowFFlagEditorWarning = false; - App.State.Save(); } private void Continue() @@ -52,6 +74,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)); }