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