Skip to content

Instantly share code, notes, and snippets.

@adrianhall
Created April 18, 2018 19:47
Show Gist options
  • Save adrianhall/74f5aa7e838b3f318ec68e0ddcbfdd4b to your computer and use it in GitHub Desktop.
Save adrianhall/74f5aa7e838b3f318ec68e0ddcbfdd4b to your computer and use it in GitHub Desktop.
Partial AuthenticatorActivity for a custom UI
class AuthenticatorActivity : Activity(), AuthenticationHandler {
companion object {
private val TAG: String = this::class.java.simpleName
}
/**
* The identityManager for this application
*/
private val identityManager: IdentityManager by inject()
/**
* Called when the activity is starting. This is where most initialization should go: calling
* setContentView(int) to inflate the activity's UI, initializing any view models, etc.
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_authenticator)
// We should be able to close this activity, in which case we go back
// to the prior activity.
authenticator_cancel_button.onClick { [email protected]() }
// Hook up validator for email address and password. In this case, we
// do a minimal validation for the input as it will be checked by the
// Amazon Cognito system as well.
loginform_username.validate({ s -> isUsernameValid(s) }, "Valid email address required")
// Now do the same for password. We require a minimum length of 6 characters
loginform_password.validate({ s -> isPasswordValid(s) }, "Minimum 6 characters required")
// We only enable the login button when both the email address and password are both
// valid. To do this, we wire up an additional text listener on both to call the
// checker
loginform_username.afterTextChanged { checkLoginEnabled() }
loginform_password.afterTextChanged { checkLoginEnabled() }
// Wire up the form buttons
loginform_signin_button.onClick { handleLogin() }
loginform_signup_button.onClick { Log.d(TAG, "Sign-up not implemented") }
loginform_forgotpassword_button.onClick { Log.d(TAG, "Forgot Password not implemented") }
}
/**
* Checks the loginform_username and loginform_password. If both of them are
* valid, then enable the signin button
*/
private fun checkLoginEnabled() {
loginform_signin_button.isEnabled =
isUsernameValid(loginform_username.getContent())
&& isPasswordValid(loginform_password.getContent())
}
/**
* Checks to see if the username is valid
*/
private fun isUsernameValid(s: String): Boolean = s.isValidEmail()
/**
* Checks to see if the password is valid
*/
private fun isPasswordValid(s: String): Boolean = s.length >= 6
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment