Add suggested passwords in AutoFill
This commit is contained in:
parent
156588bd93
commit
d4669bbfcb
4 changed files with 94 additions and 6 deletions
|
|
@ -12,20 +12,69 @@ import passKit
|
|||
class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
||||
var passwordTableEntries: [PasswordTableEntry]
|
||||
var filteredPasswordsTableEntries: [PasswordTableEntry]
|
||||
var suggestedPasswordsTableEntries: [PasswordTableEntry]
|
||||
var otherPasswordsTableEntries: [PasswordTableEntry]
|
||||
|
||||
var showSuggestion: Bool = false
|
||||
|
||||
init(entries: [PasswordTableEntry] = []) {
|
||||
passwordTableEntries = entries
|
||||
filteredPasswordsTableEntries = passwordTableEntries
|
||||
suggestedPasswordsTableEntries = []
|
||||
otherPasswordsTableEntries = []
|
||||
}
|
||||
|
||||
func numberOfSections(in tableView: UITableView) -> Int {
|
||||
if !showSuggestion {
|
||||
return 1
|
||||
} else {
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
filteredPasswordsTableEntries.count
|
||||
if !showSuggestion {
|
||||
return filteredPasswordsTableEntries.count
|
||||
}
|
||||
|
||||
if section == 0 {
|
||||
return suggestedPasswordsTableEntries.count
|
||||
} else {
|
||||
return otherPasswordsTableEntries.count
|
||||
}
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
||||
if suggestedPasswordsTableEntries.isEmpty {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !showSuggestion {
|
||||
return "All Passwords"
|
||||
}
|
||||
|
||||
if section == 0 {
|
||||
return "Suggested Passwords"
|
||||
} else {
|
||||
return "Other Passwords"
|
||||
}
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath) as! PasswordTableViewCell
|
||||
|
||||
let entry = filteredPasswordsTableEntries[indexPath.row]
|
||||
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]
|
||||
}
|
||||
cell.configure(with: entry)
|
||||
|
||||
return cell
|
||||
|
|
@ -34,9 +83,26 @@ class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
|||
func showTableEntries(matching text: String) {
|
||||
guard !text.isEmpty else {
|
||||
filteredPasswordsTableEntries = passwordTableEntries
|
||||
showSuggestion = true
|
||||
return
|
||||
}
|
||||
|
||||
filteredPasswordsTableEntries = passwordTableEntries.filter { $0.match(text) }
|
||||
showSuggestion = false
|
||||
}
|
||||
|
||||
func showTableEntriesWithSuggestion(matching keywords: [String]) {
|
||||
for entry in passwordTableEntries {
|
||||
var match = false
|
||||
for keyword in keywords {
|
||||
match = match || entry.match(keyword)
|
||||
}
|
||||
if match {
|
||||
suggestedPasswordsTableEntries.append(entry)
|
||||
} else {
|
||||
otherPasswordsTableEntries.append(entry)
|
||||
}
|
||||
}
|
||||
showSuggestion = !suggestedPasswordsTableEntries.isEmpty
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue