diff --git a/src/iOS.Autofill/LoginSearchViewController.cs b/src/iOS.Autofill/LoginSearchViewController.cs index 1fc852501..ab6a66973 100644 --- a/src/iOS.Autofill/LoginSearchViewController.cs +++ b/src/iOS.Autofill/LoginSearchViewController.cs @@ -7,6 +7,7 @@ using Bit.iOS.Core.Utilities; using Bit.iOS.Core.Controllers; using Bit.App.Resources; using Bit.iOS.Core.Views; +using System.Threading; namespace Bit.iOS.Autofill { @@ -158,20 +159,21 @@ namespace Bit.iOS.Autofill } } } - } - public class SearchDelegate : UISearchBarDelegate - { - private readonly LoginSearchViewController _controller; - - public SearchDelegate(LoginSearchViewController controller) + public class SearchDelegate : UISearchBarDelegate { - _controller = controller; - } + private readonly LoginSearchViewController _controller; - public override void TextChanged(UISearchBar searchBar, string searchText) - { - System.Diagnostics.Debug.WriteLine("search text: " + searchText); + public SearchDelegate(LoginSearchViewController controller) + { + _controller = controller; + } + + public override void TextChanged(UISearchBar searchBar, string searchText) + { + System.Diagnostics.Debug.WriteLine("search text: " + searchText); + ((TableSource)_controller.TableView.Source).FilterResults(searchText, new CancellationToken()); + } } } } diff --git a/src/iOS.Core/Views/ExtensionTableSource.cs b/src/iOS.Core/Views/ExtensionTableSource.cs index 84fc7d016..a57c0172d 100644 --- a/src/iOS.Core/Views/ExtensionTableSource.cs +++ b/src/iOS.Core/Views/ExtensionTableSource.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Threading; using System.Threading.Tasks; using UIKit; using XLabs.Ioc; @@ -19,6 +20,7 @@ namespace Bit.iOS.Core.Views { private const string CellIdentifier = "TableCell"; + private IEnumerable _allItems = new List(); protected IEnumerable _tableItems = new List(); protected ICipherService _cipherService; protected ISettings _settings; @@ -39,14 +41,14 @@ namespace Bit.iOS.Core.Views { var combinedLogins = new List(); - if (urlFilter) + if(urlFilter) { var logins = await _cipherService.GetAllAsync(_context.UrlString); - if (logins?.Item1 != null) + if(logins?.Item1 != null) { combinedLogins.AddRange(logins.Item1); } - if (logins?.Item2 != null) + if(logins?.Item2 != null) { combinedLogins.AddRange(logins.Item2); } @@ -57,10 +59,31 @@ namespace Bit.iOS.Core.Views combinedLogins.AddRange(logins); } - _tableItems = combinedLogins.Select(s => new CipherViewModel(s)) + _allItems = combinedLogins.Select(s => new CipherViewModel(s)) .OrderBy(s => s.Name) .ThenBy(s => s.Username) .ToList() ?? new List(); + FilterResults(searchFilter, new CancellationToken()); + } + + public void FilterResults(string searchFilter, CancellationToken ct) + { + ct.ThrowIfCancellationRequested(); + + if(string.IsNullOrWhiteSpace(searchFilter)) + { + _tableItems = _allItems.ToList(); + } + else + { + searchFilter = searchFilter.ToLower(); + _tableItems = _allItems + .Where(s => s.Name.ToLower().Contains(searchFilter) || + (s.Username?.ToLower().Contains(searchFilter) ?? false) || + (s.Uris?.FirstOrDefault()?.Uri.ToLower().Contains(searchFilter) ?? false)) + .TakeWhile(s => !ct.IsCancellationRequested) + .ToArray(); + } } public IEnumerable TableItems { get; set; }