Commit 73d78ad2 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Remove RC client creation and format the code.

parent dff143c9
package chat.rocket.android.authentication.login.presentation package chat.rocket.android.authentication.login.presentation
import android.widget.EditText import android.widget.EditText
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.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.core.RocketChatClient import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.chatRooms
import chat.rocket.core.internal.rest.getRoomFavoriteMessages
import chat.rocket.core.internal.rest.login import chat.rocket.core.internal.rest.login
import okhttp3.HttpUrl import chat.rocket.core.internal.rest.sendMessage
import okhttp3.OkHttpClient import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
class LoginPresenter @Inject constructor(private val view: LoginView, class LoginPresenter @Inject constructor(private val view: LoginView,
private val strategy: CancelStrategy, private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator, private val navigator: AuthenticationNavigator) {
private val okHttpClient: OkHttpClient, @Inject lateinit var client: RocketChatClient
private val logger: PlatformLogger,
private val repository: AuthTokenRepository) {
// TODO: Create a single entry point to RocketChatClient
val client: RocketChatClient = RocketChatClient.create {
httpClient = okHttpClient
restUrl = HttpUrl.parse(navigator.currentServer)!!
websocketUrl = navigator.currentServer!!
tokenRepository = repository
platformLogger = logger
}
fun authenticate(usernameOrEmailEditText: EditText, passwordEditText: EditText) { fun authenticate(usernameOrEmailEditText: EditText, passwordEditText: EditText) {
val usernameOrEmail = usernameOrEmailEditText.textContent val usernameOrEmail = usernameOrEmailEditText.textContent
val password = passwordEditText.textContent val password = passwordEditText.textContent
when { when {
usernameOrEmail.isBlank() -> view.shakeView(usernameOrEmailEditText) usernameOrEmail.isBlank() -> {
password.isEmpty() -> view.shakeView(passwordEditText) view.shakeView(usernameOrEmailEditText)
else -> launchUI(strategy) { }
view.showLoading() password.isEmpty() -> {
try { view.shakeView(passwordEditText)
val token = client.login(usernameOrEmail, password) }
// TODO: Salve token. else -> {
navigator.toChatList() launchUI(strategy) {
} catch (ex: RocketChatException) { view.showLoading()
when (ex) { try {
is RocketChatTwoFactorException -> navigator.toTwoFA(navigator.currentServer!!, usernameOrEmail, password) val token = client.login(usernameOrEmail, password)
else -> { Timber.d("Created token: $token")
val errorMessage = ex.message navigator.toChatList()
if (errorMessage != null) { } catch (rocketChatException: RocketChatException) {
view.showMessage(errorMessage) when (rocketChatException) {
is RocketChatTwoFactorException -> {
navigator.toTwoFA(navigator.currentServer!!, usernameOrEmail, password)
}
else -> {
val errorMessage = rocketChatException.message
if (errorMessage != null) {
view.showMessage(errorMessage)
}
} }
} }
} finally {
view.hideLoading()
} }
} finally {
view.hideLoading()
} }
} }
} }
......
package chat.rocket.android.authentication.signup.presentation package chat.rocket.android.authentication.signup.presentation
import android.widget.EditText import android.widget.EditText
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.android.util.textContent
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login import chat.rocket.core.internal.rest.login
import chat.rocket.core.internal.rest.signup import chat.rocket.core.internal.rest.signup
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
class SignupPresenter @Inject constructor(private val view: SignupView, class SignupPresenter @Inject constructor(private val view: SignupView,
private val strategy: CancelStrategy, private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator, private val navigator: AuthenticationNavigator) {
private val okHttpClient: OkHttpClient, @Inject lateinit var client: RocketChatClient
private val logger: PlatformLogger,
private val repository: AuthTokenRepository) {
// TODO: Create a single entry point to RocketChatClient
val client: RocketChatClient = RocketChatClient.create {
httpClient = okHttpClient
restUrl = HttpUrl.parse(navigator.currentServer)!!
websocketUrl = navigator.currentServer!!
tokenRepository = repository
platformLogger = logger
}
fun signup(nameEditText: EditText, emailEditText: EditText, usernameEditText: EditText, passwordEditText: EditText) { fun signup(nameEditText: EditText, usernameEditText: EditText, passwordEditText: EditText, emailEditText: EditText) {
val name = nameEditText.textContent val name = nameEditText.textContent
val email = emailEditText.textContent
val username = usernameEditText.textContent val username = usernameEditText.textContent
val password = passwordEditText.textContent val password = passwordEditText.textContent
val email = emailEditText.textContent
when { when {
name.isBlank() -> view.shakeView(nameEditText) name.isBlank() -> {
email.isBlank() -> view.shakeView(emailEditText) view.shakeView(nameEditText)
username.isBlank() -> view.shakeView(usernameEditText) }
password.isEmpty() -> view.shakeView(passwordEditText) username.isBlank() -> {
else -> launchUI(strategy) { view.shakeView(usernameEditText)
view.showLoading() }
password.isEmpty() -> {
view.shakeView(passwordEditText)
}
email.isBlank() -> {
view.shakeView(emailEditText)
}
else -> {
launchUI(strategy) {
view.showLoading()
try { try {
val user = client.signup(email, name, username, password) val user = client.signup(email, name, username, password)
Timber.d("Created user: $user") Timber.d("Created user: $user")
val token = client.login(username, password) val token = client.login(username, password)
Timber.d("Logged in: $token") Timber.d("Logged in: $token")
navigator.toChatList() navigator.toChatList()
} catch (ex: RocketChatException) { } catch (ex: RocketChatException) {
val errorMessage = ex.message val errorMessage = ex.message
if (errorMessage != null) { if (errorMessage != null) {
view.showMessage(errorMessage) view.showMessage(errorMessage)
}
} finally {
view.hideLoading()
} }
} finally {
view.hideLoading()
} }
} }
} }
......
...@@ -16,22 +16,12 @@ import javax.inject.Inject ...@@ -16,22 +16,12 @@ import javax.inject.Inject
class TwoFAPresenter @Inject constructor(private val view: TwoFAView, class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
private val strategy: CancelStrategy, private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator, private val navigator: AuthenticationNavigator) {
private val okHttpClient: OkHttpClient, @Inject lateinit var client: RocketChatClient
private val logger: PlatformLogger,
private val repository: AuthTokenRepository) {
// TODO: Create a single entry point to RocketChatClient
val client: RocketChatClient = RocketChatClient.create {
httpClient = okHttpClient
restUrl = HttpUrl.parse(navigator.currentServer)!!
websocketUrl = navigator.currentServer!!
tokenRepository = repository
platformLogger = logger
}
// TODO: If the usernameOrEmail and password was informed by the user on the previous screen, then we should pass only the pin, like this: fun authenticate(pin: EditText) // TODO: If the usernameOrEmail and password was informed by the user on the previous screen, then we should pass only the pin, like this: fun authenticate(pin: EditText)
fun authenticate(usernameOrEmail: String, password: String, pin: EditText) { fun authenticate(usernameOrEmail: String, password: String, pin: EditText) {
val twoFACode = pin.textContent val twoFACode = pin.textContent
if (twoFACode.isBlank()) { if (twoFACode.isBlank()) {
view.shakeView(pin) view.shakeView(pin)
} else { } else {
......
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