passforios/passAutoFillExtension/Services/PasswordsTableDataSource.swift

99 lines
3 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 passKit
2021-01-31 13:17:37 +01:00
import UIKit
2020-12-31 21:46:50 -08:00
class PasswordsTableDataSource: NSObject, UITableViewDataSource {
var passwordTableEntries: [PasswordTableEntry]
var filteredPasswordsTableEntries: [PasswordTableEntry]
2021-01-02 22:13:48 -08:00
var suggestedPasswordsTableEntries: [PasswordTableEntry]
var otherPasswordsTableEntries: [PasswordTableEntry]
2021-03-06 15:26:42 +01:00
var showSuggestion = false
2020-12-31 21:46:50 -08:00
init(entries: [PasswordTableEntry] = []) {
2021-01-31 13:17:37 +01:00
self.passwordTableEntries = entries
self.filteredPasswordsTableEntries = passwordTableEntries
self.suggestedPasswordsTableEntries = []
self.otherPasswordsTableEntries = []
2021-01-02 22:13:48 -08:00
}
2021-01-31 13:17:37 +01:00
func numberOfSections(in _: UITableView) -> Int {
2021-01-31 13:34:37 +01:00
!showSuggestion ? 1 : 2
2020-12-31 21:46:50 -08:00
}
2021-01-31 13:17:37 +01:00
func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
tableEntries(at: section).count
2021-01-02 22:13:48 -08:00
}
2021-01-31 13:17:37 +01:00
func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
2021-01-02 22:13:48 -08:00
if suggestedPasswordsTableEntries.isEmpty {
return nil
}
if !showSuggestion {
return "All Passwords"
}
if section == 0 {
return "Suggested Passwords"
}
2021-01-31 13:34:37 +01:00
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
}
2021-08-26 23:12:13 +02:00
filteredPasswordsTableEntries = passwordTableEntries.filter { $0.matches(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-08-26 23:12:13 +02:00
if entry.matches(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
}
2021-01-31 13:34:37 +01:00
return otherPasswordsTableEntries
}
2021-01-31 13:34:37 +01:00
return filteredPasswordsTableEntries
}
2020-12-31 21:46:50 -08:00
}