From f394eddc015a4d098ea9368330403d008d536c74 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 24 Sep 2018 15:30:05 -0400 Subject: [PATCH] throttle fast type searches --- src/iOS.Autofill/LoginSearchViewController.cs | 26 +++++++++++++++++-- src/iOS.Core/Views/ExtensionTableSource.cs | 4 ++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/iOS.Autofill/LoginSearchViewController.cs b/src/iOS.Autofill/LoginSearchViewController.cs index 896374061..b033a4ad2 100644 --- a/src/iOS.Autofill/LoginSearchViewController.cs +++ b/src/iOS.Autofill/LoginSearchViewController.cs @@ -8,6 +8,7 @@ using Bit.iOS.Core.Controllers; using Bit.App.Resources; using Bit.iOS.Core.Views; using System.Threading; +using System.Threading.Tasks; namespace Bit.iOS.Autofill { @@ -163,6 +164,7 @@ namespace Bit.iOS.Autofill public class SearchDelegate : UISearchBarDelegate { private readonly LoginSearchViewController _controller; + private CancellationTokenSource _filterResultsCancellationTokenSource; public SearchDelegate(LoginSearchViewController controller) { @@ -171,8 +173,28 @@ namespace Bit.iOS.Autofill public override void TextChanged(UISearchBar searchBar, string searchText) { - ((TableSource)_controller.TableView.Source).FilterResults(searchText, new CancellationToken()); - _controller.TableView.ReloadData(); + var cts = new CancellationTokenSource(); + Task.Run(async () => + { + if(!string.IsNullOrWhiteSpace(searchText)) + { + await Task.Delay(300); + if(searchText != searchBar.Text) + { + return; + } + else + { + _filterResultsCancellationTokenSource?.Cancel(); + } + } + try + { + ((TableSource)_controller.TableView.Source).FilterResults(searchText, cts.Token); + _controller.TableView.ReloadData(); + } + catch(OperationCanceledException) { } + }, cts.Token); } } } diff --git a/src/iOS.Core/Views/ExtensionTableSource.cs b/src/iOS.Core/Views/ExtensionTableSource.cs index a57c0172d..5eeca6abf 100644 --- a/src/iOS.Core/Views/ExtensionTableSource.cs +++ b/src/iOS.Core/Views/ExtensionTableSource.cs @@ -59,7 +59,9 @@ namespace Bit.iOS.Core.Views combinedLogins.AddRange(logins); } - _allItems = combinedLogins.Select(s => new CipherViewModel(s)) + _allItems = combinedLogins + .Where(c => c.Type == App.Enums.CipherType.Login) + .Select(s => new CipherViewModel(s)) .OrderBy(s => s.Name) .ThenBy(s => s.Username) .ToList() ?? new List();