passforios/passAutoFillExtension/Services/PasswordsTableDataSource.swift

108 lines
3.1 KiB
Swift
Raw Normal View History

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 {
tableEntries(at: section).count
2021-01-02 22:13:48 -08:00
}
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
let entry = tableEntry(at: indexPath)
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
showSuggestion = !suggestedPasswordsTableEntries.isEmpty
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
}
2021-01-03 22:01:24 -08:00
func showTableEntriesWithSuggestion(matching text: String) {
guard !text.isEmpty else {
filteredPasswordsTableEntries = passwordTableEntries
showSuggestion = false
return
}
2021-01-02 22:13:48 -08:00
for entry in passwordTableEntries {
2021-01-03 22:01:24 -08:00
if entry.match(text) {
2021-01-02 22:13:48 -08:00
suggestedPasswordsTableEntries.append(entry)
} else {
otherPasswordsTableEntries.append(entry)
}
}
showSuggestion = !suggestedPasswordsTableEntries.isEmpty
2020-12-31 21:46:50 -08:00
}
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
}
}
2020-12-31 21:46:50 -08:00
}