Skip to content

Commit

Permalink
improve the flag editor warning viewmodel
Browse files Browse the repository at this point in the history
- no longer creates a new viewmodel every page reload
- fixes an oversight
  • Loading branch information
bluepilledgreat committed Oct 29, 2024
1 parent ba561a2 commit 81baca5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public partial class FastFlagEditorWarningPage

public FastFlagEditorWarningPage()
{
DataContext = new FastFlagEditorWarningViewModel(this);
var vm = new FastFlagEditorWarningViewModel(this);
DataContext = vm;
vm.StartCountdown();

InitializeComponent();
}

Expand All @@ -26,7 +29,7 @@ private void Page_Loaded(object sender, RoutedEventArgs e)
return;
}

DataContext = new FastFlagEditorWarningViewModel(this);
((FastFlagEditorWarningViewModel)DataContext).StartCountdown();
}
}
}
31 changes: 25 additions & 6 deletions Bloxstrap/UI/ViewModels/Settings/FastFlagEditorWarningViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal class FastFlagEditorWarningViewModel : NotifyPropertyChangedViewModel
{
private Page _page;

private CancellationTokenSource? _cancellationTokenSource;

public string ContinueButtonText { get; set; } = "";

public bool CanContinue { get; set; } = false;
Expand All @@ -24,34 +26,51 @@ internal class FastFlagEditorWarningViewModel : NotifyPropertyChangedViewModel
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;
OnPropertyChanged(nameof(ContinueButtonText));

CanContinue = true;
OnPropertyChanged(nameof(CanContinue));

App.State.Prop.ShowFFlagEditorWarning = false;
App.State.Save();
}

private void Continue()
{
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));
}
Expand Down

0 comments on commit 81baca5

Please sign in to comment.