diff --git a/enzevalos_iphone/ListViewController.swift b/enzevalos_iphone/ListViewController.swift index e733f1e26e2e91bf805f4b66ce5aa7a359d57f68..a39c723fb3dfce3e4b8feb6b17509cfccf993b2d 100644 --- a/enzevalos_iphone/ListViewController.swift +++ b/enzevalos_iphone/ListViewController.swift @@ -68,6 +68,9 @@ class ListViewController: UITableViewController { } } var loading = false + + private let searchDelay = 0.5 + private var searchTimer: Timer? override func viewWillAppear(_ animated: Bool) { tableView.reloadData() @@ -105,63 +108,53 @@ class ListViewController: UITableViewController { deinit { print("===============|| ListViewController deinitialized ||===============") } - - func filterContentForSearchText(_ searchText: String, scope: Int = 0) { + + func startSearch(searchText: String, scope: Int = 0) { + if #available(iOS 10.0, *) { + if let searchBarTimer: Timer = self.searchTimer { + searchBarTimer.invalidate() + } + self.searchTimer = Timer.scheduledTimer(withTimeInterval: searchDelay, repeats: false, block: { _ in + self.filterContentForSearchText(searchText, scope: scope) + }) + } + } + + /** + - parameters: + - searchText: user input string to look for + - scope : 0 = subject: 1 = body: 2 = cc + to: 3 = all + */ + private func filterContentForSearchText(_ searchText: String, scope: Int = 0) { filteredMails = contact!.mails.filter { mail in - var returnValue = false - switch scope { - case 0: - if let subject = mail.subject { - returnValue = subject.lowercased().contains(searchText.lowercased()) - } - case 1: - if !returnValue && mail.decryptedBody != nil { - returnValue = mail.decryptedBody!.lowercased().contains(searchText.lowercased()) - } else if !returnValue && mail.body != nil { - returnValue = mail.body!.lowercased().contains(searchText.lowercased()) - } - case 2: - if !returnValue && mail.cc?.count > 0 { - if let result = mail.cc?.contains(where: { cc -> Bool in - if let mail = cc as? MailAddress { - return mail.mailAddress.contains(searchText.lowercased()) - } - return false - }) { - returnValue = result - } - } - if !returnValue && mail.getReceivers().count > 1 { - returnValue = mail.getReceivers().contains(where: { rec -> Bool in - return rec.mailAddress.contains(searchText.lowercased()) - }) - } - default: - if let subject = mail.subject { - returnValue = subject.lowercased().contains(searchText.lowercased()) - } - if !returnValue && mail.decryptedBody != nil { - returnValue = mail.decryptedBody!.lowercased().contains(searchText.lowercased()) - } else if !returnValue && mail.body != nil && !mail.isEncrypted { - returnValue = mail.body!.lowercased().contains(searchText.lowercased()) - } - if !returnValue && mail.cc?.count > 0 { - if let res = mail.cc?.contains(where: { cc -> Bool in - if let mail = cc as? MailAddress { - return mail.mailAddress.contains(searchText.lowercased()) - } - return false - }) { - returnValue = res - } - } - if !returnValue && mail.getReceivers().count > 1 { - returnValue = mail.getReceivers().contains(where: { rec -> Bool in - return rec.mailAddress.contains(searchText.lowercased()) - }) + // a big string that we search through, contains everything from the scopes we are searching in + var str = "" + + if scope == 0 || scope == 3 { + str.append(mail.subject ?? "") + } + if scope == 1 || scope == 3 { + if let decryptedBody = mail.decryptedBody { + str.append(decryptedBody) + } else if let body = mail.body { + str.append(body) } } - return returnValue + if scope == 2 || scope == 3 { + var receivers: [MailAddress] = [] + receivers.append(contentsOf: mail.getReceivers()) + receivers.append(contentsOf: mail.getCCs()) + + // build a string of all email addresses to input in the search function + + receivers.map({ addr -> String in + return addr.mailAddress + }).forEach({ addr in + str.append(addr) + }) + } + + return containsSearchTerms(content: str, searchText: searchText) } tableView.reloadData() @@ -259,13 +252,12 @@ class ListViewController: UITableViewController { extension ListViewController: UISearchResultsUpdating { func updateSearchResults(for searchController: UISearchController) { let searchBar = searchController.searchBar - let _ = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex] - filterContentForSearchText(searchController.searchBar.text!, scope: searchBar.selectedScopeButtonIndex) + startSearch(searchText: searchController.searchBar.text!, scope: searchBar.selectedScopeButtonIndex) } } extension ListViewController: UISearchBarDelegate { func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { - filterContentForSearchText(searchBar.text!, scope: selectedScope) + startSearch(searchText: searchBar.text!, scope: selectedScope) } } diff --git a/enzevalos_iphone/SearchHelper.swift b/enzevalos_iphone/SearchHelper.swift index 7a0150c4c308658a17a4f951e91f4ff44a79f814..022a6b37048fe4788c9209a46bbbc56ccacd9aef 100644 --- a/enzevalos_iphone/SearchHelper.swift +++ b/enzevalos_iphone/SearchHelper.swift @@ -7,6 +7,11 @@ // import Foundation + +/** + A collection of helper methods that are used for the different search bars + */ + /** Function to be used to find mails that contain the search terms. All terms (separated by spaces) need to be contained in the search text. - parameters: @@ -23,7 +28,26 @@ func containsSearchTerms ( content : String?, searchText: String) -> Bool { return false ///Case Mail has no body/subject } - let terms = searchText.lowercased().components(separatedBy : " ") + + var longterms : [String] = [] + var terms : [String] = [] + //Break String into substrings separated by quoatation marks + longterms = searchText.components(separatedBy: "\"") + var i = 0 + //even elements will be outside the quotation marks and need to be separated again + while (i < longterms.count) + { + if i % 2 == 0 + { + terms.append(contentsOf: longterms[i].lowercased().components(separatedBy: " ")) + } + else + { + terms.append(longterms[i]) + } + i+=1 + } + var found = true for t in terms {