passforios/pass/Controllers/AddPasswordTableViewController.swift

74 lines
3 KiB
Swift
Raw Normal View History

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
import SwiftyUserDefaults
2017-02-10 22:15:01 +08:00
2017-03-09 03:19:36 +08:00
class AddPasswordTableViewController: PasswordEditorTableViewController {
var tempContent: String = ""
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"]],
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "password"],
[.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]],
2017-03-09 03:19:36 +08:00
[[.type: PasswordEditorCellType.textViewCell, .title: "additions"]],
[[.type: PasswordEditorCellType.scanQRCodeCell]]
2017-03-09 03:19:36 +08:00
]
2017-02-10 22:15:01 +08:00
super.viewDidLoad()
2017-02-11 20:45:56 +08:00
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "saveAddPasswordSegue" {
// check PGP key
guard passwordStore.privateKey != nil else {
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
}
// check name
let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! TextFieldTableViewCell
guard nameCell.getContent()!.isEmpty == false else {
let alertTitle = "Cannot Add Password"
let alertMessage = "Please fill in the name."
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
return false
}
}
return true
}
2017-02-10 22:15:01 +08:00
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if segue.identifier == "saveAddPasswordSegue" {
let cells = tableView.visibleCells
2017-03-09 03:19:36 +08:00
var cellContents = [String: String]()
for cell in cells {
if let indexPath = tableView.indexPath(for: cell),
let contentCell = cell as? ContentTableViewCell,
let cellTitle = tableData[indexPath.section][indexPath.row][.title] as? String,
let cellContent = contentCell.getContent() {
2017-03-09 03:19:36 +08:00
cellContents[cellTitle] = cellContent
}
}
var plainText = ""
if cellContents["additions"]! != "" {
plainText = "\(cellContents["password"]!)\n\(cellContents["additions"]!)"
} else {
plainText = "\(cellContents["password"]!)"
}
let name = URL(string: cellContents["name"]!)!.lastPathComponent
let url = URL(string: cellContents["name"]!)!.appendingPathExtension("gpg")
password = Password(name: name, url: url, plainText: plainText)
2017-02-11 16:12:10 +08:00
}
2017-02-10 22:15:01 +08:00
}
}