Polish code about edit/add passwords

This commit is contained in:
Yishi Lin 2017-03-09 03:19:36 +08:00
parent 56edfcfb9c
commit 5116cd4f3d
5 changed files with 54 additions and 95 deletions

View file

@ -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)
}
}

View file

@ -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"]! != "" {

View file

@ -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<PasswordEditorCellKey, Any>]
]()
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)
}
}