From 2d5ca58bd95730e5c31a944820aab826b137394a Mon Sep 17 00:00:00 2001 From: Danny Moesch Date: Mon, 14 Jan 2019 20:24:56 +0100 Subject: [PATCH] Use PasswordGeneratorFlavor class to set user defaults and UI elements --- .../Controllers/AddPasswordTableViewController.swift | 2 +- .../EditPasswordTableViewController.swift | 2 +- .../GeneralSettingsTableViewController.swift | 12 ++++++------ pass/en.lproj/Localizable.strings | 2 ++ passKit/Helpers/PasswordGeneratorFlavour.swift | 4 ++++ .../Helpers/PasswordGeneratorFlavourTest.swift | 5 +++++ 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/pass/Controllers/AddPasswordTableViewController.swift b/pass/Controllers/AddPasswordTableViewController.swift index afaa904..c6755e5 100644 --- a/pass/Controllers/AddPasswordTableViewController.swift +++ b/pass/Controllers/AddPasswordTableViewController.swift @@ -20,7 +20,7 @@ class AddPasswordTableViewController: PasswordEditorTableViewController { [[.type: PasswordEditorCellType.additionsCell, .title: "additions"]], [[.type: PasswordEditorCellType.scanQRCodeCell]] ] - if PasswordGeneratorFlavour.from(SharedDefaults[.passwordGeneratorFlavor]) == PasswordGeneratorFlavour.RANDOM { + if PasswordGeneratorFlavour.from(SharedDefaults[.passwordGeneratorFlavor]) == .RANDOM { tableData[1].append([.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]) } tableData[1].append([.type: PasswordEditorCellType.memorablePasswordGeneratorCell]) diff --git a/pass/Controllers/EditPasswordTableViewController.swift b/pass/Controllers/EditPasswordTableViewController.swift index ab58251..e8d4887 100644 --- a/pass/Controllers/EditPasswordTableViewController.swift +++ b/pass/Controllers/EditPasswordTableViewController.swift @@ -18,7 +18,7 @@ class EditPasswordTableViewController: PasswordEditorTableViewController { [[.type: PasswordEditorCellType.scanQRCodeCell], [.type: PasswordEditorCellType.deletePasswordCell]] ] - if PasswordGeneratorFlavour.from(SharedDefaults[.passwordGeneratorFlavor]) == PasswordGeneratorFlavour.RANDOM { + if PasswordGeneratorFlavour.from(SharedDefaults[.passwordGeneratorFlavor]) == .RANDOM { tableData[1].append([.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]) } tableData[1].append([.type: PasswordEditorCellType.memorablePasswordGeneratorCell]) diff --git a/pass/Controllers/GeneralSettingsTableViewController.swift b/pass/Controllers/GeneralSettingsTableViewController.swift index 697e0cd..e8059a9 100644 --- a/pass/Controllers/GeneralSettingsTableViewController.swift +++ b/pass/Controllers/GeneralSettingsTableViewController.swift @@ -122,7 +122,7 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { cell.accessoryView = showFolderSwitch case "Password Generator Flavor": cell.accessoryType = .disclosureIndicator - cell.detailTextLabel?.text = SharedDefaults[.passwordGeneratorFlavor] + cell.detailTextLabel?.text = PasswordGeneratorFlavour.from(SharedDefaults[.passwordGeneratorFlavor]).name default: break } return cell @@ -141,21 +141,21 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) var randomFlavorActionTitle = "" var appleFlavorActionTitle = "" - if SharedDefaults[.passwordGeneratorFlavor] == "Random" { randomFlavorActionTitle = "✓ Random String" appleFlavorActionTitle = "Apple's Keychain Style" + if SharedDefaults[.passwordGeneratorFlavor] == PasswordGeneratorFlavour.RANDOM.rawValue { } else { randomFlavorActionTitle = "Random String" appleFlavorActionTitle = "✓ Apple's Keychain Style" } let randomFlavorAction = UIAlertAction(title: randomFlavorActionTitle, style: .default) { _ in - SharedDefaults[.passwordGeneratorFlavor] = "Random" - sourceCell.detailTextLabel?.text = "Random" + SharedDefaults[.passwordGeneratorFlavor] = PasswordGeneratorFlavour.RANDOM.rawValue + sourceCell.detailTextLabel?.text = PasswordGeneratorFlavour.RANDOM.name } let appleFlavorAction = UIAlertAction(title: appleFlavorActionTitle, style: .default) { _ in - SharedDefaults[.passwordGeneratorFlavor] = "Apple" - sourceCell.detailTextLabel?.text = "Apple" + SharedDefaults[.passwordGeneratorFlavor] = PasswordGeneratorFlavour.APPLE.rawValue + sourceCell.detailTextLabel?.text = PasswordGeneratorFlavour.APPLE.name } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) diff --git a/pass/en.lproj/Localizable.strings b/pass/en.lproj/Localizable.strings index 0cc0860..f0caa01 100644 --- a/pass/en.lproj/Localizable.strings +++ b/pass/en.lproj/Localizable.strings @@ -6,3 +6,5 @@ Copyright © 2019 Bob Sun. All rights reserved. */ +"Apple" = "Apple"; +"Random" = "Random"; diff --git a/passKit/Helpers/PasswordGeneratorFlavour.swift b/passKit/Helpers/PasswordGeneratorFlavour.swift index 6ac9b27..af67825 100644 --- a/passKit/Helpers/PasswordGeneratorFlavour.swift +++ b/passKit/Helpers/PasswordGeneratorFlavour.swift @@ -18,6 +18,10 @@ public enum PasswordGeneratorFlavour: String { return PasswordGeneratorFlavour(rawValue: option) ?? PasswordGeneratorFlavour.RANDOM } + public var name: String { + return rawValue.localize() + } + public var defaultLength: (min: Int, max: Int, def: Int) { switch self { case .APPLE: diff --git a/passKitTests/Helpers/PasswordGeneratorFlavourTest.swift b/passKitTests/Helpers/PasswordGeneratorFlavourTest.swift index 9fe79fa..ab17d6f 100644 --- a/passKitTests/Helpers/PasswordGeneratorFlavourTest.swift +++ b/passKitTests/Helpers/PasswordGeneratorFlavourTest.swift @@ -22,6 +22,11 @@ class PasswordGeneratorFlavourTest: XCTestCase { XCTAssertEqual(PasswordGeneratorFlavour.from(""), PasswordGeneratorFlavour.RANDOM) } + func testLocalizedName() { + XCTAssertEqual(PasswordGeneratorFlavour.APPLE.name, "Apple".localize()) + XCTAssertEqual(PasswordGeneratorFlavour.RANDOM.name, "Random".localize()) + } + func testDefaultLength() { // Ensure properly chosen default length values. So this check no longer needs to be performed in the code. PasswordGeneratorFlavour.allCases.map { $0.defaultLength }.forEach { defaultLength in