Guest User

Untitled

a guest
Oct 16th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. ackage com.example.android.anko.sample.sign_in
  2.  
  3. import android.os.Build
  4. import android.support.v4.content.ContextCompat
  5. import android.view.Gravity
  6. import android.view.ViewGroup
  7. import android.widget.EditText
  8. import android.widget.LinearLayout
  9. import com.example.android.anko.sample.R
  10. import org.jetbrains.anko.*
  11.  
  12. /**
  13. * @author vsouhrada
  14. * @since 0.1.0
  15. * @see[AnkoComponent]
  16. * @see[SignInActivity]
  17. */
  18. class SingInView : AnkoComponent<SignInActivity> {
  19.  
  20. private lateinit var ankoContext: AnkoContext<SignInActivity>
  21.  
  22. override fun createView(ui: AnkoContext<SignInActivity>) = with(ui) {
  23. ankoContext = ui
  24.  
  25. verticalLayout {
  26. this.gravity = Gravity.CENTER
  27.  
  28. scrollView {
  29.  
  30. verticalLayout {
  31.  
  32. verticalLayout {
  33. id = R.id.formLogin
  34. gravity = Gravity.CENTER
  35. padding = dip(20)
  36.  
  37. lparams(width = dip(300), height = matchParent) {
  38. this.gravity = Gravity.CENTER
  39. // API >= 16
  40. doFromSdk(version = Build.VERSION_CODES.JELLY_BEAN) {
  41. background = ContextCompat.getDrawable(ctx, android.R.color.white)
  42. }
  43. clipToPadding = false
  44. bottomMargin = dip(16)
  45. }
  46.  
  47. val username = editText {
  48. id = R.id.usernameEditText
  49. hintResource = R.string.sign_in_username
  50.  
  51. }.lparams(width = matchParent, height = wrapContent)
  52.  
  53. val password = editText {
  54. id = R.id.passwordEditText
  55. hintResource = R.string.signIn_password
  56.  
  57. }.lparams(width = matchParent, height = wrapContent)
  58.  
  59. button {
  60. id = R.id.signIn_button
  61. textResource = R.string.signIn_button
  62.  
  63. onClick {
  64. handleOnSignInButtonPressed(username = username.text.toString(), password = password.text.toString())
  65. }
  66.  
  67. }.lparams(width = matchParent, height = wrapContent)
  68.  
  69. }.applyRecursively { view ->
  70. when (view) {
  71. is EditText -> view.textSize = 24f
  72. }
  73. }
  74.  
  75. }.lparams(width = matchParent, height = matchParent)
  76.  
  77. }.lparams(width = matchParent, height = wrapContent)
  78.  
  79. }
  80. }
  81.  
  82. private fun handleOnSignInButtonPressed(username: String, password: String) {
  83. with(ankoContext) {
  84. if (username.isBlank() or password.isBlank()) {
  85. alert(title = R.string.sigIn_alert_invalid_user_title,
  86. message = R.string.sigIn_alert_invalid_user_message) {
  87.  
  88. positiveButton(R.string.dialog_button_close) {}
  89. }.show()
  90. } else {
  91. owner.authorizeUser(username, password)
  92. }
  93. }
  94. }
  95.  
  96. fun showAccessDeniedAlertDialog() {
  97. with(ankoContext) {
  98. alert(title = R.string.sigIn_alert_access_denied_title,
  99. message = R.string.sigIn_alert_access_denied_msg) {
  100.  
  101. positiveButton(R.string.dialog_button_close) {}
  102. }.show()
  103. }
  104. }
  105.  
  106. }
Add Comment
Please, Sign In to add comment