From 165af8588a0b41b180cce07eec4bf9c62c1d557d Mon Sep 17 00:00:00 2001 From: Yishi Lin Date: Thu, 27 Apr 2017 23:07:22 +0800 Subject: [PATCH] Fix the name cell of the password editor table - Update the data table after user's editing. --- .../AddPasswordTableViewController.swift | 4 +- .../EditPasswordTableViewController.swift | 4 +- .../PasswordEditorTableViewController.swift | 39 ++++++++++++++----- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/pass/Controllers/AddPasswordTableViewController.swift b/pass/Controllers/AddPasswordTableViewController.swift index 1f172df..1fad471 100644 --- a/pass/Controllers/AddPasswordTableViewController.swift +++ b/pass/Controllers/AddPasswordTableViewController.swift @@ -15,10 +15,10 @@ class AddPasswordTableViewController: PasswordEditorTableViewController { override func viewDidLoad() { tableData = [ - [[.type: PasswordEditorCellType.textFieldCell, .title: "name"]], + [[.type: PasswordEditorCellType.nameCell, .title: "name"]], [[.type: PasswordEditorCellType.fillPasswordCell, .title: "password"], [.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]], - [[.type: PasswordEditorCellType.textViewCell, .title: "additions"]], + [[.type: PasswordEditorCellType.additionsCell, .title: "additions"]], [[.type: PasswordEditorCellType.scanQRCodeCell]] ] super.viewDidLoad() diff --git a/pass/Controllers/EditPasswordTableViewController.swift b/pass/Controllers/EditPasswordTableViewController.swift index c2c6681..26577b8 100644 --- a/pass/Controllers/EditPasswordTableViewController.swift +++ b/pass/Controllers/EditPasswordTableViewController.swift @@ -11,10 +11,10 @@ import UIKit class EditPasswordTableViewController: PasswordEditorTableViewController { override func viewDidLoad() { 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.passwordLengthCell, .title: "passwordlength"]], - [[.type: PasswordEditorCellType.textViewCell, .title: "additions", .content: password!.getAdditionsPlainText()]], + [[.type: PasswordEditorCellType.additionsCell, .title: "additions", .content: password!.getAdditionsPlainText()]], [[.type: PasswordEditorCellType.scanQRCodeCell], [.type: PasswordEditorCellType.deletePasswordCell]] ] diff --git a/pass/Controllers/PasswordEditorTableViewController.swift b/pass/Controllers/PasswordEditorTableViewController.swift index 99760c1..191a60b 100644 --- a/pass/Controllers/PasswordEditorTableViewController.swift +++ b/pass/Controllers/PasswordEditorTableViewController.swift @@ -11,14 +11,14 @@ import SwiftyUserDefaults import OneTimePassword enum PasswordEditorCellType { - case textFieldCell, textViewCell, fillPasswordCell, passwordLengthCell, deletePasswordCell, scanQRCodeCell + case nameCell, fillPasswordCell, passwordLengthCell, additionsCell, deletePasswordCell, scanQRCodeCell } enum PasswordEditorCellKey { case type, title, content, placeholders } -class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate, PasswordSettingSliderTableViewCellDelegate, QRScannerControllerDelegate { +class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate, PasswordSettingSliderTableViewCellDelegate, QRScannerControllerDelegate, UITextFieldDelegate, UITextViewDelegate { var tableData = [ [Dictionary] @@ -29,12 +29,15 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl private var sectionHeaderTitles = ["name", "password", "additions",""].map {$0.uppercased()} private var sectionFooterTitles = ["", "", "Use \"key: value\" format for additional fields.", ""] + private let nameSection = 0 private let passwordSection = 1 private let additionsSection = 2 private var hidePasswordSettings = true + private var nameCell: TextFieldTableViewCell? private var fillPasswordCell: FillPasswordTableViewCell? private var passwordLengthCell: SliderTableViewCell? + private var additionsCell: TextViewTableViewCell? private var deletePasswordCell: UITableViewCell? private var scanQRCodeCell: UITableViewCell? @@ -77,10 +80,11 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl let cellData = tableData[indexPath.section][indexPath.row] switch cellData[PasswordEditorCellKey.type] as! PasswordEditorCellType { - case .textViewCell: - let cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! ContentTableViewCell - cell.setContent(content: cellData[PasswordEditorCellKey.content] as? String) - return cell + case .nameCell: + nameCell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as? TextFieldTableViewCell + nameCell?.contentTextField.delegate = self + nameCell?.setContent(content: cellData[PasswordEditorCellKey.content] as? String) + return nameCell! case .fillPasswordCell: fillPasswordCell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as? FillPasswordTableViewCell fillPasswordCell?.delegate = self @@ -96,14 +100,15 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl defaultValue: lengthSetting?.def ?? 0) passwordLengthCell?.delegate = self 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: return deletePasswordCell! case .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) { } + + // 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() + } + } }