passforios/passAutoFillExtension/Controllers/PasswordsViewController.swift

92 lines
2.9 KiB
Swift
Raw Normal View History

2020-12-31 21:46:50 -08:00
//
// PasswordsViewController.swift
// passAutoFillExtension
//
// Created by Sun, Mingshen on 12/31/20.
// Copyright © 2020 Bob Sun. All rights reserved.
//
import UIKit
import AuthenticationServices
import passKit
class PasswordsViewController: UIViewController {
@IBOutlet var tableView: UITableView!
var dataSource: PasswordsTableDataSource!
weak var selectionDelegate: PasswordSelectionDelegate?
2021-01-01 12:18:17 -08:00
lazy var searchController: UISearchController = {
2020-12-31 21:46:50 -08:00
let uiSearchController = UISearchController(searchResultsController: nil)
uiSearchController.searchBar.isTranslucent = true
uiSearchController.obscuresBackgroundDuringPresentation = false
uiSearchController.searchBar.sizeToFit()
if #available(iOS 13.0, *) {
uiSearchController.searchBar.searchTextField.clearButtonMode = .whileEditing
}
return uiSearchController
2021-01-01 12:18:17 -08:00
}()
2020-12-31 21:46:50 -08:00
2021-01-02 22:13:48 -08:00
lazy var searchBar: UISearchBar = {
self.searchController.searchBar
}()
2020-12-31 21:46:50 -08:00
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
2021-01-02 22:13:48 -08:00
searchBar.delegate = self
2020-12-31 21:46:50 -08:00
tableView.delegate = self
tableView.dataSource = dataSource
}
2021-01-03 22:01:24 -08:00
func showPasswordsWithSuggstion(matching text: String) {
dataSource.showTableEntriesWithSuggestion(matching: text)
2021-01-02 22:13:48 -08:00
tableView.reloadData()
}
2020-12-31 21:46:50 -08:00
@IBAction
private func cancel(_: AnyObject?) {
self.extensionContext?.cancelRequest(withError: NSError(domain: ASExtensionErrorDomain, code: ASExtensionError.userCanceled.rawValue))
self.dismiss(animated: true)
}
}
extension PasswordsViewController: UISearchBarDelegate {
func searchBar(_: UISearchBar, textDidChange searchText: String) {
dataSource.showTableEntries(matching: searchText)
tableView.reloadData()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
func searchBarCancelButtonClicked(_: UISearchBar) {
dataSource.showTableEntries(matching: "")
tableView.reloadData()
}
}
extension PasswordsViewController: UITableViewDelegate {
func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
var entry: PasswordTableEntry!
if dataSource.showSuggestion {
if indexPath.section == 0 {
entry = dataSource.suggestedPasswordsTableEntries[indexPath.row]
} else {
entry = dataSource.otherPasswordsTableEntries[indexPath.row]
}
} else {
entry = dataSource.filteredPasswordsTableEntries[indexPath.row]
}
2020-12-31 21:46:50 -08:00
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
self.selectionDelegate?.selected(password: entry)
}
}