Commit a9d6efbc authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Validate inputs

parent 223cacfb
package chat.rocket.android.authentication.login.presentation package chat.rocket.android.authentication.login.presentation
import android.widget.EditText
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.android.util.textContent
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.RocketChatTwoFactorException import chat.rocket.common.RocketChatTwoFactorException
import chat.rocket.common.util.PlatformLogger import chat.rocket.common.util.PlatformLogger
...@@ -19,7 +21,6 @@ class LoginPresenter @Inject constructor(private val view: LoginView, ...@@ -19,7 +21,6 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
private val okHttpClient: OkHttpClient, private val okHttpClient: OkHttpClient,
private val logger: PlatformLogger, private val logger: PlatformLogger,
private val repository: AuthTokenRepository) { private val repository: AuthTokenRepository) {
val client: RocketChatClient = RocketChatClient.create { val client: RocketChatClient = RocketChatClient.create {
httpClient = okHttpClient httpClient = okHttpClient
restUrl = HttpUrl.parse(navigator.currentServer)!! restUrl = HttpUrl.parse(navigator.currentServer)!!
...@@ -28,24 +29,36 @@ class LoginPresenter @Inject constructor(private val view: LoginView, ...@@ -28,24 +29,36 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
platformLogger = logger platformLogger = logger
} }
fun authenticate(username: String, password: String) { fun authenticate(usernameOrEmail: EditText, password: EditText) {
// TODO - validate input val user = usernameOrEmail.textContent
val pass = password.textContent
launchUI(strategy) {
view.showLoading()
try {
val token = client.login(username, password)
navigator.toChatList() if (user.isBlank() && pass.isEmpty()) {
} catch (ex: RocketChatException) { view.shakeView(usernameOrEmail)
when(ex) { view.shakeView(password)
is RocketChatTwoFactorException -> } else if (user.isBlank()) {
navigator.toTwoFA(navigator.currentServer!!, username, password) view.shakeView(usernameOrEmail)
else -> } else if (pass.isEmpty()) {
view.onLoginError(ex.message) view.shakeView(password)
} else {
launchUI(strategy) {
view.showLoading()
try {
val token = client.login(user, pass)
navigator.toChatList()
} catch (ex: RocketChatException) {
when (ex) {
is RocketChatTwoFactorException -> navigator.toTwoFA(navigator.currentServer!!, user, pass)
else -> {
val errorMessage = ex.message
if (errorMessage != null) {
view.showMessage(errorMessage)
}
}
}
} finally {
view.hideLoading()
} }
} finally {
view.hideLoading()
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment