1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-23 03:33:59 +00:00
Files
mobile/src/iOS.Core/Views/ExtensionSearchDelegate.cs
Kyle Spearrin f416f95b77 reuse code
2018-09-24 16:11:54 -04:00

49 lines
1.6 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using Foundation;
using UIKit;
namespace Bit.iOS.Core.Views
{
public class ExtensionSearchDelegate : UISearchBarDelegate
{
private readonly UITableView _tableView;
private CancellationTokenSource _filterResultsCancellationTokenSource;
public ExtensionSearchDelegate(UITableView tableView)
{
_tableView = tableView;
}
public override void TextChanged(UISearchBar searchBar, string searchText)
{
var cts = new CancellationTokenSource();
Task.Run(() =>
{
NSRunLoop.Main.BeginInvokeOnMainThread(async () =>
{
if(!string.IsNullOrWhiteSpace(searchText))
{
await Task.Delay(300);
if(searchText != searchBar.Text)
{
return;
}
else
{
_filterResultsCancellationTokenSource?.Cancel();
}
}
try
{
((ExtensionTableSource)_tableView.Source).FilterResults(searchText, cts.Token);
_tableView.ReloadData();
}
catch(OperationCanceledException) { }
_filterResultsCancellationTokenSource = cts;
});
}, cts.Token);
}
}
}