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

Remove Android Framework code from presenters.

parent fb9167a3
package chat.rocket.android.authentication.login.presentation
import android.widget.EditText
import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.util.launchUI
import chat.rocket.android.util.textContent
import chat.rocket.common.RocketChatException
import chat.rocket.common.RocketChatTwoFactorException
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.sendMessage
import timber.log.Timber
import javax.inject.Inject
......@@ -20,16 +15,13 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
private val navigator: AuthenticationNavigator) {
@Inject lateinit var client: RocketChatClient
fun authenticate(usernameOrEmailEditText: EditText, passwordEditText: EditText) {
val usernameOrEmail = usernameOrEmailEditText.textContent
val password = passwordEditText.textContent
fun authenticate(usernameOrEmail: String, password: String) {
when {
usernameOrEmail.isBlank() -> {
view.shakeView(usernameOrEmailEditText)
view.alertWrongUsernameOrEmail()
}
password.isEmpty() -> {
view.shakeView(passwordEditText)
view.alertWrongPassword()
}
else -> {
launchUI(strategy) {
......@@ -58,6 +50,7 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
}
}
fun signup() =
fun signup() {
navigator.toSignUp(navigator.currentServer!!)
}
}
\ No newline at end of file
......@@ -2,9 +2,8 @@ package chat.rocket.android.authentication.login.presentation
import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView
import chat.rocket.android.core.behaviours.ShakeView
interface LoginView : LoadingView, MessageView, ShakeView {
interface LoginView : LoadingView, MessageView {
/**
* Shows the oauth view if the server settings allow the login via social accounts.
......@@ -63,4 +62,14 @@ interface LoginView : LoadingView, MessageView, ShakeView {
* @param value True to show the sign up view, false otherwise.
*/
fun showSignUpView(value: Boolean)
/**
* Alerts the user about a wrong inputted username or email.
*/
fun alertWrongUsernameOrEmail()
/**
* Alerts the user about a wrong inputted password.
*/
fun alertWrongPassword()
}
\ No newline at end of file
package chat.rocket.android.authentication.login.ui
import DrawableHelper
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.support.v4.app.Fragment
......@@ -22,6 +23,8 @@ import javax.inject.Inject
class LoginFragment : Fragment(), LoginView {
@Inject lateinit var presenter: LoginPresenter
@Inject lateinit var appContext: Context
private val layoutListener = ViewTreeObserver.OnGlobalLayoutListener {
if (KeyboardHelper.isSoftKeyboardShown(scroll_view.rootView)) {
showOauthView(false)
......@@ -85,7 +88,7 @@ class LoginFragment : Fragment(), LoginView {
showSignUpView(true)
// -------------------------------------------------------------------------------------------------------------------
button_log_in.setOnClickListener { presenter.authenticate(text_username_or_email, text_password) }
button_log_in.setOnClickListener { presenter.authenticate(text_username_or_email.textContent, text_password.textContent) }
}
override fun onDestroyView() {
......@@ -153,6 +156,18 @@ class LoginFragment : Fragment(), LoginView {
text_new_to_rocket_chat.setVisibility(value)
}
override fun alertWrongUsernameOrEmail() {
AnimationHelper.vibrateSmartPhone(appContext)
AnimationHelper.shakeView(text_username_or_email)
text_username_or_email.requestFocus()
}
override fun alertWrongPassword() {
AnimationHelper.vibrateSmartPhone(appContext)
AnimationHelper.shakeView(text_password)
text_password.requestFocus()
}
override fun showLoading() {
enableUserInput(false)
view_loading.show()
......@@ -167,12 +182,6 @@ class LoginFragment : Fragment(), LoginView {
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
}
override fun shakeView(viewToShake: View) {
AnimationHelper.vibrateSmartPhone(viewToShake.context)
AnimationHelper.shakeView(viewToShake)
viewToShake.requestFocus()
}
private fun tintEditTextDrawableStart() {
activity?.applicationContext?.apply {
val personDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_assignment_ind_black_24dp, this)
......
package chat.rocket.android.authentication.signup.presentation
import android.widget.EditText
import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.util.launchUI
import chat.rocket.android.util.textContent
import chat.rocket.common.RocketChatException
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login
......@@ -17,35 +15,29 @@ class SignupPresenter @Inject constructor(private val view: SignupView,
private val navigator: AuthenticationNavigator) {
@Inject lateinit var client: RocketChatClient
fun signup(nameEditText: EditText, usernameEditText: EditText, passwordEditText: EditText, emailEditText: EditText) {
val name = nameEditText.textContent
val username = usernameEditText.textContent
val password = passwordEditText.textContent
val email = emailEditText.textContent
fun signup(name: String, username: String, password: String, email: String) {
when {
name.isBlank() -> {
view.shakeView(nameEditText)
view.alertBlankName()
}
username.isBlank() -> {
view.shakeView(usernameEditText)
view.alertBlankUsername()
}
password.isEmpty() -> {
view.shakeView(passwordEditText)
view.alertEmptyPassword()
}
email.isBlank() -> {
view.shakeView(emailEditText)
view.alertBlankEmail()
}
else -> {
launchUI(strategy) {
view.showLoading()
try {
val user = client.signup(email, name, username, password)
Timber.d("Created user: $user")
val token = client.login(username, password)
Timber.d("Logged in: $token")
Timber.d("Logged in. Token: $token")
navigator.toChatList()
} catch (ex: RocketChatException) {
......
......@@ -2,6 +2,26 @@ package chat.rocket.android.authentication.signup.presentation
import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView
import chat.rocket.android.core.behaviours.ShakeView
interface SignupView : LoadingView, MessageView, ShakeView
\ No newline at end of file
interface SignupView : LoadingView, MessageView {
/**
* Alerts the user about a blank name.
*/
fun alertBlankName()
/**
* Alerts the user about a blank username.
*/
fun alertBlankUsername()
/**
* Alerts the user about a empty password.
*/
fun alertEmptyPassword()
/**
* Alerts the user about a blank email.
*/
fun alertBlankEmail()
}
\ No newline at end of file
......@@ -15,6 +15,7 @@ import chat.rocket.android.helper.AnimationHelper
import chat.rocket.android.helper.KeyboardHelper
import chat.rocket.android.helper.TextHelper
import chat.rocket.android.util.setVisibility
import chat.rocket.android.util.textContent
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_authentication_sign_up.*
import javax.inject.Inject
......@@ -68,7 +69,7 @@ class SignupFragment : Fragment(), SignupView {
setUpNewUserAgreementListener()
button_sign_up.setOnClickListener {
presenter.signup(text_name, text_username, text_password, text_email)
presenter.signup(text_name.textContent, text_username.textContent, text_password.textContent, text_email.textContent)
}
}
......@@ -77,6 +78,30 @@ class SignupFragment : Fragment(), SignupView {
constraint_layout.viewTreeObserver.removeOnGlobalLayoutListener(layoutListener)
}
override fun alertBlankName() {
AnimationHelper.vibrateSmartPhone(appContext)
AnimationHelper.shakeView(text_name)
text_name.requestFocus()
}
override fun alertBlankUsername() {
AnimationHelper.vibrateSmartPhone(appContext)
AnimationHelper.shakeView(text_username)
text_username.requestFocus()
}
override fun alertEmptyPassword() {
AnimationHelper.vibrateSmartPhone(appContext)
AnimationHelper.shakeView(text_password)
text_password.requestFocus()
}
override fun alertBlankEmail() {
AnimationHelper.vibrateSmartPhone(appContext)
AnimationHelper.shakeView(text_email)
text_email.requestFocus()
}
override fun showLoading() {
enableUserInput(false)
view_loading.show()
......@@ -91,12 +116,6 @@ class SignupFragment : Fragment(), SignupView {
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
}
override fun shakeView(viewToShake: View) {
AnimationHelper.vibrateSmartPhone(appContext)
AnimationHelper.shakeView(viewToShake)
viewToShake.requestFocus()
}
private fun tintEditTextDrawableStart() {
val personDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_person_black_24dp, appContext)
val atDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_at_black_24dp, appContext)
......
package chat.rocket.android.authentication.twofactor.presentation
import android.widget.EditText
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.util.launchUI
import chat.rocket.android.util.textContent
import chat.rocket.common.RocketChatException
import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import timber.log.Timber
import javax.inject.Inject
class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
......@@ -20,16 +15,16 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
@Inject lateinit var client: RocketChatClient
// 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) {
val twoFACode = pin.textContent
if (twoFACode.isBlank()) {
view.shakeView(pin)
fun authenticate(usernameOrEmail: String, password: String, twoFactorAuthenticationCode: String) {
if (twoFactorAuthenticationCode.isBlank()) {
view.alertBlankTwoFactorAuthenticationCode()
} else {
launchUI(strategy) {
view.showLoading()
try {
val token = client.login(usernameOrEmail, password, twoFACode)
// Todo Salve token.
val token = client.login(usernameOrEmail, password, twoFactorAuthenticationCode)
Timber.d("Created token: $token")
// Todo Salve token?.
navigator.toChatList()
} catch (ex: RocketChatException) {
val errorMessage = ex.message
......
......@@ -2,6 +2,11 @@ package chat.rocket.android.authentication.twofactor.presentation
import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView
import chat.rocket.android.core.behaviours.ShakeView
interface TwoFAView : LoadingView, MessageView, ShakeView
\ No newline at end of file
interface TwoFAView : LoadingView, MessageView {
/**
* Alerts the user about a blank two factor authentication code.
*/
fun alertBlankTwoFactorAuthenticationCode()
}
\ No newline at end of file
package chat.rocket.android.authentication.twofactor.ui
import DrawableHelper
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.support.v4.app.Fragment
......@@ -14,12 +15,14 @@ import chat.rocket.android.authentication.twofactor.presentation.TwoFAPresenter
import chat.rocket.android.authentication.twofactor.presentation.TwoFAView
import chat.rocket.android.helper.AnimationHelper
import chat.rocket.android.util.setVisibility
import chat.rocket.android.util.textContent
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_authentication_two_fa.*
import javax.inject.Inject
class TwoFAFragment : Fragment(), TwoFAView {
@Inject lateinit var presenter: TwoFAPresenter
@Inject lateinit var appContext: Context
lateinit var serverUrl: String
lateinit var username: String
lateinit var password: String
......@@ -60,10 +63,15 @@ class TwoFAFragment : Fragment(), TwoFAView {
}
button_log_in.setOnClickListener {
presenter.authenticate(username, password, text_two_factor_auth)
presenter.authenticate(username, password, text_two_factor_auth.textContent)
}
}
override fun alertBlankTwoFactorAuthenticationCode() {
AnimationHelper.vibrateSmartPhone(appContext)
AnimationHelper.shakeView(text_two_factor_auth)
}
override fun showLoading() {
view_loading.setVisibility(true)
}
......@@ -76,11 +84,6 @@ class TwoFAFragment : Fragment(), TwoFAView {
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
}
override fun shakeView(viewToShake: View) {
AnimationHelper.vibrateSmartPhone(viewToShake.context)
AnimationHelper.shakeView(viewToShake)
}
private fun tintEditTextDrawableStart() {
activity?.applicationContext?.apply {
val lockDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_vpn_key_black_24dp, this)
......
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