2017-01-19 21:15:47 +08:00
|
|
|
//
|
|
|
|
|
// PasswordTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 18/1/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
import SwiftGit2
|
|
|
|
|
import Result
|
|
|
|
|
import SVProgressHUD
|
|
|
|
|
|
2017-01-23 12:48:20 +08:00
|
|
|
extension PasswordTableViewController: UISearchResultsUpdating {
|
|
|
|
|
func updateSearchResults(for searchController: UISearchController) {
|
|
|
|
|
filterContentForSearchText(searchText: searchController.searchBar.text!)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 21:15:47 +08:00
|
|
|
class PasswordTableViewController: UITableViewController {
|
|
|
|
|
private var passwordEntities: [PasswordEntity]?
|
2017-01-23 12:48:20 +08:00
|
|
|
var filteredPasswordEntities = [PasswordEntity]()
|
|
|
|
|
let searchController = UISearchController(searchResultsController: nil)
|
2017-01-22 01:42:36 +08:00
|
|
|
|
2017-01-19 21:15:47 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(PasswordTableViewController.actOnPasswordUpdatedNotification), name: NSNotification.Name(rawValue: "passwordUpdated"), object: nil)
|
2017-01-23 12:48:20 +08:00
|
|
|
searchController.searchResultsUpdater = self
|
|
|
|
|
searchController.dimsBackgroundDuringPresentation = false
|
|
|
|
|
definesPresentationContext = true
|
|
|
|
|
tableView.tableHeaderView = searchController.searchBar
|
|
|
|
|
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func filterContentForSearchText(searchText: String, scope: String = "All") {
|
|
|
|
|
filteredPasswordEntities = passwordEntities!.filter { password in
|
|
|
|
|
return password.name!.lowercased().contains(searchText.lowercased())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tableView.reloadData()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
|
super.viewWillAppear(animated)
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func actOnPasswordUpdatedNotification() {
|
|
|
|
|
passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
|
|
|
|
|
self.tableView.reloadData()
|
|
|
|
|
print("actOnPasswordUpdatedNotification")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2017-01-23 12:48:20 +08:00
|
|
|
if searchController.isActive && searchController.searchBar.text != "" {
|
|
|
|
|
return filteredPasswordEntities.count
|
|
|
|
|
}
|
2017-01-19 21:15:47 +08:00
|
|
|
return passwordEntities!.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath)
|
2017-01-23 12:48:20 +08:00
|
|
|
let password: PasswordEntity
|
|
|
|
|
if searchController.isActive && searchController.searchBar.text != "" {
|
|
|
|
|
password = filteredPasswordEntities[indexPath.row]
|
|
|
|
|
} else {
|
|
|
|
|
password = passwordEntities![indexPath.row]
|
|
|
|
|
}
|
|
|
|
|
cell.textLabel?.text = password.name
|
2017-01-19 21:15:47 +08:00
|
|
|
return cell
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-22 01:42:36 +08:00
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
|
if segue.identifier == "showPasswordDetail" {
|
|
|
|
|
if let viewController = segue.destination as? PasswordDetailViewController {
|
2017-01-23 12:48:20 +08:00
|
|
|
let selectedIndex = self.tableView.indexPath(for: sender as! UITableViewCell)!
|
|
|
|
|
let password: PasswordEntity
|
|
|
|
|
if searchController.isActive && searchController.searchBar.text != "" {
|
|
|
|
|
password = filteredPasswordEntities[selectedIndex.row]
|
|
|
|
|
} else {
|
|
|
|
|
password = passwordEntities![selectedIndex.row]
|
|
|
|
|
}
|
|
|
|
|
viewController.passwordEntity = password
|
2017-01-22 01:42:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|