Skip to content

Commit

Permalink
Merge pull request agilebits#395 from agilebits/chad/modernize-swift-…
Browse files Browse the repository at this point in the history
…style

modernize swift style
  • Loading branch information
Kyle Swank authored Dec 7, 2017
2 parents 459c5d9 + e14b1d6 commit fbdc9b3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,33 @@ class ChangePasswordViewController: UIViewController {
self.view.backgroundColor = UIColor(patternImage: patternImage)
}

self.onepasswordButton.isHidden = (false == OnePasswordExtension.shared().isAppExtensionAvailable())
onepasswordButton.isHidden = (false == OnePasswordExtension.shared().isAppExtensionAvailable())
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
return .lightContent
}

@IBAction func changePasswordIn1Password(_ sender:AnyObject) -> Void {
let changedPassword = self.freshPasswordTextField.text!
let oldPassword = self.oldPasswordTextField.text!
let confirmationPassword = self.confirmPasswordTextField.text!
@IBAction func changePasswordIn1Password(_ sender:AnyObject) {
guard let changedPassword = freshPasswordTextField.text,
let oldPassword = oldPasswordTextField.text,
let confirmationPassword = confirmPasswordTextField.text else {
return
}

// Validate that the new password and the old password are not the same.
if (oldPassword.count > 0 && oldPassword == changedPassword) {
self.showChangePasswordFailedAlertWithMessage(message: "The old and the new password must not be the same")
showChangePasswordFailedAlertWithMessage(message: "The old and the new password must not be the same")
return
}

// Validate that the new and confirmation passwords match.
if (changedPassword.count > 0 && changedPassword != confirmationPassword) {
self.showChangePasswordFailedAlertWithMessage(message: "The new passwords and the confirmation password must match")
showChangePasswordFailedAlertWithMessage(message: "The new passwords and the confirmation password must match")
return
}

let newLoginDetails:[String: Any] = [
let newLoginDetails:[String : Any] = [
AppExtensionTitleKey: "ACME", // Optional, used for the third schenario only
AppExtensionUsernameKey: "aUsername", // Optional, used for the third schenario only
AppExtensionPasswordKey: changedPassword,
Expand All @@ -54,44 +56,44 @@ class ChangePasswordViewController: UIViewController {
]

// The password generation options are optional, but are very handy in case you have strict rules about password lengths, symbols and digits.
let passwordGenerationOptions:[String: AnyObject] = [
let passwordGenerationOptions:[String : AnyObject] = [
// The minimum password length can be 4 or more.
AppExtensionGeneratedPasswordMinLengthKey: (8 as AnyObject),
AppExtensionGeneratedPasswordMinLengthKey: (8 as NSNumber),

// The maximum password length can be 50 or less.
AppExtensionGeneratedPasswordMaxLengthKey: (30 as AnyObject),
AppExtensionGeneratedPasswordMaxLengthKey: (30 as NSNumber),

// If YES, the 1Password will guarantee that the generated password will contain at least one digit (number between 0 and 9). Passing NO will not exclude digits from the generated password.
AppExtensionGeneratedPasswordRequireDigitsKey: (true as AnyObject),
AppExtensionGeneratedPasswordRequireDigitsKey: (true as NSNumber),

// If YES, the 1Password will guarantee that the generated password will contain at least one symbol (See the list below). Passing NO will not exclude symbols from the generated password.
AppExtensionGeneratedPasswordRequireSymbolsKey: (true as AnyObject),
AppExtensionGeneratedPasswordRequireSymbolsKey: (true as NSNumber),

// Here are all the symbols available in the the 1Password Password Generator:
// !@#$%^&*()_-+=|[]{}'\";.,>?/~`
// The string for AppExtensionGeneratedPasswordForbiddenCharactersKey should contain the symbols and characters that you wish 1Password to exclude from the generated password.
AppExtensionGeneratedPasswordForbiddenCharactersKey: "!@#$%/0lIO" as AnyObject
AppExtensionGeneratedPasswordForbiddenCharactersKey: "!@#$%/0lIO" as NSString
]

OnePasswordExtension.shared().changePasswordForLogin(forURLString: "https://www.acme.com", loginDetails: newLoginDetails, passwordGenerationOptions: passwordGenerationOptions, for: self, sender: sender) { (loginDictionary, error) -> Void in
if loginDictionary == nil {
if error!._code != Int(AppExtensionErrorCodeCancelledByUser) {
OnePasswordExtension.shared().changePasswordForLogin(forURLString: "https://www.acme.com", loginDetails: newLoginDetails, passwordGenerationOptions: passwordGenerationOptions, for: self, sender: sender) { (loginDictionary, error) in
guard let loginDictionary = loginDictionary else {
if let error = error as NSError?, error.code != AppExtensionErrorCodeCancelledByUser {
print("Error invoking 1Password App Extension for find login: \(String(describing: error))")
}
return
}

self.oldPasswordTextField.text = loginDictionary?[AppExtensionOldPasswordKey] as? String
self.freshPasswordTextField.text = loginDictionary?[AppExtensionPasswordKey] as? String
self.confirmPasswordTextField.text = loginDictionary?[AppExtensionPasswordKey] as? String
self.oldPasswordTextField.text = loginDictionary[AppExtensionOldPasswordKey] as? String
self.freshPasswordTextField.text = loginDictionary[AppExtensionPasswordKey] as? String
self.confirmPasswordTextField.text = loginDictionary[AppExtensionPasswordKey] as? String
}
}

// Convenience function
func showChangePasswordFailedAlertWithMessage(message:String) -> Void {
let alertController = UIAlertController(title: "Change Password Error", message: message, preferredStyle: UIAlertControllerStyle.alert)
func showChangePasswordFailedAlertWithMessage(message:String) {
let alertController = UIAlertController(title: "Change Password Error", message: message, preferredStyle: .alert)

let dismissAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (action) -> Void in
let dismissAction = UIAlertAction(title: "Cancel", style: .cancel) { action in
self.freshPasswordTextField.text = ""
self.confirmPasswordTextField.text = ""
self.freshPasswordTextField.becomeFirstResponder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LoginViewController: UIViewController {
self.view.backgroundColor = UIColor(patternImage: patternImage)
}

self.onepasswordButton.isHidden = (false == OnePasswordExtension.shared().isAppExtensionAvailable())
onepasswordButton.isHidden = (false == OnePasswordExtension.shared().isAppExtensionAvailable())
}

override func viewDidAppear(_ animated: Bool) {
Expand All @@ -42,30 +42,29 @@ class LoginViewController: UIViewController {
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
return .lightContent
}

@IBAction func findLoginFrom1Password(_ sender:AnyObject) -> Void {
OnePasswordExtension.shared().findLogin(forURLString: "https://www.acme.com", for: self, sender: sender, completion: { (loginDictionary, error) -> Void in
if loginDictionary == nil {
if error!._code != Int(AppExtensionErrorCodeCancelledByUser) {
@IBAction func findLoginFrom1Password(_ sender:AnyObject) {
OnePasswordExtension.shared().findLogin(forURLString: "https://www.acme.com", for: self, sender: sender, completion: { (loginDictionary, error) in
guard let loginDictionary = loginDictionary else {
if let error = error as NSError?, error.code != AppExtensionErrorCodeCancelledByUser {
print("Error invoking 1Password App Extension for find login: \(String(describing: error))")
}
return
}

self.usernameTextField.text = loginDictionary?[AppExtensionUsernameKey] as? String
self.passwordTextField.text = loginDictionary?[AppExtensionPasswordKey] as? String
self.usernameTextField.text = loginDictionary[AppExtensionUsernameKey] as? String
self.passwordTextField.text = loginDictionary[AppExtensionPasswordKey] as? String

if let generatedOneTimePassword = loginDictionary?[AppExtensionTOTPKey] as? String {
if let generatedOneTimePassword = loginDictionary[AppExtensionTOTPKey] as? String {
self.oneTimePasswordTextField.isHidden = false
self.oneTimePasswordTextField.text = generatedOneTimePassword

// Important: It is recommended that you submit the OTP/TOTP to your validation server as soon as you receive it, otherwise it may expire.
let delayTime = DispatchTime.now() + Double(Int64(0.5 * Double(NSEC_PER_SEC)))
DispatchQueue.main.asyncAfter(deadline: delayTime, execute: { () -> Void in
self.performSegue(withIdentifier: "showThankYouViewController", sender: self)
})
let delayTime: DispatchTime = .now() + DispatchTimeInterval.milliseconds(500)
DispatchQueue.main.asyncAfter(deadline: delayTime) { self.performSegue(withIdentifier: "showThankYouViewController", sender: self)
}
}

})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,58 @@ class RegisterViewController: UIViewController {
self.view.backgroundColor = UIColor(patternImage: patternImage)
}

self.onepasswordButton.isHidden = (false == OnePasswordExtension.shared().isAppExtensionAvailable())
onepasswordButton.isHidden = (false == OnePasswordExtension.shared().isAppExtensionAvailable())
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.default
}

@IBAction func saveLoginTo1Password(_ sender:AnyObject) -> Void {
@IBAction func saveLoginTo1Password(_ sender:AnyObject) {
let newLoginDetails:[String: Any] = [
AppExtensionTitleKey: "ACME",
AppExtensionUsernameKey: self.usernameTextField.text!,
AppExtensionPasswordKey: self.passwordTextField.text!,
AppExtensionUsernameKey: usernameTextField.text ?? "",
AppExtensionPasswordKey: passwordTextField.text ?? "",
AppExtensionNotesKey: "Saved with the ACME app",
AppExtensionSectionTitleKey: "ACME Browser",
AppExtensionFieldsKey: [
"firstname" : self.firstnameTextField.text!,
"lastname" : self.lastnameTextField.text!
"firstname" : firstnameTextField.text ?? "",
"lastname" : lastnameTextField.text ?? ""
// Add as many string fields as you please.
]
]

// The password generation options are optional, but are very handy in case you have strict rules about password lengths, symbols and digits.
let passwordGenerationOptions:[String: AnyObject] = [
// The minimum password length can be 4 or more.
AppExtensionGeneratedPasswordMinLengthKey: (8 as AnyObject),
AppExtensionGeneratedPasswordMinLengthKey: (8 as NSNumber),

// The maximum password length can be 50 or less.
AppExtensionGeneratedPasswordMaxLengthKey: (30 as AnyObject),
AppExtensionGeneratedPasswordMaxLengthKey: (30 as NSNumber),

// If YES, the 1Password will guarantee that the generated password will contain at least one digit (number between 0 and 9). Passing NO will not exclude digits from the generated password.
AppExtensionGeneratedPasswordRequireDigitsKey: (true as AnyObject),
AppExtensionGeneratedPasswordRequireDigitsKey: (true as NSNumber),

// If YES, the 1Password will guarantee that the generated password will contain at least one symbol (See the list below). Passing NO will not exclude symbols from the generated password.
AppExtensionGeneratedPasswordRequireSymbolsKey: (true as AnyObject),
AppExtensionGeneratedPasswordRequireSymbolsKey: (true as NSNumber),

// Here are all the symbols available in the the 1Password Password Generator:
// !@#$%^&*()_-+=|[]{}'\";.,>?/~`
// The string for AppExtensionGeneratedPasswordForbiddenCharactersKey should contain the symbols and characters that you wish 1Password to exclude from the generated password.
AppExtensionGeneratedPasswordForbiddenCharactersKey: "!@#$%/0lIO" as AnyObject
AppExtensionGeneratedPasswordForbiddenCharactersKey: "!@#$%/0lIO" as NSString
]

OnePasswordExtension.shared().storeLogin(forURLString: "https://www.acme.com", loginDetails: newLoginDetails, passwordGenerationOptions: passwordGenerationOptions, for: self, sender: sender) { (loginDictionary, error) -> Void in
if loginDictionary == nil {
if error!._code != Int(AppExtensionErrorCodeCancelledByUser) {
OnePasswordExtension.shared().storeLogin(forURLString: "https://www.acme.com", loginDetails: newLoginDetails, passwordGenerationOptions: passwordGenerationOptions, for: self, sender: sender) { (loginDictionary, error) in
guard let loginDictionary = loginDictionary else {
if let error = error as NSError?, error.code != AppExtensionErrorCodeCancelledByUser {
print("Error invoking 1Password App Extension for find login: \(String(describing: error))")
}
return
}

self.usernameTextField.text = loginDictionary?[AppExtensionUsernameKey] as? String
self.passwordTextField.text = loginDictionary?[AppExtensionPasswordKey] as? String
if let returnedLoginDictionary = loginDictionary![AppExtensionReturnedFieldsKey] as? [String: Any] {
self.usernameTextField.text = loginDictionary[AppExtensionUsernameKey] as? String
self.passwordTextField.text = loginDictionary[AppExtensionPasswordKey] as? String
if let returnedLoginDictionary = loginDictionary[AppExtensionReturnedFieldsKey] as? [String: Any] {
self.firstnameTextField.text = returnedLoginDictionary["firstname"] as? String
self.lastnameTextField.text = returnedLoginDictionary["lastname"] as? String
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class ThankYouViewController: UIViewController {
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
return .lightContent
}
}
Loading

0 comments on commit fbdc9b3

Please sign in to comment.