Polish codes related to password generation

1. Polish codes in PasswordGeneratorFlavour
2. Polish related codes in view controllers
This commit is contained in:
Yishi Lin 2020-02-22 23:23:02 +08:00
parent ae94388ba4
commit 71c793029a
10 changed files with 108 additions and 127 deletions

View file

@ -10,22 +10,11 @@ import UIKit
import passKit
class AddPasswordTableViewController: PasswordEditorTableViewController {
let passwordStore = PasswordStore.shared
var defaultDirPrefix = ""
override func viewDidLoad() {
tableData = [
[[.type: PasswordEditorCellType.nameCell, .title: "name"]],
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "password"]],
[[.type: PasswordEditorCellType.additionsCell, .title: "additions"]],
[[.type: PasswordEditorCellType.scanQRCodeCell]]
]
if PasswordGeneratorFlavour.from(Defaults.passwordGeneratorFlavor) == .RANDOM {
tableData[1].append([.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"])
}
tableData[1].append([.type: PasswordEditorCellType.memorablePasswordGeneratorCell])
tableData[0][0][PasswordEditorCellKey.content] = defaultDirPrefix
super.viewDidLoad()
tableData[0][0][PasswordEditorCellKey.content] = defaultDirPrefix
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
@ -39,7 +28,7 @@ class AddPasswordTableViewController: PasswordEditorTableViewController {
}
// check name
guard checkName() == true else {
guard checkName() else {
return false
}
}

View file

@ -10,25 +10,10 @@ import UIKit
import passKit
class EditPasswordTableViewController: PasswordEditorTableViewController {
override func viewDidLoad() {
tableData = [
[[.type: PasswordEditorCellType.nameCell, .title: "Name".localize(), .content: password!.namePath]],
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "Password".localize(), .content: password!.password]],
[[.type: PasswordEditorCellType.additionsCell, .title: "Additions".localize(), .content: password!.additionsPlainText]],
[[.type: PasswordEditorCellType.scanQRCodeCell],
[.type: PasswordEditorCellType.deletePasswordCell]]
]
if PasswordGeneratorFlavour.from(Defaults.passwordGeneratorFlavor) == .RANDOM {
tableData[1].append([.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"])
}
tableData[1].append([.type: PasswordEditorCellType.memorablePasswordGeneratorCell])
super.viewDidLoad()
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "saveEditPasswordSegue" {
// check name
guard checkName() == true else {
guard checkName() else {
return false
}
}

View file

@ -144,7 +144,7 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController {
hidePasswordImagesSwitch.isOn = Defaults.isHidePasswordImagesOn
case "PasswordGeneratorFlavor".localize():
cell.accessoryType = .disclosureIndicator
cell.detailTextLabel?.text = PasswordGeneratorFlavour.from(Defaults.passwordGeneratorFlavor).name
cell.detailTextLabel?.text = Defaults.passwordGeneratorFlavor.localized
default: break
}
return cell
@ -161,31 +161,26 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController {
func showPasswordGeneratorFlavorActionSheet(sourceCell: UITableViewCell) {
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
var randomFlavorActionTitle = ""
var appleFlavorActionTitle = ""
if Defaults.passwordGeneratorFlavor == PasswordGeneratorFlavour.RANDOM.rawValue {
randomFlavorActionTitle = "" + "RandomString".localize()
appleFlavorActionTitle = "ApplesKeychainStyle".localize()
} else {
randomFlavorActionTitle = "RandomString".localize()
appleFlavorActionTitle = "" + "ApplesKeychainStyle".localize()
}
let randomFlavorAction = UIAlertAction(title: randomFlavorActionTitle, style: .default) { _ in
Defaults.passwordGeneratorFlavor = PasswordGeneratorFlavour.RANDOM.rawValue
sourceCell.detailTextLabel?.text = PasswordGeneratorFlavour.RANDOM.name
}
let appleFlavorAction = UIAlertAction(title: appleFlavorActionTitle, style: .default) { _ in
Defaults.passwordGeneratorFlavor = PasswordGeneratorFlavour.APPLE.rawValue
sourceCell.detailTextLabel?.text = PasswordGeneratorFlavour.APPLE.name
PasswordGeneratorFlavor.allCases.forEach { flavor in
let actionTitlePrefix = Defaults.passwordGeneratorFlavor
var actionTitle = flavor.longNameLocalized
if Defaults.passwordGeneratorFlavor == flavor {
actionTitle = "" + actionTitle
}
let action = UIAlertAction(title: actionTitle, style: .default) { _ in
Defaults.passwordGeneratorFlavor = flavor
sourceCell.detailTextLabel?.text = Defaults.passwordGeneratorFlavor.localized
}
optionMenu.addAction(action)
}
let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil)
optionMenu.addAction(randomFlavorAction)
optionMenu.addAction(appleFlavorAction)
optionMenu.addAction(cancelAction)
optionMenu.popoverPresentationController?.sourceView = sourceCell
optionMenu.popoverPresentationController?.sourceRect = sourceCell.bounds
self.present(optionMenu, animated: true, completion: nil)
}

View file

@ -89,9 +89,22 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 48
self.tableView.sectionFooterHeight = UITableView.automaticDimension;
self.tableView.estimatedSectionFooterHeight = 0;
self.tableView.sectionFooterHeight = UITableView.automaticDimension
self.tableView.estimatedSectionFooterHeight = 0
tableData = [
[[.type: PasswordEditorCellType.nameCell, .title: "Name".localize(), .content: password?.namePath ?? ""]],
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "Password".localize(), .content: password?.password ?? ""]],
[[.type: PasswordEditorCellType.additionsCell, .title: "Additions".localize(), .content: password?.additionsPlainText ?? ""]],
[[.type: PasswordEditorCellType.scanQRCodeCell],
[.type: PasswordEditorCellType.deletePasswordCell]]
]
if Defaults.passwordGeneratorFlavor == .random {
tableData[1].append([.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"])
}
tableData[1].append([.type: PasswordEditorCellType.memorablePasswordGeneratorCell])
}
override func viewDidLayoutSubviews() {
additionsCell?.contentTextView.setContentOffset(.zero, animated: false)
}
@ -116,7 +129,7 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
return fillPasswordCell!
case .passwordLengthCell:
passwordLengthCell = tableView.dequeueReusableCell(withIdentifier: "passwordLengthCell", for: indexPath) as? SliderTableViewCell
let lengthSetting = PasswordGeneratorFlavour.from(Defaults.passwordGeneratorFlavor).defaultLength
let lengthSetting = Defaults.passwordGeneratorFlavor.defaultLength
let minimumLength = lengthSetting.min
let maximumLength = lengthSetting.max
var defaultLength = lengthSetting.def
@ -215,7 +228,7 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
showPasswordSettings()
let length = passwordLengthCell?.roundedValue ?? 0
let plainPassword = PasswordGeneratorFlavour.from(Defaults.passwordGeneratorFlavor).generatePassword(length: length)
let plainPassword = Defaults.passwordGeneratorFlavor.generate(length: length)
// update tableData so to make sure reloadData() works correctly
tableData[passwordSection][0][PasswordEditorCellKey.content] = plainPassword