-
Notifications
You must be signed in to change notification settings - Fork 5
/
LoginView.kt
258 lines (222 loc) · 8.2 KB
/
LoginView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.vsouhrada.kotlin.android.anko.fibo.function.signin.login.view
import android.support.v4.app.FragmentActivity
import android.support.v4.content.ContextCompat
import android.text.InputType.TYPE_CLASS_TEXT
import android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD
import android.view.Gravity
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.CheckBox
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.hkm.ui.processbutton.iml.ActionProcessButton
import com.rengwuxian.materialedittext.MaterialEditText
import com.rengwuxian.materialedittext.MaterialEditText.FLOATING_LABEL_NORMAL
import com.vsouhrada.kotlin.android.anko.fibo.R
import com.vsouhrada.kotlin.android.anko.fibo.core.anko.materialEditText
import com.vsouhrada.kotlin.android.anko.fibo.core.anko.string
import com.vsouhrada.kotlin.android.anko.fibo.function.signin.login.model.AuthCredentials
import com.vsouhrada.kotlin.android.anko.fibo.utils.hideKeyboard
import org.jetbrains.anko.*
import org.jetbrains.anko.design.textInputLayout
/**
* @author vsouhrada
* @see[AnkoComponent]
* @see[ILoginView]
* @since 0.1.0
*/
class LoginView : AnkoComponent<FragmentActivity>, ILoginView {
lateinit var presenter: ILoginViewPresenter
lateinit var viewState: LoginViewState
lateinit var ankoView: AnkoContext<FragmentActivity>
override var isRestoringViewState = false
override fun createView(ui: AnkoContext<FragmentActivity>) = with(ui) {
ankoView = ui
linearLayout {
this.gravity = Gravity.CENTER
lparams(width = matchParent, height = matchParent) {
orientation = LinearLayout.VERTICAL
this.gravity = Gravity.CENTER
}
scrollView {
lparams(width = matchParent, height = wrapContent)
linearLayout {
this.gravity = Gravity.CENTER
lparams(width = matchParent, height = matchParent) {
orientation = LinearLayout.VERTICAL
}
// relativeLayout {
// lparams(width = matchParent, height = wrapContent, weight = 0.5f) {
// orientation = LinearLayout.VERTICAL
// }
//
// textView {
// this.gravity = Gravity.CENTER
// text = "Fibo"
// textSize = 30f
// lparams(width = matchParent, height = wrapContent) {
// centerInParent()
// }
// }
// }
linearLayout {
id = R.id.loginForm
lparams(width = dip(300), height = wrapContent) {
background = ContextCompat.getDrawable(ctx, R.color.white)
orientation = LinearLayout.VERTICAL
this.gravity = Gravity.CENTER
padding = dip(16)
clipToPadding = false
bottomMargin = dip(16)
}
textInputLayout {
materialEditText {
id = R.id.userNameEditText
lparams(width = matchParent, height = wrapContent) {
this.topMargin = dip(12)
}
hintResource = R.string.create_user_hint_username
textSize = 24f
setIconLeft(ContextCompat.getDrawable(ctx, R.mipmap.ic_username))
setFloatingLabel(FLOATING_LABEL_NORMAL)
setPrimaryColor(R.color.colorAccent)
}
}
textInputLayout {
materialEditText {
id = R.id.passwordEditText
lparams(width = matchParent, height = wrapContent)
inputType = TYPE_CLASS_TEXT or TYPE_TEXT_VARIATION_PASSWORD
hintResource = R.string.create_user_hint_password
textSize = 24f
setIconLeft(ContextCompat.getDrawable(ctx, R.mipmap.ic_password))
}
}
// https://github.com/Kotlin/anko/issues/16
include<ActionProcessButton>(R.layout.process_button_login) {
onClick { handleOnLoginAction(ui) }
}
checkBox {
id = R.id.loginStayLoggedBox
text = string(R.string.login_label_rememberMe)
}
// actionProcessButton(theme = R.style.ActionProcessButton) {
// id = R.id.loginButton
// lparams(width = matchParent, height = dip(48)) {
// bottomMargin = dip(16)
// topMargin = dip(32)
// leftMargin = dip(16)
// rightMargin = dip(16)
// //textColor = ContextCompat.getColor(ctx, R.color.white)
// }
// textSize = 18f
// setAllCaps(true)
// text = string(R.string.button_login_text)
// onClick { toast("aaaaa") }
// }
textView {
id = R.id.errorView
lparams {
gravity = Gravity.CENTER
topMargin = dip(8)
bottomMargin = dip(16)
}
textColor = ContextCompat.getColor(ctx, R.color.red_error)
text = string(R.string.error_view_login_text)
textSize = 14f
visibility = View.GONE
}
textView {
lparams(width = matchParent, height = wrapContent) {
gravity = Gravity.CENTER
leftMargin = dip(16)
rightMargin = dip(16)
}
textColor = ContextCompat.getColor(ctx, R.color.secondary_text)
text = "Hint:\nusername = frosty password = snow"
textSize = 10f
}
}
}
}
}
}
private fun handleOnLoginAction(ui: AnkoContext<FragmentActivity>) {
with(ui.owner) {
val loginForm = find<LinearLayout>(R.id.loginForm)
val userNameTextView = find<MaterialEditText>(R.id.userNameEditText)
val passwordTextView = find<MaterialEditText>(R.id.passwordEditText)
val rememberMeBox = find<CheckBox>(R.id.loginStayLoggedBox)
val username = userNameTextView.text.toString()
val password = passwordTextView.text.toString()
loginForm.clearAnimation()
if (username.isNullOrBlank()) {
with(userNameTextView) {
clearAnimation()
startAnimation(AnimationUtils.loadAnimation(ctx, R.anim.shake))
}
return
}
if (password.isNullOrBlank()) {
with(passwordTextView) {
clearAnimation()
startAnimation(AnimationUtils.loadAnimation(ctx, R.anim.shake))
}
return
}
// Hide keyboard
if (!hideKeyboard(userNameTextView)) {
hideKeyboard(passwordTextView)
}
presenter.doLogin(AuthCredentials(userName = username, password = password, rememberMe = rememberMeBox.isChecked))
}
}
override fun showLoginForm() {
viewState.setShowLoginForm()
with(ankoView.view) {
find<TextView>(R.id.errorView).visibility = View.GONE
setFormEnabled(enabled = true)
find<ActionProcessButton>(R.id.loginButton).progress = 0
}
}
override fun showError() {
viewState.setShowError()
setFormEnabled(true)
with(ankoView.owner) {
find<ActionProcessButton>(R.id.loginButton).progress = 0
if (!isRestoringViewState) {
// Enable animations only if not restoring view state
with(find<LinearLayout>(R.id.loginForm)) {
clearAnimation()
startAnimation(AnimationUtils.loadAnimation(ctx, R.anim.shake))
}
}
find<TextView>(R.id.errorView).visibility = View.VISIBLE
}
}
override fun showLoading() {
viewState.setShowLoading()
with(ankoView.view) {
find<TextView>(R.id.errorView).visibility = View.GONE
setFormEnabled(enabled = true)
find<ActionProcessButton>(R.id.loginButton).progress = 30
}
}
override fun loginSuccessful() {
with(ankoView.view) {
find<ActionProcessButton>(R.id.loginButton).progress = 100 // done
// TODO
// ankoView.owner.finish()
// ankoView.owner.overridePendingTransition(0, R.anim.zoom_out)
}
ankoView.toast("Login OK")
}
private fun setFormEnabled(enabled: Boolean) {
with(ankoView.view) {
find<EditText>(R.id.userNameEditText).enabled = enabled
find<EditText>(R.id.passwordEditText).enabled = enabled
find<ActionProcessButton>(R.id.loginButton).enabled = enabled
}
}
}