Support folder operations

Example:
  - add: a/b/c/d
  - delete: a/b/c/d
  - move: a/b/c/d -> a/b/c/d/e
This commit is contained in:
Bob Sun 2017-04-23 10:03:09 -07:00
parent b1ef8a343f
commit d8ecd1e889
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
8 changed files with 213 additions and 141 deletions

View file

@ -14,7 +14,6 @@ import SVProgressHUD
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate {
var passwordEntity: PasswordEntity?
private var password: Password?
private var passwordCategoryText = ""
private var passwordImage: UIImage?
private var oneTimePasswordIndexPath : IndexPath?
private var shouldPopCurrentView = false
@ -77,7 +76,6 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell")
tableView.register(UINib(nibName: "PasswordDetailTitleTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordDetailTitleTableViewCell")
passwordCategoryText = passwordEntity!.getCategoryText()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PasswordDetailTableViewController.tapMenu(recognizer:)))
tapGesture.cancelsTouchesInView = false
tableView.addGestureRecognizer(tapGesture)
@ -96,24 +94,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
let image = UIImage(data: imageData as Data)
passwordImage = image
}
var passphrase = ""
if Defaults[.isRememberPassphraseOn] && self.passwordStore.pgpKeyPassphrase != nil {
passphrase = self.passwordStore.pgpKeyPassphrase!
self.decryptThenShowPassword(passphrase: passphrase)
} else {
let alert = UIAlertController(title: "Passphrase", message: "Please fill in the passphrase of your PGP secret key.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
passphrase = alert.textFields!.first!.text!
self.decryptThenShowPassword(passphrase: passphrase)
}))
alert.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = ""
textField.isSecureTextEntry = true
})
self.present(alert, animated: true, completion: nil)
}
self.decryptThenShowPassword()
self.setupOneTimePasswordAutoRefresh()
// pop the current view because this password might be "discarded"
@ -137,14 +118,33 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
}
}
private func decryptThenShowPassword(passphrase: String) {
private func requestPGPKeyPassphrase() -> String {
let sem = DispatchSemaphore(value: 0)
var passphrase = ""
DispatchQueue.main.async {
let alert = UIAlertController(title: "Passphrase", message: "Please fill in the passphrase of your PGP secret key.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
passphrase = alert.textFields!.first!.text!
sem.signal()
}))
alert.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = ""
textField.isSecureTextEntry = true
})
self.present(alert, animated: true, completion: nil)
}
let _ = sem.wait(timeout: DispatchTime.distantFuture)
if Defaults[.isRememberPassphraseOn] {
self.passwordStore.pgpKeyPassphrase = passphrase
}
return passphrase
}
private func decryptThenShowPassword() {
DispatchQueue.global(qos: .userInitiated).async {
// decrypt password
do {
self.password = try self.passwordEntity!.decrypt(passphrase: passphrase)!
self.password = try self.passwordStore.decrypt(passwordEntity: self.passwordEntity!, requestPGPKeyPassphrase: self.requestPGPKeyPassphrase)
} catch {
DispatchQueue.main.async {
let alert = UIAlertController(title: "Cannot Show Password", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
@ -215,14 +215,14 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
if self.password!.changed {
SVProgressHUD.show(withStatus: "Saving")
DispatchQueue.global(qos: .userInitiated).async {
self.passwordStore.update(passwordEntity: self.passwordEntity!, password: self.password!, progressBlock: { progress in
do {
self.passwordEntity = try self.passwordStore.update(passwordEntity: self.passwordEntity!, password: self.password!)
} catch {
DispatchQueue.main.async {
SVProgressHUD.showProgress(progress, status: "Encrypting")
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
}
})
}
DispatchQueue.main.async {
self.passwordEntity!.synced = false
self.passwordStore.saveUpdated(passwordEntity: self.passwordEntity!)
self.setTableData()
self.tableView.reloadData()
SVProgressHUD.showSuccess(withStatus: "Success")
@ -233,7 +233,6 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
}
@IBAction private func deletePassword(segue: UIStoryboardSegue) {
print("delete")
passwordStore.delete(passwordEntity: passwordEntity!)
let _ = navigationController?.popViewController(animated: true)
}
@ -390,10 +389,14 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
// commit the change of HOTP counter
if password!.changed {
DispatchQueue.global(qos: .userInitiated).async {
self.passwordStore.update(passwordEntity: self.passwordEntity!, password: self.password!, progressBlock: {_ in })
do {
self.passwordEntity = try self.passwordStore.update(passwordEntity: self.passwordEntity!, password: self.password!)
} catch {
DispatchQueue.main.async {
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
}
}
DispatchQueue.main.async {
self.passwordEntity!.synced = false
self.passwordStore.saveUpdated(passwordEntity: self.passwordEntity!)
SVProgressHUD.showSuccess(withStatus: "Password Copied\nCounter Updated")
SVProgressHUD.dismiss(withDelay: 1)
}
@ -435,7 +438,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
cell.nameLabel.text = passwordName
}
}
cell.categoryLabel.text = passwordCategoryText
cell.categoryLabel.text = passwordEntity!.getCategoryText()
cell.selectionStyle = .none
return cell
case .main, .addition:
@ -465,7 +468,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
footerLabel.numberOfLines = 0
footerLabel.font = UIFont.preferredFont(forTextStyle: .footnote)
footerLabel.textColor = UIColor.gray
let dateString = self.passwordStore.getLatestUpdateInfo(filename: (passwordEntity?.path)!)
let dateString = self.passwordStore.getLatestUpdateInfo(filename: password!.url!.path)
footerLabel.text = "Last Updated: \(dateString)"
view.addSubview(footerLabel)
return view