2020-12-31 21:46:50 -08:00
|
|
|
//
|
|
|
|
|
// PasswordsTableDataSource.swift
|
|
|
|
|
// passAutoFillExtension
|
|
|
|
|
//
|
|
|
|
|
// Created by Sun, Mingshen on 12/31/20.
|
|
|
|
|
// Copyright © 2020 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
import passKit
|
|
|
|
|
|
|
|
|
|
class PasswordsTableDataSource: NSObject, UITableViewDataSource {
|
|
|
|
|
var passwordTableEntries: [PasswordTableEntry]
|
|
|
|
|
var filteredPasswordsTableEntries: [PasswordTableEntry]
|
2021-01-02 22:13:48 -08:00
|
|
|
var suggestedPasswordsTableEntries: [PasswordTableEntry]
|
|
|
|
|
var otherPasswordsTableEntries: [PasswordTableEntry]
|
|
|
|
|
|
|
|
|
|
var showSuggestion: Bool = false
|
2020-12-31 21:46:50 -08:00
|
|
|
|
|
|
|
|
init(entries: [PasswordTableEntry] = []) {
|
|
|
|
|
passwordTableEntries = entries
|
|
|
|
|
filteredPasswordsTableEntries = passwordTableEntries
|
2021-01-02 22:13:48 -08:00
|
|
|
suggestedPasswordsTableEntries = []
|
|
|
|
|
otherPasswordsTableEntries = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
|
if !showSuggestion {
|
|
|
|
|
return 1
|
|
|
|
|
} else {
|
|
|
|
|
return 2
|
|
|
|
|
}
|
2020-12-31 21:46:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2021-01-02 22:13:48 -08:00
|
|
|
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"
|
|
|
|
|
}
|
2020-12-31 21:46:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath) as! PasswordTableViewCell
|
|
|
|
|
|
2021-01-02 22:13:48 -08:00
|
|
|
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]
|
|
|
|
|
}
|
2020-12-31 21:46:50 -08:00
|
|
|
cell.configure(with: entry)
|
|
|
|
|
|
|
|
|
|
return cell
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func showTableEntries(matching text: String) {
|
|
|
|
|
guard !text.isEmpty else {
|
|
|
|
|
filteredPasswordsTableEntries = passwordTableEntries
|
2021-01-02 22:13:48 -08:00
|
|
|
showSuggestion = true
|
2020-12-31 21:46:50 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filteredPasswordsTableEntries = passwordTableEntries.filter { $0.match(text) }
|
2021-01-02 22:13:48 -08:00
|
|
|
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
|
2020-12-31 21:46:50 -08:00
|
|
|
}
|
|
|
|
|
}
|