forked from github/wulkanowy-mirror
Update login form to Material Design 2 (#229)
This commit is contained in:

committed by
Mikołaj Pich

parent
11b6c00e4a
commit
1d7585071d
@ -16,6 +16,8 @@ import io.github.wulkanowy.data.db.entities.Student
|
||||
import io.github.wulkanowy.ui.base.BaseFragment
|
||||
import io.github.wulkanowy.ui.modules.login.LoginActivity
|
||||
import io.github.wulkanowy.utils.hideSoftInput
|
||||
import io.github.wulkanowy.utils.setOnItemSelectedListener
|
||||
import io.github.wulkanowy.utils.setOnTextChangedListener
|
||||
import io.github.wulkanowy.utils.showSoftInput
|
||||
import kotlinx.android.synthetic.main.fragment_login_form.*
|
||||
import javax.inject.Inject
|
||||
@ -47,10 +49,15 @@ class LoginFormFragment : BaseFragment(), LoginFormView {
|
||||
)
|
||||
}
|
||||
|
||||
loginFormName.setOnTextChangedListener { presenter.onNameTextChanged() }
|
||||
loginFormPass.setOnTextChangedListener { presenter.onPassTextChanged() }
|
||||
|
||||
loginFormPass.setOnEditorActionListener { _, id, _ ->
|
||||
if (id == IME_ACTION_DONE || id == IME_NULL) loginFormSignIn.callOnClick() else false
|
||||
}
|
||||
|
||||
loginFormHost.setOnItemSelectedListener { presenter.onHostSelected() }
|
||||
|
||||
context?.let {
|
||||
loginFormHost.adapter = ArrayAdapter.createFromResource(it, R.array.endpoints_keys, android.R.layout.simple_spinner_item)
|
||||
.apply { setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) }
|
||||
@ -58,33 +65,41 @@ class LoginFormFragment : BaseFragment(), LoginFormView {
|
||||
}
|
||||
|
||||
override fun setErrorNameRequired() {
|
||||
loginFormName.run {
|
||||
loginFormNameLayout.run {
|
||||
requestFocus()
|
||||
error = getString(R.string.login_field_required)
|
||||
}
|
||||
}
|
||||
|
||||
override fun setErrorPassRequired(focus: Boolean) {
|
||||
loginFormPass.run {
|
||||
loginFormPassLayout.run {
|
||||
if (focus) requestFocus()
|
||||
error = getString(R.string.login_field_required)
|
||||
}
|
||||
}
|
||||
|
||||
override fun setErrorPassInvalid(focus: Boolean) {
|
||||
loginFormPass.run {
|
||||
loginFormPassLayout.run {
|
||||
if (focus) requestFocus()
|
||||
error = getString(R.string.login_invalid_password)
|
||||
}
|
||||
}
|
||||
|
||||
override fun setErrorPassIncorrect() {
|
||||
loginFormPass.run {
|
||||
loginFormPassLayout.run {
|
||||
requestFocus()
|
||||
error = getString(R.string.login_incorrect_password)
|
||||
}
|
||||
}
|
||||
|
||||
override fun clearNameError() {
|
||||
loginFormNameLayout.error = null
|
||||
}
|
||||
|
||||
override fun clearPassError() {
|
||||
loginFormPassLayout.error = null
|
||||
}
|
||||
|
||||
override fun showSoftKeyboard() {
|
||||
activity?.showSoftInput()
|
||||
}
|
||||
|
@ -31,6 +31,21 @@ class LoginFormPresenter @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun onHostSelected() {
|
||||
view?.apply {
|
||||
clearPassError()
|
||||
clearNameError()
|
||||
}
|
||||
}
|
||||
|
||||
fun onPassTextChanged() {
|
||||
view?.clearPassError()
|
||||
}
|
||||
|
||||
fun onNameTextChanged() {
|
||||
view?.clearNameError()
|
||||
}
|
||||
|
||||
fun attemptLogin(email: String, password: String, endpoint: String) {
|
||||
if (!validateCredentials(email, password)) return
|
||||
|
||||
|
@ -15,6 +15,10 @@ interface LoginFormView : BaseView {
|
||||
|
||||
fun setErrorPassIncorrect()
|
||||
|
||||
fun clearNameError()
|
||||
|
||||
fun clearPassError()
|
||||
|
||||
fun showSoftKeyboard()
|
||||
|
||||
fun hideSoftKeyboard()
|
||||
|
@ -14,6 +14,7 @@ import io.github.wulkanowy.data.db.entities.Student
|
||||
import io.github.wulkanowy.ui.base.BaseFragment
|
||||
import io.github.wulkanowy.ui.modules.login.LoginActivity
|
||||
import io.github.wulkanowy.utils.hideSoftInput
|
||||
import io.github.wulkanowy.utils.setOnTextChangedListener
|
||||
import io.github.wulkanowy.utils.showSoftInput
|
||||
import kotlinx.android.synthetic.main.fragment_login_symbol.*
|
||||
import javax.inject.Inject
|
||||
@ -29,6 +30,9 @@ class LoginSymbolFragment : BaseFragment(), LoginSymbolView {
|
||||
fun newInstance() = LoginSymbolFragment()
|
||||
}
|
||||
|
||||
override val symbolNameError: CharSequence?
|
||||
get() = loginSymbolNameLayout.error
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_login_symbol, container, false)
|
||||
}
|
||||
@ -41,6 +45,8 @@ class LoginSymbolFragment : BaseFragment(), LoginSymbolView {
|
||||
override fun initView() {
|
||||
loginSymbolSignIn.setOnClickListener { presenter.attemptLogin(loginSymbolName.text.toString()) }
|
||||
|
||||
loginSymbolName.setOnTextChangedListener { presenter.onSymbolTextChanged() }
|
||||
|
||||
loginSymbolName.apply {
|
||||
setOnEditorActionListener { _, id, _ ->
|
||||
if (id == IME_ACTION_DONE || id == IME_NULL) loginSymbolSignIn.callOnClick() else false
|
||||
@ -54,22 +60,26 @@ class LoginSymbolFragment : BaseFragment(), LoginSymbolView {
|
||||
}
|
||||
|
||||
override fun setErrorSymbolIncorrect() {
|
||||
loginSymbolName.apply {
|
||||
loginSymbolNameLayout.apply {
|
||||
requestFocus()
|
||||
error = getString(R.string.login_incorrect_symbol)
|
||||
}
|
||||
}
|
||||
|
||||
override fun setErrorSymbolRequire() {
|
||||
loginSymbolName.apply {
|
||||
loginSymbolNameLayout.apply {
|
||||
requestFocus()
|
||||
error = getString(R.string.login_field_required)
|
||||
}
|
||||
}
|
||||
|
||||
override fun clearSymbolError() {
|
||||
loginSymbolNameLayout.error = null
|
||||
}
|
||||
|
||||
override fun clearAndFocusSymbol() {
|
||||
loginSymbolName.apply {
|
||||
text = null
|
||||
loginSymbolNameLayout.apply {
|
||||
editText?.text = null
|
||||
requestFocus()
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,10 @@ class LoginSymbolPresenter @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun onSymbolTextChanged() {
|
||||
view?.apply { if (symbolNameError != null) clearSymbolError() }
|
||||
}
|
||||
|
||||
fun attemptLogin(symbol: String) {
|
||||
if (symbol.isBlank()) {
|
||||
view?.setErrorSymbolRequire()
|
||||
|
@ -5,12 +5,16 @@ import io.github.wulkanowy.ui.base.BaseView
|
||||
|
||||
interface LoginSymbolView : BaseView {
|
||||
|
||||
val symbolNameError: CharSequence?
|
||||
|
||||
fun initView()
|
||||
|
||||
fun setErrorSymbolIncorrect()
|
||||
|
||||
fun setErrorSymbolRequire()
|
||||
|
||||
fun clearSymbolError()
|
||||
|
||||
fun clearAndFocusSymbol()
|
||||
|
||||
fun showSoftKeyboard()
|
||||
|
@ -0,0 +1,16 @@
|
||||
package io.github.wulkanowy.utils
|
||||
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.widget.EditText
|
||||
|
||||
inline fun EditText.setOnTextChangedListener(crossinline listener: () -> Unit) {
|
||||
addTextChangedListener(object : TextWatcher {
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||
listener()
|
||||
}
|
||||
|
||||
override fun afterTextChanged(s: Editable?) {}
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user