From 07782bd66283dd5542ede135ebe69acc51362c7f Mon Sep 17 00:00:00 2001 From: Yishi Lin Date: Thu, 9 Mar 2017 02:19:47 +0800 Subject: [PATCH 1/3] Customizing length of passwords (while adding passwords) --- pass.xcodeproj/project.pbxproj | 8 +++ .../AddPasswordTableViewController.swift | 26 ++++++-- pass/Helpers/Globals.swift | 3 + pass/Views/FillPasswordTableViewCell.swift | 8 ++- pass/Views/SliderTableViewCell.swift | 49 +++++++++++++++ pass/Views/SliderTableViewCell.xib | 59 +++++++++++++++++++ 6 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 pass/Views/SliderTableViewCell.swift create mode 100644 pass/Views/SliderTableViewCell.xib diff --git a/pass.xcodeproj/project.pbxproj b/pass.xcodeproj/project.pbxproj index 9efeb65..8bea2c6 100644 --- a/pass.xcodeproj/project.pbxproj +++ b/pass.xcodeproj/project.pbxproj @@ -10,6 +10,8 @@ 94BA784B85E071D25EE89B59 /* libPods-pass.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ADCE7A5C3CCC67D7D21BB3C4 /* libPods-pass.a */; }; A262A5891E6871D5006B0890 /* OneTimePassword.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A262A5881E6871D5006B0890 /* OneTimePassword.framework */; }; A262A58D1E68749C006B0890 /* Base32.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A262A58C1E68749C006B0890 /* Base32.framework */; }; + A2802BF91E70813A00879216 /* SliderTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2802BF71E70813A00879216 /* SliderTableViewCell.swift */; }; + A2802BFA1E70813A00879216 /* SliderTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = A2802BF81E70813A00879216 /* SliderTableViewCell.xib */; }; DC037CA61E4B883900609409 /* OpenSourceComponentsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CA51E4B883900609409 /* OpenSourceComponentsTableViewController.swift */; }; DC037CA81E4B898100609409 /* BasicStaticTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CA71E4B898100609409 /* BasicStaticTableViewController.swift */; }; DC037CAA1E4B8EAE00609409 /* SpecialThanksTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CA91E4B8EAE00609409 /* SpecialThanksTableViewController.swift */; }; @@ -66,6 +68,8 @@ 274CCFCF32444A2FF46BE7F4 /* Pods-pass.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-pass.debug.xcconfig"; path = "Pods/Target Support Files/Pods-pass/Pods-pass.debug.xcconfig"; sourceTree = ""; }; A262A5881E6871D5006B0890 /* OneTimePassword.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OneTimePassword.framework; path = "../../Library/Developer/Xcode/DerivedData/pass-fwlmfsjroyvbfhdyqmglrwfhvjli/Build/Products/Debug-iphonesimulator/OneTimePassword/OneTimePassword.framework"; sourceTree = ""; }; A262A58C1E68749C006B0890 /* Base32.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Base32.framework; path = Carthage/Build/iOS/Base32.framework; sourceTree = ""; }; + A2802BF71E70813A00879216 /* SliderTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderTableViewCell.swift; sourceTree = ""; }; + A2802BF81E70813A00879216 /* SliderTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SliderTableViewCell.xib; sourceTree = ""; }; ADCE7A5C3CCC67D7D21BB3C4 /* libPods-pass.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-pass.a"; sourceTree = BUILT_PRODUCTS_DIR; }; AEAD6B31EAF5D061447A68CC /* Pods-pass.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-pass.release.xcconfig"; path = "Pods/Target Support Files/Pods-pass/Pods-pass.release.xcconfig"; sourceTree = ""; }; DC037CA51E4B883900609409 /* OpenSourceComponentsTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenSourceComponentsTableViewController.swift; sourceTree = ""; }; @@ -203,6 +207,8 @@ DC19400F1E4B3A9E0077E0A3 /* Views */ = { isa = PBXGroup; children = ( + A2802BF71E70813A00879216 /* SliderTableViewCell.swift */, + A2802BF81E70813A00879216 /* SliderTableViewCell.xib */, DCFB779C1E4F40C7008DE471 /* FillPasswordTableViewCell.swift */, DCFB779D1E4F40C7008DE471 /* FillPasswordTableViewCell.xib */, DCFB77981E4F3BCF008DE471 /* TitleTextFieldTableViewCell.swift */, @@ -341,6 +347,7 @@ DC917BDE1E2E8231000FDF54 /* Assets.xcassets in Resources */, DCDDEAB01E4639F300F68193 /* LabelTableViewCell.xib in Resources */, DC917BDC1E2E8231000FDF54 /* Main.storyboard in Resources */, + A2802BFA1E70813A00879216 /* SliderTableViewCell.xib in Resources */, DCFB779B1E4F3BCF008DE471 /* TitleTextFieldTableViewCell.xib in Resources */, DCFB779F1E4F40C7008DE471 /* FillPasswordTableViewCell.xib in Resources */, DC037CC01E4ED4E100609409 /* TextViewTableViewCell.xib in Resources */, @@ -446,6 +453,7 @@ DC037CA61E4B883900609409 /* OpenSourceComponentsTableViewController.swift in Sources */, DC037CA81E4B898100609409 /* BasicStaticTableViewController.swift in Sources */, DCFB779E1E4F40C7008DE471 /* FillPasswordTableViewCell.swift in Sources */, + A2802BF91E70813A00879216 /* SliderTableViewCell.swift in Sources */, DC037CB21E4CAB1700609409 /* AboutRepositoryTableViewController.swift in Sources */, DC037CB01E4CA51F00609409 /* GeneralSettingsTableViewController.swift in Sources */, DC037CB81E4DD1A500609409 /* AddPasswordTableViewController.swift in Sources */, diff --git a/pass/Controllers/AddPasswordTableViewController.swift b/pass/Controllers/AddPasswordTableViewController.swift index a8eefec..d98c52e 100644 --- a/pass/Controllers/AddPasswordTableViewController.swift +++ b/pass/Controllers/AddPasswordTableViewController.swift @@ -8,16 +8,19 @@ import UIKit -class AddPasswordTableViewController: UITableViewController { +class AddPasswordTableViewController: UITableViewController, FillPasswordTableViewCellDelegate { let tableTitles = ["name", "password", "additions"] + let tableRowsInSection = [1, 2, 1] var password: Password? + + var passwordLengthCell: SliderTableViewCell? override func viewDidLoad() { 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 @@ -25,7 +28,7 @@ class AddPasswordTableViewController: UITableViewController { } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return 1 + return tableRowsInSection[section] } override func numberOfSections(in tableView: UITableView) -> Int { @@ -39,8 +42,16 @@ class AddPasswordTableViewController: UITableViewController { cell.contentTextView.text = "" return cell case "password": - let cell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as! FillPasswordTableViewCell - return cell + 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] @@ -78,4 +89,9 @@ class AddPasswordTableViewController: UITableViewController { 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/Helpers/Globals.swift b/pass/Helpers/Globals.swift index eb79f15..ead070d 100644 --- a/pass/Helpers/Globals.swift +++ b/pass/Helpers/Globals.swift @@ -24,6 +24,9 @@ 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 + private init() { } } diff --git a/pass/Views/FillPasswordTableViewCell.swift b/pass/Views/FillPasswordTableViewCell.swift index a22b88e..75dfb9f 100644 --- a/pass/Views/FillPasswordTableViewCell.swift +++ b/pass/Views/FillPasswordTableViewCell.swift @@ -8,9 +8,15 @@ import UIKit +protocol FillPasswordTableViewCellDelegate { + func generatePassword() -> String +} + class FillPasswordTableViewCell: ContentTableViewCell { @IBOutlet weak var contentTextField: UITextField! + var delegate: FillPasswordTableViewCellDelegate? + override func awakeFromNib() { super.awakeFromNib() // Initialization code @@ -23,7 +29,7 @@ class FillPasswordTableViewCell: ContentTableViewCell { } @IBAction func generatePassword(_ sender: UIButton) { - let plainPassword = Utils.generatePassword(length: 16) + let plainPassword = self.delegate?.generatePassword() ?? Utils.generatePassword(length: 16) contentTextField.attributedText = Utils.attributedPassword(plainPassword: plainPassword) Utils.copyToPasteboard(textToCopy: plainPassword) } diff --git a/pass/Views/SliderTableViewCell.swift b/pass/Views/SliderTableViewCell.swift new file mode 100644 index 0000000..3b17bb6 --- /dev/null +++ b/pass/Views/SliderTableViewCell.swift @@ -0,0 +1,49 @@ +// +// SliderTableViewCell.swift +// pass +// +// Created by Yishi Lin on 8/3/17. +// Copyright © 2017 Yishi Lin. All rights reserved. +// + + +import UIKit + +class SliderTableViewCell: UITableViewCell { + + @IBOutlet weak var titleLabel: UILabel! + @IBOutlet weak var valueLabel: UILabel! + @IBOutlet weak var slider: UISlider! + + var roundedValue: Int { + get { + return Int(slider.value) + } + } + + override func awakeFromNib() { + super.awakeFromNib() + // Initialization code + } + + override func setSelected(_ selected: Bool, animated: Bool) { + super.setSelected(selected, animated: animated) + + // Configure the view for the selected state + } + + @IBAction func handleSliderValueChange(_ sender: UISlider) { + let roundedValue = round(sender.value) + sender.value = roundedValue + valueLabel.text = "\(Int(roundedValue))" + } + + func reset(title: String, minimumValue: Int, maximumValue: Int, defaultValue: Int) { + titleLabel.text = title + slider.minimumValue = Float(minimumValue) + slider.maximumValue = Float(maximumValue) + slider.value = Float(defaultValue) + valueLabel.text = String(defaultValue) + } + +} diff --git a/pass/Views/SliderTableViewCell.xib b/pass/Views/SliderTableViewCell.xib new file mode 100644 index 0000000..902fa88 --- /dev/null +++ b/pass/Views/SliderTableViewCell.xib @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 56edfcfb9c59375b2d9148fa48cb91326f499c60 Mon Sep 17 00:00:00 2001 From: Yishi Lin Date: Thu, 9 Mar 2017 02:39:13 +0800 Subject: [PATCH 2/3] Fix a bug about adding new password with empty name / without PGP key --- .../AddPasswordTableViewController.swift | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pass/Controllers/AddPasswordTableViewController.swift b/pass/Controllers/AddPasswordTableViewController.swift index d98c52e..7ae8572 100644 --- a/pass/Controllers/AddPasswordTableViewController.swift +++ b/pass/Controllers/AddPasswordTableViewController.swift @@ -7,6 +7,7 @@ // import UIKit +import SwiftyUserDefaults class AddPasswordTableViewController: UITableViewController, FillPasswordTableViewCellDelegate { let tableTitles = ["name", "password", "additions"] @@ -65,6 +66,27 @@ class AddPasswordTableViewController: UITableViewController, FillPasswordTableVi return headerView } + override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { + if identifier == "saveAddPasswordSegue" { + // check PGP key + if Defaults[.pgpKeyID] == nil { + let alertTitle = "Cannot Add Password" + let alertMessage = "PGP Key is not set. Please set your PGP Key first." + Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil) + return false + } + // check name + let nameCell = getCellForName(name: "name")! as! TextFieldTableViewCell + if nameCell.getContent()!.isEmpty { + let alertTitle = "Cannot Add Password" + let alertMessage = "Please fill in the name." + Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil) + return false + } + } + return true + } + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "saveAddPasswordSegue" { let nameCell = getCellForName(name: "name")! as! TextFieldTableViewCell @@ -73,6 +95,7 @@ class AddPasswordTableViewController: UITableViewController, FillPasswordTableVi password = Password(name: nameCell.contentTextField.text!, plainText: "\(passwordCell.contentTextField.text!)\n\(additionsCell.contentTextView.text!)") } } + override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 44 } From 5116cd4f3dde6af6621b77bd3b3d3e162fc3ae51 Mon Sep 17 00:00:00 2001 From: Yishi Lin Date: Thu, 9 Mar 2017 03:19:36 +0800 Subject: [PATCH 3/3] Polish code about edit/add passwords --- .../AddPasswordTableViewController.swift | 106 +++++------------- .../EditPasswordTableViewController.swift | 10 +- .../PasswordEditorTableViewController.swift | 27 +++-- pass/Helpers/Globals.swift | 4 +- pass/Views/SliderTableViewCell.swift | 2 +- 5 files changed, 54 insertions(+), 95 deletions(-) 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!