From eca4d79178112f153362977b65a188d07a07f96c Mon Sep 17 00:00:00 2001 From: Danny Moesch Date: Sat, 7 Mar 2020 23:32:59 +0100 Subject: [PATCH] Implement selection of password generator flavor with segmented control --- .../PasswordEditorTableViewController.swift | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/pass/Controllers/PasswordEditorTableViewController.swift b/pass/Controllers/PasswordEditorTableViewController.swift index 127850a..c237b4f 100644 --- a/pass/Controllers/PasswordEditorTableViewController.swift +++ b/pass/Controllers/PasswordEditorTableViewController.swift @@ -82,11 +82,14 @@ class PasswordEditorTableViewController: UITableViewController { scanQRCodeCell?.selectionStyle = .default scanQRCodeCell?.accessoryType = .disclosureIndicator - passwordFlavorCell = UITableViewCell(style: .value1, reuseIdentifier: "default") + passwordFlavorCell = UITableViewCell(style: .default, reuseIdentifier: "default") passwordFlavorCell?.textLabel?.text = "PasswordGeneratorFlavor".localize() passwordFlavorCell?.selectionStyle = .none - passwordFlavorCell?.accessoryType = .disclosureIndicator - passwordFlavorCell?.detailTextLabel?.text = passwordGenerator.flavor.localized + + let passwordFlavorSelector = UISegmentedControl(items: PasswordGeneratorFlavor.allCases.map { $0.localized }) + passwordFlavorSelector.selectedSegmentIndex = PasswordGeneratorFlavor.allCases.firstIndex(of: passwordGenerator.flavor)! + passwordFlavorSelector.addTarget(self, action: #selector(flavorChanged), for: .valueChanged) + passwordFlavorCell?.accessoryView = passwordFlavorSelector } override func viewDidLoad() { @@ -112,11 +115,11 @@ class PasswordEditorTableViewController: UITableViewController { ], [ [.type: PasswordEditorCellType.fillPasswordCell, .title: "Password".localize(), .content: password?.password ?? ""], + [.type: PasswordEditorCellType.passwordFlavorCell], [.type: PasswordEditorCellType.passwordLengthCell], [.type: PasswordEditorCellType.passwordUseDigitsCell], [.type: PasswordEditorCellType.passwordVaryCasesCell], [.type: PasswordEditorCellType.passwordUseSpecialSymbols], - [.type: PasswordEditorCellType.passwordFlavorCell], ], [ [.type: PasswordEditorCellType.additionsCell, .title: "Additions".localize(), .content: password?.additionsPlainText ?? ""], @@ -296,7 +299,7 @@ class PasswordEditorTableViewController: UITableViewController { guard tableData[1].first(where: isPasswordDelimiterCellData) == nil else { return } - tableData[1].insert([.type: PasswordEditorCellType.passwordGroupsCell], at: tableData[1].endIndex - 1) + tableData[1].insert([.type: PasswordEditorCellType.passwordGroupsCell], at: tableData[1].endIndex) case .xkcd: tableData[1].removeAll(where: isPasswordDelimiterCellData) } @@ -306,6 +309,17 @@ class PasswordEditorTableViewController: UITableViewController { return (data[.type] as? PasswordEditorCellType) == .some(.passwordGroupsCell) } + @objc func flavorChanged(_ sender: UISegmentedControl) { + let flavor = PasswordGeneratorFlavor.allCases[sender.selectedSegmentIndex] + guard passwordGenerator.flavor != flavor else { + return + } + passwordGenerator.flavor = flavor + updateTableData(withRespectTo: flavor) + tableView.reloadSections([passwordSection], with: .none) + generateAndCopyPassword() + } + // generate the password, don't care whether the original line is otp private func generateAndCopyPasswordNoOtpCheck() { // show password settings (e.g., the length slider)