Polish the password editor table view

This commit is contained in:
Yishi Lin 2017-04-27 23:26:12 +08:00
parent 165af8588a
commit 034f43220b
3 changed files with 30 additions and 51 deletions

View file

@ -35,8 +35,7 @@ class AddPasswordTableViewController: PasswordEditorTableViewController {
} }
// check name // check name
let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! TextFieldTableViewCell guard nameCell?.getContent()?.isEmpty == false else {
guard nameCell.getContent()!.isEmpty == false else {
let alertTitle = "Cannot Add Password" let alertTitle = "Cannot Add Password"
let alertMessage = "Please fill in the name." let alertMessage = "Please fill in the name."
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil) Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
@ -49,23 +48,12 @@ class AddPasswordTableViewController: PasswordEditorTableViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender) super.prepare(for: segue, sender: sender)
if segue.identifier == "saveAddPasswordSegue" { if segue.identifier == "saveAddPasswordSegue" {
let cells = tableView.visibleCells var plainText = (fillPasswordCell?.getContent())!
var cellContents = [String: String]() if let additionsString = additionsCell?.getContent(), additionsString.isEmpty == false {
for cell in cells { plainText.append("\n")
if let indexPath = tableView.indexPath(for: cell), plainText.append(additionsString)
let contentCell = cell as? ContentTableViewCell,
let cellTitle = tableData[indexPath.section][indexPath.row][.title] as? String,
let cellContent = contentCell.getContent() {
cellContents[cellTitle] = cellContent
}
} }
var plainText = "" let encodedName = (nameCell?.getContent()?.stringByAddingPercentEncodingForRFC3986())!
if cellContents["additions"]! != "" {
plainText = "\(cellContents["password"]!)\n\(cellContents["additions"]!)"
} else {
plainText = "\(cellContents["password"]!)"
}
let encodedName = cellContents["name"]!.stringByAddingPercentEncodingForRFC3986()!
let name = URL(string: encodedName)!.lastPathComponent let name = URL(string: encodedName)!.lastPathComponent
let url = URL(string: encodedName)!.appendingPathExtension("gpg") let url = URL(string: encodedName)!.appendingPathExtension("gpg")
password = Password(name: name, url: url, plainText: plainText) password = Password(name: name, url: url, plainText: plainText)

View file

@ -23,15 +23,13 @@ class EditPasswordTableViewController: PasswordEditorTableViewController {
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "saveEditPasswordSegue" { if identifier == "saveEditPasswordSegue" {
if let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? ContentTableViewCell { if let name = nameCell?.getContent(),
if let name = nameCell.getContent(), let path = name.stringByAddingPercentEncodingForRFC3986(),
let path = name.stringByAddingPercentEncodingForRFC3986(), let _ = URL(string: path) {
let _ = URL(string: path) { return true
return true } else {
} else { Utils.alert(title: "Cannot Save", message: "Password name is invalid.", controller: self, completion: nil)
Utils.alert(title: "Cannot Save", message: "Password name is invalid.", controller: self, completion: nil) return false
return false
}
} }
} }
return true return true
@ -40,24 +38,14 @@ class EditPasswordTableViewController: PasswordEditorTableViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender) super.prepare(for: segue, sender: sender)
if segue.identifier == "saveEditPasswordSegue" { if segue.identifier == "saveEditPasswordSegue" {
let cells = tableView.visibleCells var plainText = (fillPasswordCell?.getContent())!
var cellContents = [String: String]() if let additionsString = additionsCell?.getContent(), additionsString.isEmpty == false {
for cell in cells { plainText.append("\n")
if let indexPath = tableView.indexPath(for: cell), plainText.append(additionsString)
let contentCell = cell as? ContentTableViewCell,
let cellTitle = tableData[indexPath.section][indexPath.row][.title] as? String,
let cellContent = contentCell.getContent() {
cellContents[cellTitle] = cellContent
}
} }
var plainText = "" let encodedName = (nameCell?.getContent()?.stringByAddingPercentEncodingForRFC3986())!
if cellContents["additions"]! != "" { let name = URL(string: encodedName)!.lastPathComponent
plainText = "\(cellContents["password"]!)\n\(cellContents["additions"]!)" let url = URL(string: encodedName)!.appendingPathExtension("gpg")
} else {
plainText = "\(cellContents["password"]!)"
}
let name = URL(string: cellContents["name"]!.stringByAddingPercentEncodingForRFC3986()!)!.lastPathComponent
let url = URL(string: cellContents["name"]!.stringByAddingPercentEncodingForRFC3986()!)!.appendingPathExtension("gpg")
if password!.plainText != plainText || password!.url!.path != url.path { if password!.plainText != plainText || password!.url!.path != url.path {
password!.updatePassword(name: name, url: url, plainText: plainText) password!.updatePassword(name: name, url: url, plainText: plainText)
} }

View file

@ -34,10 +34,10 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
private let additionsSection = 2 private let additionsSection = 2
private var hidePasswordSettings = true private var hidePasswordSettings = true
private var nameCell: TextFieldTableViewCell? var nameCell: TextFieldTableViewCell?
private var fillPasswordCell: FillPasswordTableViewCell? var fillPasswordCell: FillPasswordTableViewCell?
private var passwordLengthCell: SliderTableViewCell? private var passwordLengthCell: SliderTableViewCell?
private var additionsCell: TextViewTableViewCell? var additionsCell: TextViewTableViewCell?
private var deletePasswordCell: UITableViewCell? private var deletePasswordCell: UITableViewCell?
private var scanQRCodeCell: UITableViewCell? private var scanQRCodeCell: UITableViewCell?
@ -193,13 +193,16 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
func insertScannedOTPFields(_ otpauth: String) { func insertScannedOTPFields(_ otpauth: String) {
// update tableData // update tableData
var additionsString = ""
if let additionsPlainText = (tableData[additionsSection][0][PasswordEditorCellKey.content] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines), additionsPlainText != "" { if let additionsPlainText = (tableData[additionsSection][0][PasswordEditorCellKey.content] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines), additionsPlainText != "" {
tableData[additionsSection][0][PasswordEditorCellKey.content] = additionsPlainText + "\n" + otpauth additionsString = additionsPlainText + "\n" + otpauth
} else { } else {
tableData[additionsSection][0][PasswordEditorCellKey.content] = otpauth additionsString = otpauth
} }
// reload tableData[additionsSection][0][PasswordEditorCellKey.content] = additionsString
tableView.reloadSections([additionsSection], with: .none)
// reload the additions cell
additionsCell?.setContent(content: additionsString)
} }
// MARK: - QRScannerControllerDelegate Methods // MARK: - QRScannerControllerDelegate Methods