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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,6 @@ namespace Bit.iOS.Core.Views
|
||||
private const string CellIdentifier = "TableCell";
|
||||
|
||||
private IEnumerable<CipherViewModel> _allItems = new List<CipherViewModel>();
|
||||
protected IEnumerable<CipherViewModel> _tableItems = new List<CipherViewModel>();
|
||||
protected ICipherService _cipherService;
|
||||
protected ISettings _settings;
|
||||
private bool _accessPremium;
|
||||
@@ -37,6 +36,8 @@ namespace Bit.iOS.Core.Views
|
||||
_controller = controller;
|
||||
}
|
||||
|
||||
public IEnumerable<CipherViewModel> Items { get; private set; }
|
||||
|
||||
public async Task LoadItemsAsync(bool urlFilter = true, string searchFilter = null)
|
||||
{
|
||||
var combinedLogins = new List<Cipher>();
|
||||
@@ -74,12 +75,12 @@ namespace Bit.iOS.Core.Views
|
||||
|
||||
if(string.IsNullOrWhiteSpace(searchFilter))
|
||||
{
|
||||
_tableItems = _allItems.ToList();
|
||||
Items = _allItems.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
searchFilter = searchFilter.ToLower();
|
||||
_tableItems = _allItems
|
||||
Items = _allItems
|
||||
.Where(s => s.Name.ToLower().Contains(searchFilter) ||
|
||||
(s.Username?.ToLower().Contains(searchFilter) ?? false) ||
|
||||
(s.Uris?.FirstOrDefault()?.Uri.ToLower().Contains(searchFilter) ?? false))
|
||||
@@ -92,12 +93,12 @@ namespace Bit.iOS.Core.Views
|
||||
|
||||
public override nint RowsInSection(UITableView tableview, nint section)
|
||||
{
|
||||
return _tableItems == null || _tableItems.Count() == 0 ? 1 : _tableItems.Count();
|
||||
return Items == null || Items.Count() == 0 ? 1 : Items.Count();
|
||||
}
|
||||
|
||||
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
|
||||
{
|
||||
if(_tableItems == null || _tableItems.Count() == 0)
|
||||
if(Items == null || Items.Count() == 0)
|
||||
{
|
||||
var noDataCell = new UITableViewCell(UITableViewCellStyle.Default, "NoDataCell");
|
||||
noDataCell.TextLabel.Text = AppResources.NoItemsTap;
|
||||
@@ -121,17 +122,17 @@ namespace Bit.iOS.Core.Views
|
||||
|
||||
public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
|
||||
{
|
||||
if(_tableItems == null || _tableItems.Count() == 0 || cell == null)
|
||||
if(Items == null || Items.Count() == 0 || cell == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var item = _tableItems.ElementAt(indexPath.Row);
|
||||
var item = Items.ElementAt(indexPath.Row);
|
||||
cell.TextLabel.Text = item.Name;
|
||||
cell.DetailTextLabel.Text = item.Username;
|
||||
}
|
||||
|
||||
protected string GetTotp(CipherViewModel item)
|
||||
public string GetTotp(CipherViewModel item)
|
||||
{
|
||||
string totp = null;
|
||||
if(_accessPremium)
|
||||
|
||||
@@ -71,6 +71,7 @@
|
||||
<Compile Include="Views\SliderTableViewCell.cs" />
|
||||
<Compile Include="Views\SwitchTableViewCell.cs" />
|
||||
<Compile Include="Views\FormEntryTableViewCell.cs" />
|
||||
<Compile Include="Views\ExtensionSearchDelegate.cs" />
|
||||
<Compile Include="Views\Toast.cs" />
|
||||
<Compile Include="Models\CipherViewModel.cs" />
|
||||
<Compile Include="Utilities\ASHelpers.cs" />
|
||||
|
||||
Reference in New Issue
Block a user