save length and history when value done changing

This commit is contained in:
Kyle Spearrin 2019-05-31 09:09:32 -04:00
parent ac6f3a6bb6
commit 8df940447d
5 changed files with 55 additions and 5 deletions

View file

@ -105,6 +105,7 @@
VerticalOptions="CenterAndExpand"
HorizontalTextAlignment="End" />
<controls:ExtendedSlider
ValueChanged="LengthSlider_ValueChanged"
Value="{Binding Length}"
StyleClass="box-value"
VerticalOptions="CenterAndExpand"

View file

@ -23,6 +23,8 @@ namespace Bit.App.Pages
}
}
public DateTime LastLengthSliderChange { get; set; } = DateTime.MinValue;
public async Task InitAsync()
{
await _vm.InitAsync();
@ -57,5 +59,13 @@ namespace Bit.App.Pages
var page = new GeneratorHistoryPage();
await Navigation.PushModalAsync(new NavigationPage(page));
}
private void LengthSlider_ValueChanged(object sender, ValueChangedEventArgs e)
{
if(e.NewValue != e.OldValue)
{
LastLengthSliderChange = DateTime.UtcNow;
}
}
}
}

View file

@ -3,7 +3,9 @@ using Bit.App.Utilities;
using Bit.Core.Abstractions;
using Bit.Core.Models.Domain;
using Bit.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms;
@ -29,6 +31,7 @@ namespace Bit.App.Pages
private string _wordSeparator;
private int _typeSelectedIndex;
private bool _doneIniting;
private CancellationTokenSource _sliderCancellationTokenSource;
public GeneratorPageViewModel()
{
@ -67,7 +70,7 @@ namespace Bit.App.Pages
if(SetProperty(ref _length, value))
{
_options.Length = value;
var task = SaveOptionsAsync();
var task = SaveOptionsSliderAsync();
}
}
}
@ -207,8 +210,7 @@ namespace Bit.App.Pages
{
_options = await _passwordGenerationService.GetOptionsAsync();
LoadFromOptions();
Password = await _passwordGenerationService.GeneratePasswordAsync(_options);
await _passwordGenerationService.AddHistoryAsync(Password);
await RegenerateAsync();
_doneIniting = true;
}
@ -234,6 +236,39 @@ namespace Bit.App.Pages
}
}
public async Task SaveOptionsSliderAsync()
{
if(!_doneIniting)
{
return;
}
SetOptions();
_passwordGenerationService.NormalizeOptions(_options);
LoadFromOptions();
Password = await _passwordGenerationService.GeneratePasswordAsync(_options);
var page = Page as GeneratorPage;
var previousCts = _sliderCancellationTokenSource;
var cts = new CancellationTokenSource();
var task = Task.Run(async () =>
{
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);

View file

@ -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<string> GeneratePassphraseAsync(PasswordGenerationOptions options);
Task<string> GeneratePasswordAsync(PasswordGenerationOptions options);

View file

@ -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<GeneratedPasswordHistory>();
}
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);
}