Add option to select two flavors of password generator: random and apple's (keychain)

This commit is contained in:
Bob Sun 2017-03-08 00:57:49 -08:00
parent 3cf88dcbc8
commit ad0c39b541
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
5 changed files with 59 additions and 11 deletions

View file

@ -33,6 +33,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.perform(#selector(postSearchNotification), with: nil, afterDelay: 0.4) self.perform(#selector(postSearchNotification), with: nil, afterDelay: 0.4)
} }
} }
Utils.initDefaultKeys()
return true return true
} }

View file

@ -52,6 +52,11 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController {
[[.title: "About Repository", .action: "segue", .link: "showAboutRepositorySegue"],], [[.title: "About Repository", .action: "segue", .link: "showAboutRepositorySegue"],],
// section 1 // section 1
[
[.title: "Password Generator Flavor", .action: "none", .style: CellDataStyle.value1],
],
// section 2
[ [
[.title: "Remember Passphrase", .action: "none",], [.title: "Remember Passphrase", .action: "none",],
], ],
@ -81,7 +86,7 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController {
cell.accessoryView = accessoryView cell.accessoryView = accessoryView
cell.selectionStyle = .none cell.selectionStyle = .none
hideUnknownSwitch.isOn = Defaults[.isHideUnknownOn] hideUnknownSwitch.isOn = Defaults[.isHideUnknownOn]
case "Hide One Time Password Fields": case "Hide OTP Fields":
cell.accessoryType = .none cell.accessoryType = .none
let detailButton = UIButton(type: .detailDisclosure) let detailButton = UIButton(type: .detailDisclosure)
hideOTPSwitch.frame = CGRect(x: detailButton.bounds.width+10, y: 0, width: hideOTPSwitch.bounds.width, height: hideOTPSwitch.bounds.height) hideOTPSwitch.frame = CGRect(x: detailButton.bounds.width+10, y: 0, width: hideOTPSwitch.bounds.width, height: hideOTPSwitch.bounds.height)
@ -101,11 +106,53 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController {
cell.accessoryType = .none cell.accessoryType = .none
cell.selectionStyle = .none cell.selectionStyle = .none
cell.accessoryView = showFolderSwitch cell.accessoryView = showFolderSwitch
case "Password Generator Flavor":
cell.accessoryType = .disclosureIndicator
cell.detailTextLabel?.text = Defaults[.passwordGeneratorFlavor]
default: break default: break
} }
return cell return cell
} }
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
super.tableView(tableView, didSelectRowAt: indexPath)
let cell = tableView.cellForRow(at: indexPath)!
if cell.textLabel!.text! == "Password Generator Flavor" {
tableView.deselectRow(at: indexPath, animated: true)
showPasswordGeneratorFlavorActionSheet(sourceCell: cell)
}
}
func showPasswordGeneratorFlavorActionSheet(sourceCell: UITableViewCell) {
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
var randomFlavorActionTitle = ""
var appleFlavorActionTitle = ""
if Defaults[.passwordGeneratorFlavor] == "Random" {
randomFlavorActionTitle = "✓ Random String"
appleFlavorActionTitle = "Apple's Keychain Style"
} else {
randomFlavorActionTitle = "Random String"
appleFlavorActionTitle = "✓ Apple's Keychain Style"
}
let randomFlavorAction = UIAlertAction(title: randomFlavorActionTitle, style: .default) { _ in
Defaults[.passwordGeneratorFlavor] = "Random"
sourceCell.detailTextLabel?.text = "Random"
}
let appleFlavorAction = UIAlertAction(title: appleFlavorActionTitle, style: .default) { _ in
Defaults[.passwordGeneratorFlavor] = "Apple"
sourceCell.detailTextLabel?.text = "Apple"
}
let cancelAction = UIAlertAction(title: "Cancel", 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)
}
func tapHideUnknownSwitchDetailButton(_ sender: Any?) { func tapHideUnknownSwitchDetailButton(_ sender: Any?) {
let alertMessage = "Only \"key: value\" format in additional fields is supported. Unsupported fields will be given \"unkown\" keys. Turn on this switch to hide unsupported fields." let alertMessage = "Only \"key: value\" format in additional fields is supported. Unsupported fields will be given \"unkown\" keys. Turn on this switch to hide unsupported fields."
let alertTitle = "Hide Unknown Fields" let alertTitle = "Hide Unknown Fields"

View file

@ -36,12 +36,7 @@ extension DefaultsKeys {
static let isHideOTPOn = DefaultsKey<Bool>("isHideOTPOn") static let isHideOTPOn = DefaultsKey<Bool>("isHideOTPOn")
static let isRememberPassphraseOn = DefaultsKey<Bool>("isRememberPassphraseOn") static let isRememberPassphraseOn = DefaultsKey<Bool>("isRememberPassphraseOn")
static let isShowFolderOn = DefaultsKey<Bool>("isShowFolderOn") static let isShowFolderOn = DefaultsKey<Bool>("isShowFolderOn")
static let passwordGeneratorFlavor = DefaultsKey<String>("passwordGeneratorFlavor")
static let passwordGenerationMethod = DefaultsKey<String>("passwordGenerationMethod")
func initDefaultKeys() {
if Defaults[.passwordGenerationMethod] == "" {
Defaults[.passwordGenerationMethod] = "Random"
}
}
} }

View file

@ -38,10 +38,10 @@ class Utils {
} }
static func generatePassword(length: Int) -> String{ static func generatePassword(length: Int) -> String{
switch Defaults[.passwordGenerationMethod] { switch Defaults[.passwordGeneratorFlavor] {
case "Random": case "Random":
return randomString(length: length) return randomString(length: length)
case "Keychain": case "Apple":
return Keychain.generatePassword() return Keychain.generatePassword()
default: default:
return randomString(length: length) return randomString(length: length)
@ -138,6 +138,11 @@ class Utils {
} }
return attributedPassword return attributedPassword
} }
static func initDefaultKeys() {
if Defaults[.passwordGeneratorFlavor] == "" {
Defaults[.passwordGeneratorFlavor] = "Random"
}
}
} }
// https://gist.github.com/NikolaiRuhe/eeb135d20c84a7097516 // https://gist.github.com/NikolaiRuhe/eeb135d20c84a7097516

View file

@ -23,7 +23,7 @@ class FillPasswordTableViewCell: ContentTableViewCell {
} }
@IBAction func generatePassword(_ sender: UIButton) { @IBAction func generatePassword(_ sender: UIButton) {
let plainPassword = Utils.randomString(length: 16) let plainPassword = Utils.generatePassword(length: 16)
contentTextField.attributedText = Utils.attributedPassword(plainPassword: plainPassword) contentTextField.attributedText = Utils.attributedPassword(plainPassword: plainPassword)
Utils.copyToPasteboard(textToCopy: plainPassword) Utils.copyToPasteboard(textToCopy: plainPassword)
} }