finish simple "add password function"

This commit is contained in:
Bob Sun 2017-02-10 22:15:01 +08:00
parent cbbb631e08
commit b954a4dcab
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
13 changed files with 314 additions and 26 deletions

View file

@ -0,0 +1,51 @@
//
// AddPasswordTableViewController.swift
// pass
//
// Created by Mingshen Sun on 10/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
class AddPasswordTableViewController: UITableViewController {
let tableTitles = ["name", "password"]
var password: Password?
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "TextFieldTableViewCell", bundle: nil), forCellReuseIdentifier: "textFieldCell")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 52
tableView.allowsSelection = false
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableTitles.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! TextFieldTableViewCell
cell.titleLabel.text = tableTitles[indexPath.row]
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let name = getCellForName(name: "name")!.contentTextField.text ?? ""
let passwordText = getCellForName(name: "password")!.contentTextField.text ?? ""
// let additions = getCellForName(name: "additions")!.contentTextField.text ?? ""
// let additionSplit = additions.characters.split(separator: ":").map(String.init)
// print(additionSplit)
// let additionField = AdditionField(title: additionSplit[0], content: additionSplit[1])
password = Password(name: name, username: "", password: passwordText, additions: [])
}
func getCellAt(row: Int) -> TextFieldTableViewCell? {
return tableView.cellForRow(at: IndexPath(row: row, section: 0)) as? TextFieldTableViewCell
}
func getCellForName(name: String) -> TextFieldTableViewCell? {
let index = tableTitles.index(of: name)!
return getCellAt(row: Int(index))
}
}

View file

@ -11,18 +11,21 @@ import SwiftyUserDefaults
class PGPKeySettingTableViewController: UITableViewController {
@IBOutlet weak var pgpKeyURLTextField: UITextField!
@IBOutlet weak var pgpPublicKeyURLTextField: UITextField!
@IBOutlet weak var pgpPrivateKeyURLTextField: UITextField!
@IBOutlet weak var pgpKeyPassphraseTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
pgpKeyURLTextField.text = Defaults[.pgpKeyURL]?.absoluteString
pgpPublicKeyURLTextField.text = Defaults[.pgpPublicKeyURL]?.absoluteString
pgpPrivateKeyURLTextField.text = Defaults[.pgpPrivateKeyURL]?.absoluteString
pgpKeyPassphraseTextField.text = Defaults[.pgpKeyPassphrase]
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "savePGPKeySegue" {
if URL(string: pgpKeyURLTextField.text!)!.scheme! == "http" {
if URL(string: pgpPublicKeyURLTextField.text!)!.scheme! == "http" &&
URL(string: pgpPrivateKeyURLTextField.text!)!.scheme! == "http" {
let alertMessage = "HTTP connection is not supported."
let alert = UIAlertController(title: "Cannot Save Settings", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

View file

@ -27,6 +27,15 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
@IBOutlet weak var tableView: UITableView!
@IBAction func cancelAddPassword(segue: UIStoryboardSegue) {
}
@IBAction func saveAddPassword(segue: UIStoryboardSegue) {
if let controller = segue.source as? AddPasswordTableViewController {
PasswordStore.shared.add(password: controller.password!)
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
}
}
func syncPasswords() {
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.light)

View file

@ -26,9 +26,11 @@ class SettingsTableViewController: UITableViewController {
@IBAction func save(segue: UIStoryboardSegue) {
if let controller = segue.source as? PGPKeySettingTableViewController {
if Defaults[.pgpKeyURL] != URL(string: controller.pgpKeyURLTextField.text!) ||
if Defaults[.pgpPrivateKeyURL] != URL(string: controller.pgpPrivateKeyURLTextField.text!) ||
Defaults[.pgpPublicKeyURL] != URL(string: controller.pgpPublicKeyURLTextField.text!) ||
Defaults[.pgpKeyPassphrase] != controller.pgpKeyPassphraseTextField.text! {
Defaults[.pgpKeyURL] = URL(string: controller.pgpKeyURLTextField.text!)
Defaults[.pgpPrivateKeyURL] = URL(string: controller.pgpPrivateKeyURLTextField.text!)
Defaults[.pgpPublicKeyURL] = URL(string: controller.pgpPublicKeyURLTextField.text!)
Defaults[.pgpKeyPassphrase] = controller.pgpKeyPassphraseTextField.text!
SVProgressHUD.setDefaultMaskType(.black)
@ -36,7 +38,10 @@ class SettingsTableViewController: UITableViewController {
SVProgressHUD.show(withStatus: "Fetching PGP Key")
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
do {
try PasswordStore.shared.initPGP(pgpKeyURL: Defaults[.pgpKeyURL]!, pgpKeyLocalPath: Globals.secringPath)
try PasswordStore.shared.initPGP(pgpPublicKeyURL: Defaults[.pgpPublicKeyURL]!,
pgpPublicKeyLocalPath: Globals.pgpPublicKeyPath,
pgpPrivateKeyURL: Defaults[.pgpPrivateKeyURL]!,
pgpPrivateKeyLocalPath: Globals.pgpPrivateKeyPath)
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID]
SVProgressHUD.showSuccess(withStatus: "Success. Remember to remove the key from the server.")