Invoke PresentationFolders reset on main thread. Formatting. Move to ThrowIfCancellationRequested.

This commit is contained in:
Kyle Spearrin 2016-08-15 23:09:41 -04:00
parent c03b5d6c00
commit db36f618db

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Acr.UserDialogs; using Acr.UserDialogs;
@ -108,7 +107,8 @@ namespace Bit.App.Pages
private void SearchBar_SearchButtonPressed(object sender, EventArgs e) private void SearchBar_SearchButtonPressed(object sender, EventArgs e)
{ {
_filterResultsCancellationTokenSource = FilterResultsBackground(((SearchBar)sender).Text, _filterResultsCancellationTokenSource); _filterResultsCancellationTokenSource = FilterResultsBackground(((SearchBar)sender).Text,
_filterResultsCancellationTokenSource);
} }
private void SearchBar_TextChanged(object sender, TextChangedEventArgs e) private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
@ -120,7 +120,8 @@ namespace Bit.App.Pages
return; return;
} }
_filterResultsCancellationTokenSource = FilterResultsBackground(e.NewTextValue, _filterResultsCancellationTokenSource); _filterResultsCancellationTokenSource = FilterResultsBackground(e.NewTextValue,
_filterResultsCancellationTokenSource);
} }
private CancellationTokenSource FilterResultsBackground(string searchFilter, CancellationTokenSource previousCts) private CancellationTokenSource FilterResultsBackground(string searchFilter, CancellationTokenSource previousCts)
@ -153,10 +154,7 @@ namespace Bit.App.Pages
private void FilterResults(string searchFilter, CancellationToken ct) private void FilterResults(string searchFilter, CancellationToken ct)
{ {
if(ct.IsCancellationRequested) ct.ThrowIfCancellationRequested();
{
throw new OperationCanceledException(ct);
}
if(string.IsNullOrWhiteSpace(searchFilter)) if(string.IsNullOrWhiteSpace(searchFilter))
{ {
@ -170,11 +168,7 @@ namespace Bit.App.Pages
.TakeWhile(s => !ct.IsCancellationRequested) .TakeWhile(s => !ct.IsCancellationRequested)
.ToList(); .ToList();
if(ct.IsCancellationRequested) ct.ThrowIfCancellationRequested();
{
throw new OperationCanceledException(ct);
}
LoadFolders(filteredSites, ct); LoadFolders(filteredSites, ct);
} }
} }
@ -193,7 +187,8 @@ namespace Bit.App.Pages
Action registerAction = () => Action registerAction = () =>
{ {
var lastPushRegistration = _settings.GetValueOrDefault<DateTime?>(Constants.PushLastRegistrationDate); var lastPushRegistration = _settings.GetValueOrDefault<DateTime?>(Constants.PushLastRegistrationDate);
if(!pushPromptShow || !lastPushRegistration.HasValue || (DateTime.UtcNow - lastPushRegistration) > TimeSpan.FromDays(1)) if(!pushPromptShow || !lastPushRegistration.HasValue
|| (DateTime.UtcNow - lastPushRegistration) > TimeSpan.FromDays(1))
{ {
_pushNotification.Register(); _pushNotification.Register();
} }
@ -205,7 +200,8 @@ namespace Bit.App.Pages
_userDialogs.Alert(new AlertConfig _userDialogs.Alert(new AlertConfig
{ {
Message = "bitwarden keeps your vault automatically synced by using push notifications." Message = "bitwarden keeps your vault automatically synced by using push notifications."
+ " For the best possible experience, please select \"Ok\" on the following prompt when asked to enable push notifications.", + " For the best possible experience, please select \"Ok\" on the following prompt when"
+ " asked to enable push notifications.",
Title = "Enable Automatic Syncing", Title = "Enable Automatic Syncing",
OnOk = registerAction, OnOk = registerAction,
OkText = "Ok, got it!" OkText = "Ok, got it!"
@ -230,6 +226,8 @@ namespace Bit.App.Pages
return cts; return cts;
} }
_filterResultsCancellationTokenSource?.Cancel();
Task.Run(async () => Task.Run(async () =>
{ {
var foldersTask = _folderService.GetAllAsync(); var foldersTask = _folderService.GetAllAsync();
@ -268,11 +266,7 @@ namespace Bit.App.Pages
.TakeWhile(s => !ct.IsCancellationRequested) .TakeWhile(s => !ct.IsCancellationRequested)
.ToList(); .ToList();
if(ct.IsCancellationRequested) ct.ThrowIfCancellationRequested();
{
throw new OperationCanceledException(ct);
}
folder.AddRange(sitesToAdd); folder.AddRange(sitesToAdd);
} }
@ -281,10 +275,7 @@ namespace Bit.App.Pages
.TakeWhile(s => !ct.IsCancellationRequested) .TakeWhile(s => !ct.IsCancellationRequested)
.ToList(); .ToList();
if(ct.IsCancellationRequested) ct.ThrowIfCancellationRequested();
{
throw new OperationCanceledException(ct);
}
var noneFolder = new VaultListPageModel.Folder(noneToAdd); var noneFolder = new VaultListPageModel.Folder(noneToAdd);
folders.Add(noneFolder); folders.Add(noneFolder);
@ -294,12 +285,8 @@ namespace Bit.App.Pages
.TakeWhile(s => !ct.IsCancellationRequested) .TakeWhile(s => !ct.IsCancellationRequested)
.ToList(); .ToList();
if(ct.IsCancellationRequested) ct.ThrowIfCancellationRequested();
{ Device.BeginInvokeOnMainThread(() => PresentationFolders.ResetWithRange(foldersToAdd));
throw new OperationCanceledException(ct);
}
PresentationFolders.ResetWithRange(foldersToAdd);
} }
private void SiteSelected(object sender, SelectedItemChangedEventArgs e) private void SiteSelected(object sender, SelectedItemChangedEventArgs e)
@ -320,7 +307,8 @@ namespace Bit.App.Pages
{ {
buttons.Add(AppResources.CopyUsername); buttons.Add(AppResources.CopyUsername);
} }
if(!string.IsNullOrWhiteSpace(site.Uri.Value) && (site.Uri.Value.StartsWith("http://") || site.Uri.Value.StartsWith("https://"))) if(!string.IsNullOrWhiteSpace(site.Uri.Value) && (site.Uri.Value.StartsWith("http://")
|| site.Uri.Value.StartsWith("https://")))
{ {
buttons.Add(AppResources.GoToWebsite); buttons.Add(AppResources.GoToWebsite);
} }