diff --git a/src/App/Pages/Generator/GeneratorPage.xaml b/src/App/Pages/Generator/GeneratorPage.xaml index 609a31874..63faa575b 100644 --- a/src/App/Pages/Generator/GeneratorPage.xaml +++ b/src/App/Pages/Generator/GeneratorPage.xaml @@ -105,6 +105,7 @@ VerticalOptions="CenterAndExpand" HorizontalTextAlignment="End" /> + { + await Task.Delay(500); + if(DateTime.UtcNow - page.LastLengthSliderChange < TimeSpan.FromMilliseconds(450)) + { + return; + } + else + { + previousCts?.Cancel(); + } + cts.Token.ThrowIfCancellationRequested(); + await _passwordGenerationService.SaveOptionsAsync(_options); + cts.Token.ThrowIfCancellationRequested(); + await _passwordGenerationService.AddHistoryAsync(Password, cts.Token); + }, cts.Token); + _sliderCancellationTokenSource = cts; + } + public async Task CopyAsync() { await _platformUtilsService.CopyToClipboardAsync(Password); diff --git a/src/Core/Abstractions/IPasswordGenerationService.cs b/src/Core/Abstractions/IPasswordGenerationService.cs index d4e37d603..dc91d82d7 100644 --- a/src/Core/Abstractions/IPasswordGenerationService.cs +++ b/src/Core/Abstractions/IPasswordGenerationService.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Bit.Core.Models.Domain; @@ -6,7 +7,7 @@ namespace Bit.Core.Abstractions { public interface IPasswordGenerationService { - Task AddHistoryAsync(string password); + Task AddHistoryAsync(string password, CancellationToken token = default(CancellationToken)); Task ClearAsync(); Task GeneratePassphraseAsync(PasswordGenerationOptions options); Task GeneratePasswordAsync(PasswordGenerationOptions options); diff --git a/src/Core/Services/PasswordGenerationService.cs b/src/Core/Services/PasswordGenerationService.cs index c55a91c78..76f5b61f0 100644 --- a/src/Core/Services/PasswordGenerationService.cs +++ b/src/Core/Services/PasswordGenerationService.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace Bit.Core.Services @@ -240,7 +241,7 @@ namespace Bit.Core.Services return _history ?? new List(); } - public async Task AddHistoryAsync(string password) + public async Task AddHistoryAsync(string password, CancellationToken token = default(CancellationToken)) { var hasKey = await _cryptoService.HasKeyAsync(); if(!hasKey) @@ -253,6 +254,7 @@ namespace Bit.Core.Services { return; } + token.ThrowIfCancellationRequested(); currentHistory.Insert(0, new GeneratedPasswordHistory { Password = password, Date = DateTime.UtcNow }); // Remove old items. if(currentHistory.Count > MaxPasswordsInHistory) @@ -260,6 +262,7 @@ namespace Bit.Core.Services currentHistory.RemoveAt(currentHistory.Count - 1); } var newHistory = await EncryptHistoryAsync(currentHistory); + token.ThrowIfCancellationRequested(); await _storageService.SaveAsync(Keys_History, newHistory); }