diff --git a/pass/Controllers/AddPasswordTableViewController.swift b/pass/Controllers/AddPasswordTableViewController.swift index 1fad471..e56c1af 100644 --- a/pass/Controllers/AddPasswordTableViewController.swift +++ b/pass/Controllers/AddPasswordTableViewController.swift @@ -35,8 +35,7 @@ class AddPasswordTableViewController: PasswordEditorTableViewController { } // 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 alertMessage = "Please fill in the name." 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?) { super.prepare(for: segue, sender: sender) if segue.identifier == "saveAddPasswordSegue" { - let cells = tableView.visibleCells - 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() { - cellContents[cellTitle] = cellContent - } + var plainText = (fillPasswordCell?.getContent())! + if let additionsString = additionsCell?.getContent(), additionsString.isEmpty == false { + plainText.append("\n") + plainText.append(additionsString) } - var plainText = "" - if cellContents["additions"]! != "" { - plainText = "\(cellContents["password"]!)\n\(cellContents["additions"]!)" - } else { - plainText = "\(cellContents["password"]!)" - } - let encodedName = cellContents["name"]!.stringByAddingPercentEncodingForRFC3986()! + let encodedName = (nameCell?.getContent()?.stringByAddingPercentEncodingForRFC3986())! let name = URL(string: encodedName)!.lastPathComponent let url = URL(string: encodedName)!.appendingPathExtension("gpg") password = Password(name: name, url: url, plainText: plainText) diff --git a/pass/Controllers/EditPasswordTableViewController.swift b/pass/Controllers/EditPasswordTableViewController.swift index 26577b8..6e69839 100644 --- a/pass/Controllers/EditPasswordTableViewController.swift +++ b/pass/Controllers/EditPasswordTableViewController.swift @@ -23,15 +23,13 @@ class EditPasswordTableViewController: PasswordEditorTableViewController { override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { if identifier == "saveEditPasswordSegue" { - if let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? ContentTableViewCell { - if let name = nameCell.getContent(), - let path = name.stringByAddingPercentEncodingForRFC3986(), - let _ = URL(string: path) { - return true - } else { - Utils.alert(title: "Cannot Save", message: "Password name is invalid.", controller: self, completion: nil) - return false - } + if let name = nameCell?.getContent(), + let path = name.stringByAddingPercentEncodingForRFC3986(), + let _ = URL(string: path) { + return true + } else { + Utils.alert(title: "Cannot Save", message: "Password name is invalid.", controller: self, completion: nil) + return false } } return true @@ -40,24 +38,14 @@ class EditPasswordTableViewController: PasswordEditorTableViewController { override func prepare(for segue: UIStoryboardSegue, sender: Any?) { super.prepare(for: segue, sender: sender) if segue.identifier == "saveEditPasswordSegue" { - let cells = tableView.visibleCells - 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() { - cellContents[cellTitle] = cellContent - } + var plainText = (fillPasswordCell?.getContent())! + if let additionsString = additionsCell?.getContent(), additionsString.isEmpty == false { + plainText.append("\n") + plainText.append(additionsString) } - var plainText = "" - if cellContents["additions"]! != "" { - plainText = "\(cellContents["password"]!)\n\(cellContents["additions"]!)" - } else { - plainText = "\(cellContents["password"]!)" - } - let name = URL(string: cellContents["name"]!.stringByAddingPercentEncodingForRFC3986()!)!.lastPathComponent - let url = URL(string: cellContents["name"]!.stringByAddingPercentEncodingForRFC3986()!)!.appendingPathExtension("gpg") + let encodedName = (nameCell?.getContent()?.stringByAddingPercentEncodingForRFC3986())! + let name = URL(string: encodedName)!.lastPathComponent + let url = URL(string: encodedName)!.appendingPathExtension("gpg") if password!.plainText != plainText || password!.url!.path != url.path { password!.updatePassword(name: name, url: url, plainText: plainText) } diff --git a/pass/Controllers/PasswordEditorTableViewController.swift b/pass/Controllers/PasswordEditorTableViewController.swift index 191a60b..3441490 100644 --- a/pass/Controllers/PasswordEditorTableViewController.swift +++ b/pass/Controllers/PasswordEditorTableViewController.swift @@ -34,10 +34,10 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl private let additionsSection = 2 private var hidePasswordSettings = true - private var nameCell: TextFieldTableViewCell? - private var fillPasswordCell: FillPasswordTableViewCell? + var nameCell: TextFieldTableViewCell? + var fillPasswordCell: FillPasswordTableViewCell? private var passwordLengthCell: SliderTableViewCell? - private var additionsCell: TextViewTableViewCell? + var additionsCell: TextViewTableViewCell? private var deletePasswordCell: UITableViewCell? private var scanQRCodeCell: UITableViewCell? @@ -193,13 +193,16 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl func insertScannedOTPFields(_ otpauth: String) { // update tableData + var additionsString = "" 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 { - tableData[additionsSection][0][PasswordEditorCellKey.content] = otpauth + additionsString = otpauth } - // reload - tableView.reloadSections([additionsSection], with: .none) + tableData[additionsSection][0][PasswordEditorCellKey.content] = additionsString + + // reload the additions cell + additionsCell?.setContent(content: additionsString) } // MARK: - QRScannerControllerDelegate Methods