diff --git a/pass/Controllers/AddPasswordTableViewController.swift b/pass/Controllers/AddPasswordTableViewController.swift index 7ae8572..d824c92 100644 --- a/pass/Controllers/AddPasswordTableViewController.swift +++ b/pass/Controllers/AddPasswordTableViewController.swift @@ -9,61 +9,19 @@ import UIKit import SwiftyUserDefaults -class AddPasswordTableViewController: UITableViewController, FillPasswordTableViewCellDelegate { - let tableTitles = ["name", "password", "additions"] - let tableRowsInSection = [1, 2, 1] - var password: Password? +class AddPasswordTableViewController: PasswordEditorTableViewController { - var passwordLengthCell: SliderTableViewCell? + var password: Password? + var tempContent: String = "" override func viewDidLoad() { + tableData = [ + [[.type: PasswordEditorCellType.textFieldCell, .title: "name"]], + [[.type: PasswordEditorCellType.fillPasswordCell, .title: "password"], + [.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]], + [[.type: PasswordEditorCellType.textViewCell, .title: "additions"]], + ] super.viewDidLoad() - tableView.register(UINib(nibName: "TextFieldTableViewCell", bundle: nil), forCellReuseIdentifier: "textFieldCell") - tableView.register(UINib(nibName: "TextViewTableViewCell", bundle: nil), forCellReuseIdentifier: "textViewCell") - tableView.register(UINib(nibName: "FillPasswordTableViewCell", bundle: nil), forCellReuseIdentifier: "fillPasswordCell") - tableView.register(UINib(nibName: "SliderTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordLengthCell") - - tableView.rowHeight = UITableViewAutomaticDimension - tableView.estimatedRowHeight = 48 - tableView.allowsSelection = false - } - - override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return tableRowsInSection[section] - } - - override func numberOfSections(in tableView: UITableView) -> Int { - return tableTitles.count - } - - override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - switch tableTitles[indexPath.section] { - case "additions": - let cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! TextViewTableViewCell - cell.contentTextView.text = "" - return cell - case "password": - switch indexPath.row { - case 0: - let cell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as! FillPasswordTableViewCell - cell.delegate = self - return cell - default: - passwordLengthCell = (tableView.dequeueReusableCell(withIdentifier: "passwordLengthCell", for: indexPath) as! SliderTableViewCell) - passwordLengthCell!.reset(title: "Length", minimumValue: 1, maximumValue: Globals.passwordMaximumLength, defaultValue: Globals.passwordDefaultLength) - return passwordLengthCell! - } - default: - let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! TextFieldTableViewCell - cell.contentTextField.placeholder = tableTitles[indexPath.section] - return cell - } - } - - override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { - let headerView = UITableViewHeaderFooterView() - headerView.textLabel?.text = tableTitles[section].uppercased() - return headerView } override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { @@ -76,7 +34,7 @@ class AddPasswordTableViewController: UITableViewController, FillPasswordTableVi return false } // check name - let nameCell = getCellForName(name: "name")! as! TextFieldTableViewCell + let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! ContentTableViewCell if nameCell.getContent()!.isEmpty { let alertTitle = "Cannot Add Password" let alertMessage = "Please fill in the name." @@ -88,33 +46,23 @@ class AddPasswordTableViewController: UITableViewController, FillPasswordTableVi } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - if segue.identifier == "saveAddPasswordSegue" { - let nameCell = getCellForName(name: "name")! as! TextFieldTableViewCell - let passwordCell = getCellForName(name: "password")! as! FillPasswordTableViewCell - let additionsCell = getCellForName(name: "additions")! as! TextViewTableViewCell - password = Password(name: nameCell.contentTextField.text!, plainText: "\(passwordCell.contentTextField.text!)\n\(additionsCell.contentTextView.text!)") + if segue.identifier == "saveAddPasswordSegue" {let cells = tableView.visibleCells + var cellContents = [String: String]() + for cell in cells { + let indexPath = tableView.indexPath(for: cell)! + let contentCell = cell as! ContentTableViewCell + let cellTitle = tableData[indexPath.section][indexPath.row][.title] as! String + if let cellContent = contentCell.getContent() { + cellContents[cellTitle] = cellContent + } + } + var plainText = "" + if cellContents["additions"]! != "" { + plainText = "\(cellContents["password"]!)\n\(cellContents["additions"]!)" + } else { + plainText = "\(cellContents["password"]!)" + } + password = Password(name: cellContents["name"]!, plainText: plainText) } } - - override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { - return 44 - } - - override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { - return 0.1 - } - - func getCellAt(section: Int) -> UITableViewCell? { - return tableView.cellForRow(at: IndexPath(row: 0, section: section)) - } - - func getCellForName(name: String) -> UITableViewCell? { - let index = tableTitles.index(of: name)! - return getCellAt(section: Int(index)) - } - - func generatePassword() -> String { - let length = passwordLengthCell?.roundedValue ?? Globals.passwordDefaultLength - return Utils.generatePassword(length: length) - } } diff --git a/pass/Controllers/EditPasswordTableViewController.swift b/pass/Controllers/EditPasswordTableViewController.swift index 80a2024..405e321 100644 --- a/pass/Controllers/EditPasswordTableViewController.swift +++ b/pass/Controllers/EditPasswordTableViewController.swift @@ -15,12 +15,10 @@ class EditPasswordTableViewController: PasswordEditorTableViewController { override func viewDidLoad() { tableData = [ [[.type: PasswordEditorCellType.textFieldCell, .title: "name", .content: password!.name]], - [[.type: PasswordEditorCellType.fillPasswordCell, .title: "password", .content: password!.password]], + [[.type: PasswordEditorCellType.fillPasswordCell, .title: "password", .content: password!.password], + [.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]], [[.type: PasswordEditorCellType.textViewCell, .title: "additions", .content: password!.getAdditionsPlainText()]], ] - sectionHeaderTitles = ["name", "password", "additions"].map {$0.uppercased()} - sectionFooterTitles = ["", "", "It is recommended to use \"key: value\" format to store additional fields as follows:\n url: https://www.apple.com\n username: passforios@gmail.com."] - super.viewDidLoad() } @@ -47,7 +45,9 @@ class EditPasswordTableViewController: PasswordEditorTableViewController { let indexPath = tableView.indexPath(for: cell)! let contentCell = cell as! ContentTableViewCell let cellTitle = tableData[indexPath.section][indexPath.row][.title] as! String - cellContents[cellTitle] = contentCell.getContent()! + if let cellContent = contentCell.getContent() { + cellContents[cellTitle] = cellContent + } } var plainText = "" if cellContents["additions"]! != "" { diff --git a/pass/Controllers/PasswordEditorTableViewController.swift b/pass/Controllers/PasswordEditorTableViewController.swift index 365cfb2..cf9ab95 100644 --- a/pass/Controllers/PasswordEditorTableViewController.swift +++ b/pass/Controllers/PasswordEditorTableViewController.swift @@ -8,20 +8,22 @@ import UIKit enum PasswordEditorCellType { - case textFieldCell, textViewCell, fillPasswordCell + case textFieldCell, textViewCell, fillPasswordCell, passwordLengthCell } enum PasswordEditorCellKey { case type, title, content, placeholders } -class PasswordEditorTableViewController: UITableViewController { +class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate { var navigationItemTitle: String? var tableData = [ [Dictionary] ]() - var sectionHeaderTitles = [String]() - var sectionFooterTitles = [String]() + var sectionHeaderTitles = ["name", "password", "additions"].map {$0.uppercased()} + var sectionFooterTitles = ["", "", "It is recommended to use \"key: value\" format to store additional fields as follows:\n url: https://www.apple.com\n username: passforios@gmail.com."] + + var passwordLengthCell: SliderTableViewCell? override func viewDidLoad() { super.viewDidLoad() @@ -29,7 +31,7 @@ class PasswordEditorTableViewController: UITableViewController { tableView.register(UINib(nibName: "TextFieldTableViewCell", bundle: nil), forCellReuseIdentifier: "textFieldCell") tableView.register(UINib(nibName: "TextViewTableViewCell", bundle: nil), forCellReuseIdentifier: "textViewCell") tableView.register(UINib(nibName: "FillPasswordTableViewCell", bundle: nil), forCellReuseIdentifier: "fillPasswordCell") - + tableView.register(UINib(nibName: "SliderTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordLengthCell") tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 48 @@ -46,7 +48,13 @@ class PasswordEditorTableViewController: UITableViewController { case .textViewCell: cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! ContentTableViewCell case .fillPasswordCell: - cell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as! ContentTableViewCell + let fillPasswordCell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as?FillPasswordTableViewCell + fillPasswordCell?.delegate = self + cell = fillPasswordCell! + case .passwordLengthCell: + passwordLengthCell = tableView.dequeueReusableCell(withIdentifier: "passwordLengthCell", for: indexPath) as? SliderTableViewCell + passwordLengthCell?.reset(title: "Length", minimumValue: 1, maximumValue: Globals.passwordMaximumLength, defaultValue: Globals.passwordDefaultLength) + cell = passwordLengthCell! default: cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! ContentTableViewCell } @@ -75,6 +83,9 @@ class PasswordEditorTableViewController: UITableViewController { override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { return sectionFooterTitles[section] } - - + + func generatePassword() -> String { + let length = passwordLengthCell?.roundedValue ?? Globals.passwordDefaultLength + return Utils.generatePassword(length: length) + } } diff --git a/pass/Helpers/Globals.swift b/pass/Helpers/Globals.swift index ead070d..6b63372 100644 --- a/pass/Helpers/Globals.swift +++ b/pass/Helpers/Globals.swift @@ -24,8 +24,8 @@ class Globals { static let red = UIColor(red:1.00, green:0.23, blue:0.19, alpha:1.0) static let blue = UIColor(red:0.00, green:0.48, blue:1.00, alpha:1.0) - static let passwordMaximumLength = 64 - static let passwordDefaultLength = 16 + static let passwordMaximumLength = 32 + static let passwordDefaultLength = 16 private init() { } } diff --git a/pass/Views/SliderTableViewCell.swift b/pass/Views/SliderTableViewCell.swift index 3b17bb6..7de2c9d 100644 --- a/pass/Views/SliderTableViewCell.swift +++ b/pass/Views/SliderTableViewCell.swift @@ -9,7 +9,7 @@ import UIKit -class SliderTableViewCell: UITableViewCell { +class SliderTableViewCell: ContentTableViewCell { @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var valueLabel: UILabel!