Fix search issues

This commit is contained in:
Mingshen Sun 2021-01-17 20:47:52 -08:00
parent 68077bf04c
commit 8afc40a5a1
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
2 changed files with 23 additions and 17 deletions

View file

@ -76,7 +76,7 @@ class PasswordNavigationViewController: UIViewController {
} }
private func configureSearchBar() { private func configureSearchBar() {
if Defaults.isShowFolderOn, !isRootViewController() { if Defaults.isShowFolderOn {
searchBar.scopeButtonTitles = SearchBarScope.allCases.map(\.localizedName) searchBar.scopeButtonTitles = SearchBarScope.allCases.map(\.localizedName)
} else { } else {
searchBar.scopeButtonTitles = nil searchBar.scopeButtonTitles = nil
@ -84,16 +84,25 @@ class PasswordNavigationViewController: UIViewController {
} }
private func configureTableView(in dir: PasswordEntity?) { private func configureTableView(in dir: PasswordEntity?) {
let passwordTableEntries = fetchPasswordTableEntries(in: dir) configureTableViewDataSource(in: dir, isShowFolder: Defaults.isShowFolderOn)
dataSource = PasswordNavigationDataSource(entries: passwordTableEntries)
tableView.addGestureRecognizer(gestureRecognizer) tableView.addGestureRecognizer(gestureRecognizer)
tableView.dataSource = dataSource
tableView.delegate = self tableView.delegate = self
let atribbutedTitle = "LastSynced".localize() + ": \(PasswordStore.shared.lastSyncedTimeString)" let atribbutedTitle = "LastSynced".localize() + ": \(PasswordStore.shared.lastSyncedTimeString)"
refreshControl.attributedTitle = NSAttributedString(string: atribbutedTitle) refreshControl.attributedTitle = NSAttributedString(string: atribbutedTitle)
tableView.refreshControl = refreshControl tableView.refreshControl = refreshControl
} }
private func configureTableViewDataSource(in dir: PasswordEntity?, isShowFolder: Bool) {
var passwordTableEntries: [PasswordTableEntry]
if isShowFolder {
passwordTableEntries = PasswordStore.shared.fetchPasswordEntityCoreData(parent: dir).compactMap { PasswordTableEntry($0) }
} else {
passwordTableEntries = PasswordStore.shared.fetchPasswordEntityCoreData(withDir: false).compactMap { PasswordTableEntry($0) }
}
dataSource = PasswordNavigationDataSource(entries: passwordTableEntries)
tableView.dataSource = dataSource
}
private func configureTabBarItem() { private func configureTabBarItem() {
guard let tabBarItem = navigationController?.tabBarItem else { guard let tabBarItem = navigationController?.tabBarItem else {
return return
@ -107,14 +116,6 @@ class PasswordNavigationViewController: UIViewController {
} }
} }
private func fetchPasswordTableEntries(in dir: PasswordEntity? = nil) -> [PasswordTableEntry] {
if Defaults.isShowFolderOn {
return PasswordStore.shared.fetchPasswordEntityCoreData(parent: dir).compactMap { PasswordTableEntry($0) }
} else {
return PasswordStore.shared.fetchPasswordEntityCoreData(withDir: false).compactMap { PasswordTableEntry($0) }
}
}
private func configureNavigationItem() { private func configureNavigationItem() {
if isRootViewController() { if isRootViewController() {
navigationItem.largeTitleDisplayMode = .automatic navigationItem.largeTitleDisplayMode = .automatic
@ -321,6 +322,7 @@ extension PasswordNavigationViewController {
configureNavigationItem() configureNavigationItem()
configureTabBarItem() configureTabBarItem()
configureNavigationBar() configureNavigationBar()
configureSearchBar()
} }
} }
@ -332,9 +334,9 @@ extension PasswordNavigationViewController: UISearchBarDelegate {
func activateSearch(_ selectedScope: Int?) { func activateSearch(_ selectedScope: Int?) {
if selectedScope == SearchBarScope.all.rawValue { if selectedScope == SearchBarScope.all.rawValue {
configureTableView(in: nil) configureTableViewDataSource(in: nil, isShowFolder: false)
} else { } else {
configureTableView(in: parentPasswordEntity) configureTableViewDataSource(in: parentPasswordEntity, isShowFolder: true)
} }
dataSource?.isSearchActive = true dataSource?.isSearchActive = true
tableView.reloadData() tableView.reloadData()
@ -345,7 +347,7 @@ extension PasswordNavigationViewController: UISearchBarDelegate {
} }
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
if Defaults.isShowFolderOn, Defaults.searchDefault == .all { if Defaults.searchDefault == .all {
searchBar.selectedScopeButtonIndex = SearchBarScope.all.rawValue searchBar.selectedScopeButtonIndex = SearchBarScope.all.rawValue
} else { } else {
searchBar.selectedScopeButtonIndex = SearchBarScope.current.rawValue searchBar.selectedScopeButtonIndex = SearchBarScope.current.rawValue

View file

@ -11,8 +11,12 @@ import SwiftyUserDefaults
public extension PasswordEntity { public extension PasswordEntity {
var nameWithCategory: String { var nameWithCategory: String {
if let path = path, path.hasSuffix(".gpg") { if let path = path {
return String(path.prefix(upTo: path.index(path.endIndex, offsetBy: -4))) if path.hasSuffix(".gpg") {
return String(path.prefix(upTo: path.index(path.endIndex, offsetBy: -4)))
} else {
return path
}
} }
return "" return ""
} }