Fix the name cell of the password editor table

- Update the data table after user's editing.
This commit is contained in:
Yishi Lin 2017-04-27 23:07:22 +08:00
parent 98cc71bf30
commit 165af8588a
3 changed files with 33 additions and 14 deletions

View file

@ -15,10 +15,10 @@ class AddPasswordTableViewController: PasswordEditorTableViewController {
override func viewDidLoad() { override func viewDidLoad() {
tableData = [ tableData = [
[[.type: PasswordEditorCellType.textFieldCell, .title: "name"]], [[.type: PasswordEditorCellType.nameCell, .title: "name"]],
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "password"], [[.type: PasswordEditorCellType.fillPasswordCell, .title: "password"],
[.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]], [.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]],
[[.type: PasswordEditorCellType.textViewCell, .title: "additions"]], [[.type: PasswordEditorCellType.additionsCell, .title: "additions"]],
[[.type: PasswordEditorCellType.scanQRCodeCell]] [[.type: PasswordEditorCellType.scanQRCodeCell]]
] ]
super.viewDidLoad() super.viewDidLoad()

View file

@ -11,10 +11,10 @@ import UIKit
class EditPasswordTableViewController: PasswordEditorTableViewController { class EditPasswordTableViewController: PasswordEditorTableViewController {
override func viewDidLoad() { override func viewDidLoad() {
tableData = [ tableData = [
[[.type: PasswordEditorCellType.textFieldCell, .title: "name", .content: password!.namePath]], [[.type: PasswordEditorCellType.nameCell, .title: "name", .content: password!.namePath]],
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "password", .content: password!.password], [[.type: PasswordEditorCellType.fillPasswordCell, .title: "password", .content: password!.password],
[.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]], [.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]],
[[.type: PasswordEditorCellType.textViewCell, .title: "additions", .content: password!.getAdditionsPlainText()]], [[.type: PasswordEditorCellType.additionsCell, .title: "additions", .content: password!.getAdditionsPlainText()]],
[[.type: PasswordEditorCellType.scanQRCodeCell], [[.type: PasswordEditorCellType.scanQRCodeCell],
[.type: PasswordEditorCellType.deletePasswordCell]] [.type: PasswordEditorCellType.deletePasswordCell]]
] ]

View file

@ -11,14 +11,14 @@ import SwiftyUserDefaults
import OneTimePassword import OneTimePassword
enum PasswordEditorCellType { enum PasswordEditorCellType {
case textFieldCell, textViewCell, fillPasswordCell, passwordLengthCell, deletePasswordCell, scanQRCodeCell case nameCell, fillPasswordCell, passwordLengthCell, additionsCell, deletePasswordCell, scanQRCodeCell
} }
enum PasswordEditorCellKey { enum PasswordEditorCellKey {
case type, title, content, placeholders case type, title, content, placeholders
} }
class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate, PasswordSettingSliderTableViewCellDelegate, QRScannerControllerDelegate { class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate, PasswordSettingSliderTableViewCellDelegate, QRScannerControllerDelegate, UITextFieldDelegate, UITextViewDelegate {
var tableData = [ var tableData = [
[Dictionary<PasswordEditorCellKey, Any>] [Dictionary<PasswordEditorCellKey, Any>]
@ -29,12 +29,15 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
private var sectionHeaderTitles = ["name", "password", "additions",""].map {$0.uppercased()} private var sectionHeaderTitles = ["name", "password", "additions",""].map {$0.uppercased()}
private var sectionFooterTitles = ["", "", "Use \"key: value\" format for additional fields.", ""] private var sectionFooterTitles = ["", "", "Use \"key: value\" format for additional fields.", ""]
private let nameSection = 0
private let passwordSection = 1 private let passwordSection = 1
private let additionsSection = 2 private let additionsSection = 2
private var hidePasswordSettings = true private var hidePasswordSettings = true
private var nameCell: TextFieldTableViewCell?
private var fillPasswordCell: FillPasswordTableViewCell? private var fillPasswordCell: FillPasswordTableViewCell?
private var passwordLengthCell: SliderTableViewCell? private var passwordLengthCell: SliderTableViewCell?
private var additionsCell: TextViewTableViewCell?
private var deletePasswordCell: UITableViewCell? private var deletePasswordCell: UITableViewCell?
private var scanQRCodeCell: UITableViewCell? private var scanQRCodeCell: UITableViewCell?
@ -77,10 +80,11 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
let cellData = tableData[indexPath.section][indexPath.row] let cellData = tableData[indexPath.section][indexPath.row]
switch cellData[PasswordEditorCellKey.type] as! PasswordEditorCellType { switch cellData[PasswordEditorCellKey.type] as! PasswordEditorCellType {
case .textViewCell: case .nameCell:
let cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! ContentTableViewCell nameCell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as? TextFieldTableViewCell
cell.setContent(content: cellData[PasswordEditorCellKey.content] as? String) nameCell?.contentTextField.delegate = self
return cell nameCell?.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
return nameCell!
case .fillPasswordCell: case .fillPasswordCell:
fillPasswordCell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as? FillPasswordTableViewCell fillPasswordCell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as? FillPasswordTableViewCell
fillPasswordCell?.delegate = self fillPasswordCell?.delegate = self
@ -96,14 +100,15 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
defaultValue: lengthSetting?.def ?? 0) defaultValue: lengthSetting?.def ?? 0)
passwordLengthCell?.delegate = self passwordLengthCell?.delegate = self
return passwordLengthCell! return passwordLengthCell!
case .additionsCell:
additionsCell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as?TextViewTableViewCell
additionsCell?.contentTextView.delegate = self
additionsCell?.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
return additionsCell!
case .deletePasswordCell: case .deletePasswordCell:
return deletePasswordCell! return deletePasswordCell!
case .scanQRCodeCell: case .scanQRCodeCell:
return scanQRCodeCell! return scanQRCodeCell!
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! ContentTableViewCell
cell.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
return cell
} }
} }
@ -226,4 +231,18 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
@IBAction private func cancelOTPScanner(segue: UIStoryboardSegue) { @IBAction private func cancelOTPScanner(segue: UIStoryboardSegue) {
} }
// update the data table after editing
func textFieldDidEndEditing(_ textField: UITextField) {
if textField == nameCell?.contentTextField {
tableData[nameSection][0][PasswordEditorCellKey.content] = nameCell?.getContent()
}
}
// update the data table after editing
func textViewDidEndEditing(_ textView: UITextView) {
if textView == additionsCell?.contentTextView {
tableData[additionsSection][0][PasswordEditorCellKey.content] = additionsCell?.getContent()
}
}
} }