2017-02-13 01:15:42 +08:00
|
|
|
//
|
|
|
|
|
// PasswordEditorTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 12/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
enum PasswordEditorCellType {
|
2017-03-21 13:16:25 -07:00
|
|
|
case textFieldCell, textViewCell, fillPasswordCell, passwordLengthCell, deletePasswordCell
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum PasswordEditorCellKey {
|
|
|
|
|
case type, title, content, placeholders
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 01:28:46 +08:00
|
|
|
class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate, PasswordSettingSliderTableViewCellDelegate, UIGestureRecognizerDelegate {
|
2017-02-13 01:15:42 +08:00
|
|
|
var navigationItemTitle: String?
|
2017-03-21 13:16:25 -07:00
|
|
|
var password: Password?
|
2017-02-13 01:15:42 +08:00
|
|
|
var tableData = [
|
|
|
|
|
[Dictionary<PasswordEditorCellKey, Any>]
|
|
|
|
|
]()
|
2017-03-21 13:16:25 -07:00
|
|
|
var sectionHeaderTitles = ["name", "password", "additions",""].map {$0.uppercased()}
|
|
|
|
|
var sectionFooterTitles = ["", "", "Use \"key: value\" format for additional fields.", ""]
|
2017-03-23 01:28:46 +08:00
|
|
|
let passwordSection = 1
|
2017-03-09 03:19:36 +08:00
|
|
|
|
2017-03-23 01:28:46 +08:00
|
|
|
var fillPasswordCell: FillPasswordTableViewCell?
|
2017-03-09 03:19:36 +08:00
|
|
|
var passwordLengthCell: SliderTableViewCell?
|
2017-03-21 13:16:25 -07:00
|
|
|
var deletePasswordCell: UITableViewCell?
|
|
|
|
|
|
|
|
|
|
override func loadView() {
|
|
|
|
|
super.loadView()
|
|
|
|
|
|
|
|
|
|
deletePasswordCell = UITableViewCell(style: .default, reuseIdentifier: "default")
|
|
|
|
|
deletePasswordCell!.textLabel?.text = "Delete Password"
|
|
|
|
|
deletePasswordCell!.textLabel?.textColor = Globals.red
|
|
|
|
|
deletePasswordCell?.selectionStyle = .default
|
|
|
|
|
}
|
2017-02-13 01:15:42 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
navigationItem.title = navigationItemTitle
|
|
|
|
|
tableView.register(UINib(nibName: "TextFieldTableViewCell", bundle: nil), forCellReuseIdentifier: "textFieldCell")
|
|
|
|
|
tableView.register(UINib(nibName: "TextViewTableViewCell", bundle: nil), forCellReuseIdentifier: "textViewCell")
|
|
|
|
|
tableView.register(UINib(nibName: "FillPasswordTableViewCell", bundle: nil), forCellReuseIdentifier: "fillPasswordCell")
|
2017-03-09 03:19:36 +08:00
|
|
|
tableView.register(UINib(nibName: "SliderTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordLengthCell")
|
2017-02-13 01:15:42 +08:00
|
|
|
|
|
|
|
|
tableView.rowHeight = UITableViewAutomaticDimension
|
|
|
|
|
tableView.estimatedRowHeight = 48
|
2017-02-21 01:06:03 +08:00
|
|
|
self.tableView.sectionFooterHeight = UITableViewAutomaticDimension;
|
|
|
|
|
self.tableView.estimatedSectionFooterHeight = 0;
|
2017-03-23 01:28:46 +08:00
|
|
|
|
|
|
|
|
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tableTapped))
|
|
|
|
|
tapGesture.delegate = self
|
|
|
|
|
tableView.addGestureRecognizer(tapGesture)
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
|
let cellData = tableData[indexPath.section][indexPath.row]
|
|
|
|
|
|
|
|
|
|
switch cellData[PasswordEditorCellKey.type] as! PasswordEditorCellType {
|
|
|
|
|
case .textViewCell:
|
2017-03-21 13:16:25 -07:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! ContentTableViewCell
|
|
|
|
|
cell.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
|
|
|
|
|
return cell
|
2017-02-13 01:15:42 +08:00
|
|
|
case .fillPasswordCell:
|
2017-03-23 01:28:46 +08:00
|
|
|
fillPasswordCell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as? FillPasswordTableViewCell
|
|
|
|
|
fillPasswordCell?.delegate = self
|
|
|
|
|
fillPasswordCell?.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
|
|
|
|
|
return fillPasswordCell!
|
2017-03-09 03:19:36 +08:00
|
|
|
case .passwordLengthCell:
|
|
|
|
|
passwordLengthCell = tableView.dequeueReusableCell(withIdentifier: "passwordLengthCell", for: indexPath) as? SliderTableViewCell
|
2017-03-08 20:39:08 -08:00
|
|
|
passwordLengthCell?.reset(title: "Length", minimumValue: Globals.passwordMinimumLength, maximumValue: Globals.passwordMaximumLength, defaultValue: Globals.passwordDefaultLength)
|
2017-03-23 01:28:46 +08:00
|
|
|
passwordLengthCell?.delegate = self
|
2017-03-21 13:16:25 -07:00
|
|
|
return passwordLengthCell!
|
|
|
|
|
case .deletePasswordCell:
|
|
|
|
|
return deletePasswordCell!
|
2017-02-13 01:15:42 +08:00
|
|
|
default:
|
2017-03-21 13:16:25 -07:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! ContentTableViewCell
|
|
|
|
|
cell.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
|
|
|
|
|
return cell
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
|
|
|
return 44
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
|
return tableData.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
|
return tableData[section].count
|
|
|
|
|
}
|
2017-03-23 01:28:46 +08:00
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
|
return sectionHeaderTitles[section]
|
|
|
|
|
}
|
2017-02-21 01:06:03 +08:00
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
|
|
|
|
return sectionFooterTitles[section]
|
|
|
|
|
}
|
2017-03-09 03:19:36 +08:00
|
|
|
|
2017-03-21 13:16:25 -07:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
if tableView.cellForRow(at: indexPath) == deletePasswordCell {
|
|
|
|
|
let alert = UIAlertController(title: "Delete Password?", message: nil, preferredStyle: UIAlertControllerStyle.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Delete", style: UIAlertActionStyle.destructive, handler: {[unowned self] (action) -> Void in
|
|
|
|
|
self.performSegue(withIdentifier: "deletePasswordSegue", sender: self)
|
|
|
|
|
}))
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:nil))
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 01:28:46 +08:00
|
|
|
// generate password, copy to pasteboard, and set the cell
|
|
|
|
|
func generateAndCopyPassword() {
|
|
|
|
|
// insert the length slider if not existed
|
|
|
|
|
if passwordLengthCell == nil {
|
|
|
|
|
let row = tableData[passwordSection].count
|
|
|
|
|
tableData[passwordSection].append([.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"])
|
|
|
|
|
let indexPath = IndexPath(row: row, section: passwordSection)
|
|
|
|
|
tableView.insertRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
|
|
|
|
|
}
|
2017-03-09 03:19:36 +08:00
|
|
|
let length = passwordLengthCell?.roundedValue ?? Globals.passwordDefaultLength
|
2017-03-23 01:28:46 +08:00
|
|
|
let plainPassword = Utils.generatePassword(length: length)
|
|
|
|
|
Utils.copyToPasteboard(textToCopy: plainPassword)
|
|
|
|
|
fillPasswordCell?.setContent(content: plainPassword)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func tableTapped(tap: UITapGestureRecognizer) {
|
|
|
|
|
let location = tap.location(in: self.tableView)
|
|
|
|
|
let path = self.tableView.indexPathForRow(at: location)
|
|
|
|
|
if path?.section != passwordSection, tableData[passwordSection].count > 1 {
|
|
|
|
|
// remove password settings (e.g., sliders)
|
|
|
|
|
let row = tableData[passwordSection].count
|
|
|
|
|
passwordLengthCell = nil
|
|
|
|
|
tableData[passwordSection].removeLast(row - 1)
|
|
|
|
|
let indexPaths = (1...row-1).map{IndexPath(row: $0, section: passwordSection)}
|
|
|
|
|
print(indexPaths)
|
|
|
|
|
tableView.deleteRows(at: indexPaths, with: UITableViewRowAnimation.automatic)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
|
|
|
|
|
if (gestureRecognizer is UITapGestureRecognizer) {
|
|
|
|
|
// so that the tap gesture could be passed by
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-03-09 03:19:36 +08:00
|
|
|
}
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|