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 {
|
extension PasswordsViewController: UITableViewDelegate {
|
||||||
func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
|
func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||||
tableView.deselectRow(at: indexPath, animated: true)
|
tableView.deselectRow(at: indexPath, animated: true)
|
||||||
var entry: PasswordTableEntry!
|
let entry = dataSource.tableEntry(at: indexPath)
|
||||||
if dataSource.showSuggestion {
|
|
||||||
if indexPath.section == 0 {
|
|
||||||
entry = dataSource.suggestedPasswordsTableEntries[indexPath.row]
|
|
||||||
} else {
|
|
||||||
entry = dataSource.otherPasswordsTableEntries[indexPath.row]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
entry = dataSource.filteredPasswordsTableEntries[indexPath.row]
|
|
||||||
}
|
|
||||||
|
|
||||||
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
|
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
|
||||||
self.selectionDelegate?.selected(password: entry)
|
self.selectionDelegate?.selected(password: entry)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,15 +33,7 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||||
if !showSuggestion {
|
tableEntries(at: section).count
|
||||||
return filteredPasswordsTableEntries.count
|
|
||||||
}
|
|
||||||
|
|
||||||
if section == 0 {
|
|
||||||
return suggestedPasswordsTableEntries.count
|
|
||||||
} else {
|
|
||||||
return otherPasswordsTableEntries.count
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
||||||
|
|
@ -63,18 +55,7 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
||||||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||||
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath) as! PasswordTableViewCell
|
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath) as! PasswordTableViewCell
|
||||||
|
|
||||||
var entry: PasswordTableEntry!
|
let entry = tableEntry(at: indexPath)
|
||||||
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]
|
|
||||||
}
|
|
||||||
cell.configure(with: entry)
|
cell.configure(with: entry)
|
||||||
|
|
||||||
return cell
|
return cell
|
||||||
|
|
@ -83,7 +64,7 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
||||||
func showTableEntries(matching text: String) {
|
func showTableEntries(matching text: String) {
|
||||||
guard !text.isEmpty else {
|
guard !text.isEmpty else {
|
||||||
filteredPasswordsTableEntries = passwordTableEntries
|
filteredPasswordsTableEntries = passwordTableEntries
|
||||||
showSuggestion = true
|
showSuggestion = !suggestedPasswordsTableEntries.isEmpty
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -92,6 +73,12 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
func showTableEntriesWithSuggestion(matching text: String) {
|
func showTableEntriesWithSuggestion(matching text: String) {
|
||||||
|
guard !text.isEmpty else {
|
||||||
|
filteredPasswordsTableEntries = passwordTableEntries
|
||||||
|
showSuggestion = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for entry in passwordTableEntries {
|
for entry in passwordTableEntries {
|
||||||
if entry.match(text) {
|
if entry.match(text) {
|
||||||
suggestedPasswordsTableEntries.append(entry)
|
suggestedPasswordsTableEntries.append(entry)
|
||||||
|
|
@ -101,4 +88,20 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
||||||
}
|
}
|
||||||
showSuggestion = !suggestedPasswordsTableEntries.isEmpty
|
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