mirror of
https://github.com/bitwarden/mobile
synced 2025-12-23 03:33:59 +00:00
reuse code
This commit is contained in:
49
src/iOS.Core/Views/ExtensionSearchDelegate.cs
Normal file
49
src/iOS.Core/Views/ExtensionSearchDelegate.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user