Update Safari extension search logic #138

- Match the entered text in each pathname levels
This commit is contained in:
Yishi Lin 2017-10-28 15:32:30 +08:00
parent 3380f41855
commit 15ed7aaf2f
2 changed files with 22 additions and 9 deletions

View file

@ -12,10 +12,14 @@ import passKit
fileprivate class PasswordsTableEntry : NSObject { fileprivate class PasswordsTableEntry : NSObject {
var title: String var title: String
var categoryText: String
var categoryArray: [String]
var passwordEntity: PasswordEntity? var passwordEntity: PasswordEntity?
init(title: String, passwordEntity: PasswordEntity?) { init(_ entity: PasswordEntity) {
self.title = title self.title = entity.name!
self.passwordEntity = passwordEntity self.categoryText = entity.getCategoryText()
self.categoryArray = entity.getCategoryArray()
self.passwordEntity = entity
} }
} }
@ -46,7 +50,7 @@ class ExtensionViewController: UIViewController, UITableViewDataSource, UITableV
var passwordEntities = [PasswordEntity]() var passwordEntities = [PasswordEntity]()
passwordEntities = self.passwordStore.fetchPasswordEntityCoreData(withDir: false) passwordEntities = self.passwordStore.fetchPasswordEntityCoreData(withDir: false)
passwordsTableEntries = passwordEntities.map { passwordsTableEntries = passwordEntities.map {
PasswordsTableEntry(title: $0.name!, passwordEntity: $0) PasswordsTableEntry($0)
} }
} }
@ -141,7 +145,7 @@ class ExtensionViewController: UIViewController, UITableViewDataSource, UITableV
} }
cell.accessoryType = .none cell.accessoryType = .none
cell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .footnote) cell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .footnote)
cell.detailTextLabel?.text = entry.passwordEntity?.getCategoryText() cell.detailTextLabel?.text = entry.categoryText
return cell return cell
} }
@ -241,10 +245,15 @@ class ExtensionViewController: UIViewController, UITableViewDataSource, UITableV
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let searchText = searchBar.text, searchText.isEmpty == false { if let searchText = searchBar.text, searchText.isEmpty == false {
let searchTextLowerCased = searchText.lowercased()
filteredPasswordsTableEntries = passwordsTableEntries.filter { entry in filteredPasswordsTableEntries = passwordsTableEntries.filter { entry in
let entryTitle = entry.title.lowercased() var matched = false
return entryTitle.contains(searchTextLowerCased) || searchTextLowerCased.contains(entryTitle) matched = matched || entry.title.range(of: searchText, options: .caseInsensitive) != nil
matched = matched || searchText.range(of: entry.title, options: .caseInsensitive) != nil
entry.categoryArray.forEach({ (category) in
matched = matched || category.range(of: searchText, options: .caseInsensitive) != nil
matched = matched || searchText.range(of: category, options: .caseInsensitive) != nil
})
return matched
} }
searchActive = true searchActive = true
} else { } else {

View file

@ -22,6 +22,10 @@ extension PasswordEntity {
} }
public func getCategoryText() -> String { public func getCategoryText() -> String {
return getCategoryArray().joined(separator: " > ")
}
public func getCategoryArray() -> [String] {
var parentEntity = parent var parentEntity = parent
var passwordCategoryArray: [String] = [] var passwordCategoryArray: [String] = []
while parentEntity != nil { while parentEntity != nil {
@ -29,7 +33,7 @@ extension PasswordEntity {
parentEntity = parentEntity!.parent parentEntity = parentEntity!.parent
} }
passwordCategoryArray.reverse() passwordCategoryArray.reverse()
return passwordCategoryArray.joined(separator: " > ") return passwordCategoryArray
} }
public func getURL() -> URL? { public func getURL() -> URL? {