passforios/pass/Controllers/PasswordDetailTableViewController.swift

202 lines
9 KiB
Swift
Raw Normal View History

2017-02-02 21:04:31 +08:00
//
// PasswordDetailTableViewController.swift
// pass
//
// Created by Mingshen Sun on 2/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
2017-02-09 13:17:11 +08:00
import FavIcon
2017-02-02 21:04:31 +08:00
2017-02-05 00:35:23 +08:00
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate {
2017-02-02 21:04:31 +08:00
var passwordEntity: PasswordEntity?
2017-02-06 21:53:54 +08:00
var passwordCategoryEntities: [PasswordCategoryEntity]?
var passwordCategoryText = ""
2017-02-06 14:28:57 +08:00
var password = Password()
2017-02-09 13:17:11 +08:00
var passwordImage: UIImage?
2017-02-02 21:04:31 +08:00
2017-02-04 20:38:40 +08:00
struct TableCell {
2017-02-02 21:04:31 +08:00
var title: String
var content: String
2017-02-09 15:47:42 +08:00
init() {
title = ""
content = ""
}
init(title: String, content: String) {
self.title = title
self.content = content
}
2017-02-02 21:04:31 +08:00
}
2017-02-04 20:38:40 +08:00
struct TableSection {
var title: String
var item: Array<TableCell>
}
var tableData = Array<TableSection>()
2017-02-02 21:04:31 +08:00
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell")
tableView.register(UINib(nibName: "PasswordDetailTitleTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordDetailTitleTableViewCell")
2017-02-05 00:35:23 +08:00
2017-02-09 15:58:32 +08:00
let passwordCategoryArray = passwordCategoryEntities?.map { $0.category! }
2017-02-06 21:53:54 +08:00
passwordCategoryText = (passwordCategoryArray?.joined(separator: " > "))!
2017-02-05 00:35:23 +08:00
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PasswordDetailTableViewController.tapMenu(recognizer:)))
tableView.addGestureRecognizer(tapGesture)
tapGesture.delegate = self
2017-02-05 14:08:19 +08:00
tableView.contentInset = UIEdgeInsetsMake(-36, 0, 0, 0);
2017-02-05 14:08:19 +08:00
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 52
2017-02-06 15:31:28 +08:00
let indicatorLable = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 21))
indicatorLable.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.height * 0.382 + 22)
indicatorLable.backgroundColor = UIColor.clear
indicatorLable.textColor = UIColor.gray
indicatorLable.text = "decrypting password"
indicatorLable.textAlignment = .center
indicatorLable.font = UIFont.preferredFont(forTextStyle: .footnote)
let indicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
indicator.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.height * 0.382)
indicator.startAnimating()
tableView.addSubview(indicator)
2017-02-06 15:31:28 +08:00
tableView.addSubview(indicatorLable)
2017-02-09 14:41:59 +08:00
if let imageData = passwordEntity?.image {
let image = UIImage(data: imageData as Data)
passwordImage = image
}
2017-02-09 15:47:42 +08:00
tableData.append(TableSection(title: "", item: []))
tableData[0].item.append(TableCell())
DispatchQueue.global(qos: .userInitiated).async {
do {
self.password = try self.passwordEntity!.decrypt()!
} catch {
let alert = UIAlertController(title: "Cannot Show Password", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(UIAlertAction) -> Void in
self.navigationController!.popViewController(animated: true)
}))
self.present(alert, animated: true, completion: nil)
}
2017-02-09 15:47:42 +08:00
var tableDataIndex = 1
self.tableData.append(TableSection(title: "", item: []))
2017-02-06 22:14:42 +08:00
if self.password.username != "" {
self.tableData[tableDataIndex].item.append(TableCell(title: "username", content: self.password.username))
}
self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: self.password.password))
if self.password.additions.count > 0 {
self.tableData.append(TableSection(title: "additions", item: []))
tableDataIndex += 1
2017-02-06 22:14:42 +08:00
for addition in self.password.additions {
self.tableData[tableDataIndex].item.append(TableCell(title: addition.title, content: addition.content))
2017-02-09 13:17:11 +08:00
if addition.title.lowercased() == "url" {
self.password.url = addition.content
}
}
}
2017-02-09 13:33:50 +08:00
DispatchQueue.main.async { [weak self] in
self?.tableView.reloadData()
indicator.stopAnimating()
2017-02-06 15:31:28 +08:00
indicatorLable.isHidden = true
2017-02-09 14:41:59 +08:00
if self?.password.url != "" && self?.passwordEntity?.image == nil{
2017-02-09 13:33:50 +08:00
self?.updatePasswordImage(url: self?.password.url ?? "")
2017-02-09 13:17:11 +08:00
}
}
}
}
func updatePasswordImage(url: String) {
do {
2017-02-09 13:33:50 +08:00
try FavIcon.downloadPreferred(url) { [weak self] result in
2017-02-09 13:17:11 +08:00
switch result {
case .success(let image):
let indexPath = IndexPath(row: 0, section: 0)
2017-02-09 13:33:50 +08:00
self?.passwordImage = image
self?.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
2017-02-09 14:41:59 +08:00
let imageData = UIImageJPEGRepresentation(image, 1)
self?.passwordEntity?.setValue(imageData, forKey: "image")
2017-02-09 13:17:11 +08:00
case .failure(let error):
print(error)
}
}
2017-02-09 13:17:11 +08:00
} catch {
print(error)
}
2017-02-05 00:35:23 +08:00
}
func tapMenu(recognizer: UITapGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.ended {
let tapLocation = recognizer.location(in: self.tableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? LabelTableViewCell {
tappedCell.becomeFirstResponder()
let menuController = UIMenuController.shared
let revealItem = UIMenuItem(title: "Reveal", action: #selector(LabelTableViewCell.revealPassword(_:)))
let concealItem = UIMenuItem(title: "Conceal", action: #selector(LabelTableViewCell.concealPassword(_:)))
2017-02-08 17:55:18 +08:00
let openURLItem = UIMenuItem(title: "Copy Password & Open Link", action: #selector(LabelTableViewCell.openLink(_:)))
menuController.menuItems = [revealItem, concealItem, openURLItem]
2017-02-05 00:35:23 +08:00
menuController.setTargetRect(tappedCell.contentLabel.frame, in: tappedCell.contentLabel.superview!)
menuController.setMenuVisible(true, animated: true)
}
}
}
2017-02-04 20:38:40 +08:00
}
2017-02-08 17:55:18 +08:00
2017-02-02 21:04:31 +08:00
override func numberOfSections(in tableView: UITableView) -> Int {
2017-02-09 15:47:42 +08:00
return tableData.count
2017-02-02 21:04:31 +08:00
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
2017-02-09 15:47:42 +08:00
return tableData[section].item.count
2017-02-02 21:04:31 +08:00
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let sectionIndex = indexPath.section
let rowIndex = indexPath.row
2017-02-05 13:56:37 +08:00
if sectionIndex == 0 && rowIndex == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordDetailTitleTableViewCell", for: indexPath) as! PasswordDetailTitleTableViewCell
2017-02-09 13:17:11 +08:00
cell.passwordImageImageView.image = passwordImage ?? #imageLiteral(resourceName: "PasswordImagePlaceHolder")
cell.nameLabel.text = passwordEntity?.name
2017-02-06 21:53:54 +08:00
cell.categoryLabel.text = passwordCategoryText
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell
2017-02-09 15:47:42 +08:00
let titleData = tableData[sectionIndex].item[rowIndex].title
let contentData = tableData[sectionIndex].item[rowIndex].content
2017-02-08 17:55:18 +08:00
cell.password = password
cell.isPasswordCell = (titleData.lowercased() == "password" ? true : false)
cell.isURLCell = (titleData.lowercased() == "url" ? true : false)
cell.cellData = LabelTableViewCellData(title: titleData, content: contentData)
return cell
}
2017-02-02 21:04:31 +08:00
}
2017-02-04 20:38:40 +08:00
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
2017-02-09 15:47:42 +08:00
return tableData[section].title
2017-02-04 20:38:40 +08:00
}
2017-02-03 18:02:40 +08:00
override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
if action == #selector(copy(_:)) {
2017-02-04 20:38:40 +08:00
UIPasteboard.general.string = tableData[indexPath.section].item[indexPath.row].content
2017-02-03 18:02:40 +08:00
}
}
override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return action == #selector(UIResponderStandardEditActions.copy(_:))
}
override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
2017-02-02 21:04:31 +08:00
}