2017-02-10 22:15:01 +08:00
|
|
|
//
|
|
|
|
|
// AddPasswordTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 10/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2017-03-09 02:39:13 +08:00
|
|
|
import SwiftyUserDefaults
|
2017-02-10 22:15:01 +08:00
|
|
|
|
2017-03-09 03:19:36 +08:00
|
|
|
class AddPasswordTableViewController: PasswordEditorTableViewController {
|
|
|
|
|
var tempContent: String = ""
|
2017-03-16 22:39:03 -07:00
|
|
|
let passwordStore = PasswordStore.shared
|
2017-02-10 22:15:01 +08:00
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
2017-03-09 03:19:36 +08:00
|
|
|
tableData = [
|
|
|
|
|
[[.type: PasswordEditorCellType.textFieldCell, .title: "name"]],
|
2017-03-23 01:28:46 +08:00
|
|
|
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "password"]],
|
2017-03-09 03:19:36 +08:00
|
|
|
[[.type: PasswordEditorCellType.textViewCell, .title: "additions"]],
|
|
|
|
|
]
|
2017-02-10 22:15:01 +08:00
|
|
|
super.viewDidLoad()
|
2017-02-11 20:45:56 +08:00
|
|
|
}
|
|
|
|
|
|
2017-03-09 02:39:13 +08:00
|
|
|
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
|
|
|
|
if identifier == "saveAddPasswordSegue" {
|
|
|
|
|
// check PGP key
|
2017-03-22 17:42:37 -07:00
|
|
|
guard passwordStore.privateKey != nil else {
|
2017-03-09 02:39:13 +08:00
|
|
|
let alertTitle = "Cannot Add Password"
|
|
|
|
|
let alertMessage = "PGP Key is not set. Please set your PGP Key first."
|
|
|
|
|
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-03-22 17:42:37 -07:00
|
|
|
|
2017-03-09 02:39:13 +08:00
|
|
|
// check name
|
2017-03-15 23:03:43 -07:00
|
|
|
let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! TextFieldTableViewCell
|
2017-03-22 17:42:37 -07:00
|
|
|
guard nameCell.getContent()!.isEmpty == false else {
|
2017-03-09 02:39:13 +08:00
|
|
|
let alertTitle = "Cannot Add Password"
|
|
|
|
|
let alertMessage = "Please fill in the name."
|
|
|
|
|
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-03-22 17:42:37 -07:00
|
|
|
|
|
|
|
|
// check "/"
|
|
|
|
|
guard nameCell.getContent()!.contains("/") == false else {
|
|
|
|
|
let alertTitle = "Cannot Add Password"
|
|
|
|
|
let alertMessage = "Illegal character."
|
|
|
|
|
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-03-09 02:39:13 +08:00
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 22:15:01 +08:00
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
2017-03-09 03:19:36 +08:00
|
|
|
if segue.identifier == "saveAddPasswordSegue" {let cells = tableView.visibleCells
|
|
|
|
|
var cellContents = [String: String]()
|
|
|
|
|
for cell in cells {
|
|
|
|
|
let indexPath = tableView.indexPath(for: cell)!
|
|
|
|
|
let contentCell = cell as! ContentTableViewCell
|
|
|
|
|
let cellTitle = tableData[indexPath.section][indexPath.row][.title] as! String
|
|
|
|
|
if let cellContent = contentCell.getContent() {
|
|
|
|
|
cellContents[cellTitle] = cellContent
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var plainText = ""
|
|
|
|
|
if cellContents["additions"]! != "" {
|
|
|
|
|
plainText = "\(cellContents["password"]!)\n\(cellContents["additions"]!)"
|
|
|
|
|
} else {
|
|
|
|
|
plainText = "\(cellContents["password"]!)"
|
|
|
|
|
}
|
|
|
|
|
password = Password(name: cellContents["name"]!, plainText: plainText)
|
2017-02-11 16:12:10 +08:00
|
|
|
}
|
2017-02-10 22:15:01 +08:00
|
|
|
}
|
|
|
|
|
}
|