Skip to content
Snippets Groups Projects
Commit 6c7af886 authored by lazarog98's avatar lazarog98
Browse files

Add delay when searching

parent 63da3385
No related branches found
No related tags found
1 merge request!27Resolve "Improve searching"
......@@ -39,6 +39,7 @@ class InboxViewController: UITableViewController, InboxCellDelegator {
}
}
}
var searchBarTimer: Timer?
@IBOutlet weak var lastUpdateButton: UIBarButtonItem!
var lastUpdateLabel = UILabel(frame: CGRect.zero)
......@@ -248,7 +249,20 @@ class InboxViewController: UITableViewController, InboxCellDelegator {
return searchController.isActive && !searchBarIsEmpty
}
func filterContentForSearchText(_ searchText: String, scope: Int = 0) {
/// searches a given string with a delay so that the entire client doesn't lag if the inbox is very big
func startSearch(searchText: String, scope: Int = 0) {
if #available(iOS 10.0, *) {
if let searchBarTimer: Timer = self.searchBarTimer {
searchBarTimer.invalidate()
}
self.searchBarTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false, block: { _ in
self.filterContentForSearchText(searchText, scope: scope)
})
}
}
private func filterContentForSearchText(_ searchText: String, scope: Int = 0) {
var records = [KeyRecord]()
if scope == 0 || scope == 3 {
records += folder.records.filter({ (record: KeyRecord) -> Bool in
......@@ -286,13 +300,13 @@ extension InboxViewController: UISearchResultsUpdating {
// https://www.raywenderlich.com/157864/uisearchcontroller-tutorial-getting-started
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!, scope: searchController.searchBar.selectedScopeButtonIndex)
startSearch(searchText: searchController.searchBar.text!, scope: searchController.searchBar.selectedScopeButtonIndex)
}
}
extension InboxViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchBar.text!, scope: selectedScope)
startSearch(searchText: searchBar.text!, scope: selectedScope)
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment