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

Override functions.

Also delete the ProgressDialog (we have our custom loading view and we should use it instead).
parent 11b151bf
package chat.rocket.android.authentication.login.ui package chat.rocket.android.authentication.login.ui
import DrawableHelper import DrawableHelper
import android.app.ProgressDialog
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
...@@ -9,14 +8,33 @@ import android.view.* ...@@ -9,14 +8,33 @@ import android.view.*
import android.widget.ScrollView import android.widget.ScrollView
import android.widget.Toast import android.widget.Toast
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.helper.KeyboardHelper
import chat.rocket.android.authentication.login.presentation.LoginPresenter import chat.rocket.android.authentication.login.presentation.LoginPresenter
import chat.rocket.android.authentication.login.presentation.LoginView import chat.rocket.android.authentication.login.presentation.LoginView
import chat.rocket.android.helper.AnimationHelper
import chat.rocket.android.helper.KeyboardHelper
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_log_in.* import kotlinx.android.synthetic.main.fragment_authentication_log_in.*
import javax.inject.Inject import javax.inject.Inject
class LoginFragment : Fragment(), LoginView { class LoginFragment : Fragment(), LoginView {
@Inject lateinit var presenter: LoginPresenter
private val layoutListener = ViewTreeObserver.OnGlobalLayoutListener {
if (KeyboardHelper.isSoftKeyboardShown(scroll_view.rootView)) {
shouldShowOauthView(false)
shouldShowSignUpView(false)
shouldShowLoginButton(true)
} else {
if (isEditTextEmpty()) {
shouldShowOauthView(true)
shouldShowSignUpView(true)
shouldShowLoginButton(false)
}
}
}
private var isGlobalLayoutListenerSetUp = false
private var isSignUpViewOnClickListenerSetUp = false
companion object { companion object {
private const val SERVER_URL = "server_url" private const val SERVER_URL = "server_url"
...@@ -28,12 +46,8 @@ class LoginFragment : Fragment(), LoginView { ...@@ -28,12 +46,8 @@ class LoginFragment : Fragment(), LoginView {
} }
} }
var progress: ProgressDialog? = null
lateinit var serverUrl: String lateinit var serverUrl: String
@Inject
lateinit var presenter: LoginPresenter
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
AndroidSupportInjection.inject(this) AndroidSupportInjection.inject(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -53,135 +67,141 @@ class LoginFragment : Fragment(), LoginView { ...@@ -53,135 +67,141 @@ class LoginFragment : Fragment(), LoginView {
tintEditTextDrawableStart() tintEditTextDrawableStart()
} }
// Just an example: if the server allow the login via social accounts (oauth authentication) then show the respective interface. // TODO: THIS IS A PRESENTER CONCERN - REMOVE THAT !
// -------------------------------------------------------------------------------------------------------------------
shouldShowOauthView(true) shouldShowOauthView(true)
// In this case we need to setup the layout to hide and show the oauth interface when the soft keyboard is shown (means that the user touched the text_username_or_email and text_password EditText).
setupGlobalLayoutListener()
// Show the first three social account's ImageButton (REMARK: we must show at maximum *three* views) // Show the first three social account's ImageButton (REMARK: we must show at maximum *three* views)
showLoginUsingFacebookImageButton() enableLoginByFacebook()
showLoginUsingGithubImageButton() enableLoginByGithub()
showLoginUsingGoogleImageButton() enableLoginByGoogle()
// Setup the FloatingActionButton to show more social account's ImageButton (it expands the social accounts interface to show more views).
setupFabListener() setupFabListener()
// Just an example: if the server allow the new users registration then show the respective interface. // Just an example: if the server allow the new users registration then show the respective interface.
shouldShowSignUpMsgView(true) shouldShowSignUpView(true)
// -------------------------------------------------------------------------------------------------------------------
button_log_in.setOnClickListener {
presenter.authenticate(text_username_or_email.text.toString(), text_password.text.toString())
}
text_new_to_rocket_chat.setOnClickListener { button_log_in.setOnClickListener { presenter.authenticate(text_username_or_email, text_password) }
presenter.signup()
}
} }
override fun onDestroyView() { override fun onDestroyView() {
scroll_view.viewTreeObserver.removeOnGlobalLayoutListener(layoutListener)
super.onDestroyView() super.onDestroyView()
if (isGlobalLayoutListenerSetUp) {
scroll_view.viewTreeObserver.removeOnGlobalLayoutListener(layoutListener)
isGlobalLayoutListenerSetUp = false
}
} }
private fun tintEditTextDrawableStart() { override fun shouldShowOauthView(show: Boolean) {
activity?.applicationContext?.apply { if (show) {
val personDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_assignment_ind_black_24dp, this) social_accounts_container.setVisibility(true)
val lockDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_lock_black_24dp, this) button_fab.setVisibility(true)
val drawables = arrayOf(personDrawable, lockDrawable) // We need to setup the layout to hide and show the oauth interface when the soft keyboard is shown
DrawableHelper.wrapDrawables(drawables) // (means that the user touched the text_username_or_email or text_password EditText to fill that respective fields).
DrawableHelper.tintDrawables(drawables, this, R.color.colorDrawableTintGrey) if (!isGlobalLayoutListenerSetUp) {
DrawableHelper.compoundDrawables(arrayOf(text_username_or_email, text_password), drawables) scroll_view.viewTreeObserver.addOnGlobalLayoutListener(layoutListener)
isGlobalLayoutListenerSetUp = true
}
} else {
social_accounts_container.setVisibility(false)
button_fab.setVisibility(false)
} }
} }
private fun setupGlobalLayoutListener() { override fun setupFabListener() {
scroll_view.viewTreeObserver.addOnGlobalLayoutListener(layoutListener) button_fab.setOnClickListener({
enableLoginByLinkedin()
enableLoginByMeteor()
enableLoginByTwitter()
enableLoginByGitlab()
scrollToBottom()
hideFab()
})
} }
val layoutListener = ViewTreeObserver.OnGlobalLayoutListener { override fun enableLoginByFacebook() {
if (KeyboardHelper.isSoftKeyboardShown(scroll_view.rootView)) { button_facebook.setVisibility(true)
shouldShowOauthView(false)
shouldShowSignUpMsgView(false)
shouldShowLoginButton(true)
} else {
if (isEditTextNullOrBlank()) {
shouldShowOauthView(true)
shouldShowSignUpMsgView(true)
shouldShowLoginButton(false)
}
}
} }
private fun shouldShowOauthView(show: Boolean) { override fun enableLoginByGithub() {
if (show) { button_github.setVisibility(true)
social_accounts_container.visibility = View.VISIBLE
button_fab.visibility = View.VISIBLE
} else {
social_accounts_container.visibility = View.GONE
button_fab.visibility = View.GONE
}
} }
private fun shouldShowSignUpMsgView(show: Boolean) { override fun enableLoginByGoogle() {
if (show) { button_google.setVisibility(true)
text_new_to_rocket_chat.visibility = View.VISIBLE
} else {
text_new_to_rocket_chat.visibility = View.GONE
}
} }
private fun shouldShowLoginButton(show: Boolean) { override fun enableLoginByLinkedin() {
if (show) { button_linkedin.setVisibility(true)
button_log_in.visibility = View.VISIBLE
} else {
button_log_in.visibility = View.GONE
}
} }
private fun showLoginUsingFacebookImageButton() { override fun enableLoginByMeteor() {
button_facebook.visibility = View.VISIBLE button_meteor.setVisibility(true)
} }
private fun showLoginUsingGithubImageButton() { override fun enableLoginByTwitter() {
button_github.visibility = View.VISIBLE button_twitter.setVisibility(true)
} }
private fun showLoginUsingGoogleImageButton() { override fun enableLoginByGitlab() {
button_google.visibility = View.VISIBLE button_gitlab.setVisibility(true)
} }
private fun showLoginUsingLinkedinImageButton() { override fun shouldShowSignUpView(show: Boolean) {
button_linkedin.visibility = View.VISIBLE if (show) {
text_new_to_rocket_chat.setVisibility(true)
if (!isSignUpViewOnClickListenerSetUp) {
text_new_to_rocket_chat.setOnClickListener { presenter.signup() }
isSignUpViewOnClickListenerSetUp = true
}
} else {
text_new_to_rocket_chat.setVisibility(false)
}
} }
private fun showLoginUsingMeteorImageButton() { override fun showLoading() {
button_meteor.visibility = View.VISIBLE view_loading.setVisibility(true)
} }
private fun showLoginUsingTwitterImageButton() { override fun hideLoading() {
button_twitter.visibility = View.VISIBLE view_loading.setVisibility(false)
} }
private fun showLoginUsingGitlabImageButton() { override fun showMessage(message: String) {
button_gitlab.visibility = View.VISIBLE Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
} }
private fun setupFabListener() { override fun shakeView(viewToShake: View) {
button_fab.setOnClickListener({ AnimationHelper.vibrate(viewToShake.context)
showLoginUsingLinkedinImageButton() AnimationHelper.shakeView(viewToShake)
showLoginUsingMeteorImageButton() }
showLoginUsingTwitterImageButton()
showLoginUsingGitlabImageButton()
scrollToBottom() private fun tintEditTextDrawableStart() {
hideFab() activity?.applicationContext?.apply {
}) val personDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_assignment_ind_black_24dp, this)
val lockDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_lock_black_24dp, this)
val drawables = arrayOf(personDrawable, lockDrawable)
DrawableHelper.wrapDrawables(drawables)
DrawableHelper.tintDrawables(drawables, this, R.color.colorDrawableTintGrey)
DrawableHelper.compoundDrawables(arrayOf(text_username_or_email, text_password), drawables)
}
} }
// Returns true if *all* EditText are null or blank. private fun shouldShowLoginButton(show: Boolean) {
private fun isEditTextNullOrBlank(): Boolean { if (show) {
return text_username_or_email.text.isNullOrBlank() && text_password.text.isNullOrBlank() button_log_in.setVisibility(true)
} else {
button_log_in.setVisibility(false)
}
}
// Returns true if *all* EditTexts are empty.
private fun isEditTextEmpty(): Boolean {
return text_username_or_email.textContent.isBlank() && text_password.textContent.isEmpty()
} }
private fun scrollToBottom() { private fun scrollToBottom() {
...@@ -195,22 +215,4 @@ class LoginFragment : Fragment(), LoginView { ...@@ -195,22 +215,4 @@ class LoginFragment : Fragment(), LoginView {
button_fab.hide() button_fab.hide()
}, 1500) }, 1500)
} }
override fun showLoading() {
// TODO - change for a proper progress indicator
progress = ProgressDialog.show(activity, "Authenticating",
"Verifying user credentials", true, true)
}
override fun hideLoading() {
progress?.apply {
cancel()
}
progress = null
}
override fun onLoginError(message: String?) {
// TODO - show a proper error message
Toast.makeText(activity, message, Toast.LENGTH_LONG).show()
}
} }
\ No newline at end of file
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