passforios/pass/PasswordsTableViewController.swift

111 lines
4.5 KiB
Swift
Raw Normal View History

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 Result
import SVProgressHUD
2017-01-23 12:48:20 +08:00
2017-01-23 13:43:06 +08:00
class PasswordsTableViewController: UITableViewController {
2017-01-19 21:15:47 +08:00
private var passwordEntities: [PasswordEntity]?
2017-01-23 12:48:20 +08:00
var filteredPasswordEntities = [PasswordEntity]()
let searchController = UISearchController(searchResultsController: nil)
2017-01-23 13:43:06 +08:00
2017-01-23 16:29:36 +08:00
@IBAction func refreshPasswords(_ sender: UIBarButtonItem) {
2017-01-23 17:36:10 +08:00
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.show(withStatus: "Pull Remote Repository")
2017-01-23 16:29:36 +08:00
DispatchQueue.global(qos: .userInitiated).async {
2017-01-23 17:36:10 +08:00
if PasswordStore.shared.pullRepository(transferProgressBlock: {(git_transfer_progress, stop) in
DispatchQueue.main.async {
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository")
}
}) {
DispatchQueue.main.async {
SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1)
}
2017-01-23 16:29:36 +08:00
print("pull success")
self.passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
self.tableView.reloadData()
}
}
}
2017-01-19 21:15:47 +08:00
override func viewDidLoad() {
super.viewDidLoad()
passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
2017-01-23 13:43:06 +08:00
NotificationCenter.default.addObserver(self, selector: #selector(PasswordsTableViewController.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)
2017-01-23 12:48:20 +08:00
}
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")
}
2017-01-23 13:43:06 +08:00
2017-01-19 21:15:47 +08:00
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
}
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-23 13:12:22 +08:00
viewController.navigationItem.title = password.name
}
}
}
2017-01-19 21:15:47 +08:00
}
2017-01-23 16:29:36 +08:00
extension PasswordsTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}