Skip to content

Instantly share code, notes, and snippets.

@thenixan
Created January 12, 2018 15:50
Show Gist options
  • Save thenixan/f589a28204c4279598bb97a9f5149021 to your computer and use it in GitHub Desktop.
Save thenixan/f589a28204c4279598bb97a9f5149021 to your computer and use it in GitHub Desktop.
class AuthActivity : AppCompatActivity() {
private val authCardView by lazy { findViewById<CardView>(R.id.authCardView) }
private val okButton by lazy { findViewById<Button>(R.id.okButton) }
private val cancelButton by lazy { findViewById<Button>(R.id.cancelButton) }
private val loginEditText by lazy { findViewById<EditText>(R.id.loginEditText) }
private val passwordEditText by lazy { findViewById<EditText>(R.id.passwordEditText) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_auth)
authCardView.animate()
.setDuration(500L)
.setInterpolator(AccelerateDecelerateInterpolator())
.alpha(1F)
.setListener(object : Animator.AnimatorListener {
override fun onAnimationRepeat(p0: Animator?) {
}
override fun onAnimationCancel(p0: Animator?) {
}
override fun onAnimationStart(p0: Animator?) {
authCardView.alpha = 0F
authCardView.visibility = View.VISIBLE
}
override fun onAnimationEnd(p0: Animator?) {
authCardView.visibility = View.VISIBLE
}
})
okButton.setOnClickListener {
performInputChecksAndSaveUser { login, password ->
saveUser(User(login, password))
setResult(Activity.RESULT_OK)
finish()
}
}
cancelButton.setOnClickListener {
finish()
}
}
private fun performInputChecksAndSaveUser(successCallback: (String, String) -> Unit) {
if (loginEditText.text.isBlank()) {
loginEditText.error = getText(R.string.errorEmptyLogin)
}
if (passwordEditText.text.isBlank()) {
passwordEditText.error = getText(R.string.errorEmptyPassword)
}
if (loginEditText.text.isNotBlank() && passwordEditText.text.isNotBlank()) {
successCallback.invoke(loginEditText.text.toString(), passwordEditText.text.toString())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment