Polish and refactor PasswordsTableDataSource
This commit is contained in:
parent
8e66b2905b
commit
a706f90d0d
2 changed files with 26 additions and 33 deletions
|
|
@ -68,17 +68,7 @@ extension PasswordsViewController: UISearchBarDelegate {
|
|||
extension PasswordsViewController: UITableViewDelegate {
|
||||
func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
tableView.deselectRow(at: indexPath, animated: true)
|
||||
var entry: PasswordTableEntry!
|
||||
if dataSource.showSuggestion {
|
||||
if indexPath.section == 0 {
|
||||
entry = dataSource.suggestedPasswordsTableEntries[indexPath.row]
|
||||
} else {
|
||||
entry = dataSource.otherPasswordsTableEntries[indexPath.row]
|
||||
}
|
||||
} else {
|
||||
entry = dataSource.filteredPasswordsTableEntries[indexPath.row]
|
||||
}
|
||||
|
||||
let entry = dataSource.tableEntry(at: indexPath)
|
||||
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
|
||||
self.selectionDelegate?.selected(password: entry)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,15 +33,7 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
|||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
if !showSuggestion {
|
||||
return filteredPasswordsTableEntries.count
|
||||
}
|
||||
|
||||
if section == 0 {
|
||||
return suggestedPasswordsTableEntries.count
|
||||
} else {
|
||||
return otherPasswordsTableEntries.count
|
||||
}
|
||||
tableEntries(at: section).count
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
||||
|
|
@ -63,18 +55,7 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
|||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath) as! PasswordTableViewCell
|
||||
|
||||
var entry: PasswordTableEntry!
|
||||
if !showSuggestion {
|
||||
entry = filteredPasswordsTableEntries[indexPath.row]
|
||||
cell.configure(with: entry)
|
||||
return cell
|
||||
}
|
||||
|
||||
if indexPath.section == 0 {
|
||||
entry = suggestedPasswordsTableEntries[indexPath.row]
|
||||
} else {
|
||||
entry = otherPasswordsTableEntries[indexPath.row]
|
||||
}
|
||||
let entry = tableEntry(at: indexPath)
|
||||
cell.configure(with: entry)
|
||||
|
||||
return cell
|
||||
|
|
@ -83,7 +64,7 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
|||
func showTableEntries(matching text: String) {
|
||||
guard !text.isEmpty else {
|
||||
filteredPasswordsTableEntries = passwordTableEntries
|
||||
showSuggestion = true
|
||||
showSuggestion = !suggestedPasswordsTableEntries.isEmpty
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -92,6 +73,12 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
|||
}
|
||||
|
||||
func showTableEntriesWithSuggestion(matching text: String) {
|
||||
guard !text.isEmpty else {
|
||||
filteredPasswordsTableEntries = passwordTableEntries
|
||||
showSuggestion = false
|
||||
return
|
||||
}
|
||||
|
||||
for entry in passwordTableEntries {
|
||||
if entry.match(text) {
|
||||
suggestedPasswordsTableEntries.append(entry)
|
||||
|
|
@ -101,4 +88,20 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
|||
}
|
||||
showSuggestion = !suggestedPasswordsTableEntries.isEmpty
|
||||
}
|
||||
|
||||
func tableEntry(at indexPath: IndexPath) -> PasswordTableEntry {
|
||||
tableEntries(at: indexPath.section)[indexPath.row]
|
||||
}
|
||||
|
||||
func tableEntries(at section: Int) -> [PasswordTableEntry] {
|
||||
if showSuggestion {
|
||||
if section == 0 {
|
||||
return suggestedPasswordsTableEntries
|
||||
} else {
|
||||
return otherPasswordsTableEntries
|
||||
}
|
||||
} else {
|
||||
return filteredPasswordsTableEntries
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue