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
import DrawableHelper
import android.app.ProgressDialog
import android.os.Build
import android.os.Bundle
import android.support.v4.app.Fragment
......@@ -9,14 +8,33 @@ import android.view.*
import android.widget.ScrollView
import android.widget.Toast
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.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 kotlinx.android.synthetic.main.fragment_authentication_log_in.*
import javax.inject.Inject
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 {
private const val SERVER_URL = "server_url"
......@@ -28,12 +46,8 @@ class LoginFragment : Fragment(), LoginView {
}
}
var progress: ProgressDialog? = null
lateinit var serverUrl: String
@Inject
lateinit var presenter: LoginPresenter
override fun onCreate(savedInstanceState: Bundle?) {
AndroidSupportInjection.inject(this)
super.onCreate(savedInstanceState)
......@@ -53,135 +67,141 @@ class LoginFragment : Fragment(), LoginView {
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)
// 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)
showLoginUsingFacebookImageButton()
showLoginUsingGithubImageButton()
showLoginUsingGoogleImageButton()
enableLoginByFacebook()
enableLoginByGithub()
enableLoginByGoogle()
// Setup the FloatingActionButton to show more social account's ImageButton (it expands the social accounts interface to show more views).
setupFabListener()
// Just an example: if the server allow the new users registration then show the respective interface.
shouldShowSignUpMsgView(true)
button_log_in.setOnClickListener {
presenter.authenticate(text_username_or_email.text.toString(), text_password.text.toString())
}
shouldShowSignUpView(true)
// -------------------------------------------------------------------------------------------------------------------
text_new_to_rocket_chat.setOnClickListener {
presenter.signup()
}
button_log_in.setOnClickListener { presenter.authenticate(text_username_or_email, text_password) }
}
override fun onDestroyView() {
scroll_view.viewTreeObserver.removeOnGlobalLayoutListener(layoutListener)
super.onDestroyView()
if (isGlobalLayoutListenerSetUp) {
scroll_view.viewTreeObserver.removeOnGlobalLayoutListener(layoutListener)
isGlobalLayoutListenerSetUp = false
}
}
private fun tintEditTextDrawableStart() {
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)
override fun shouldShowOauthView(show: Boolean) {
if (show) {
social_accounts_container.setVisibility(true)
button_fab.setVisibility(true)
// 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 or text_password EditText to fill that respective fields).
if (!isGlobalLayoutListenerSetUp) {
scroll_view.viewTreeObserver.addOnGlobalLayoutListener(layoutListener)
isGlobalLayoutListenerSetUp = true
}
} else {
social_accounts_container.setVisibility(false)
button_fab.setVisibility(false)
}
}
private fun setupGlobalLayoutListener() {
scroll_view.viewTreeObserver.addOnGlobalLayoutListener(layoutListener)
override fun setupFabListener() {
button_fab.setOnClickListener({
enableLoginByLinkedin()
enableLoginByMeteor()
enableLoginByTwitter()
enableLoginByGitlab()
scrollToBottom()
hideFab()
})
}
val layoutListener = ViewTreeObserver.OnGlobalLayoutListener {
if (KeyboardHelper.isSoftKeyboardShown(scroll_view.rootView)) {
shouldShowOauthView(false)
shouldShowSignUpMsgView(false)
shouldShowLoginButton(true)
} else {
if (isEditTextNullOrBlank()) {
shouldShowOauthView(true)
shouldShowSignUpMsgView(true)
shouldShowLoginButton(false)
}
}
override fun enableLoginByFacebook() {
button_facebook.setVisibility(true)
}
private fun shouldShowOauthView(show: Boolean) {
if (show) {
social_accounts_container.visibility = View.VISIBLE
button_fab.visibility = View.VISIBLE
} else {
social_accounts_container.visibility = View.GONE
button_fab.visibility = View.GONE
}
override fun enableLoginByGithub() {
button_github.setVisibility(true)
}
private fun shouldShowSignUpMsgView(show: Boolean) {
if (show) {
text_new_to_rocket_chat.visibility = View.VISIBLE
} else {
text_new_to_rocket_chat.visibility = View.GONE
}
override fun enableLoginByGoogle() {
button_google.setVisibility(true)
}
private fun shouldShowLoginButton(show: Boolean) {
if (show) {
button_log_in.visibility = View.VISIBLE
} else {
button_log_in.visibility = View.GONE
}
override fun enableLoginByLinkedin() {
button_linkedin.setVisibility(true)
}
private fun showLoginUsingFacebookImageButton() {
button_facebook.visibility = View.VISIBLE
override fun enableLoginByMeteor() {
button_meteor.setVisibility(true)
}
private fun showLoginUsingGithubImageButton() {
button_github.visibility = View.VISIBLE
override fun enableLoginByTwitter() {
button_twitter.setVisibility(true)
}
private fun showLoginUsingGoogleImageButton() {
button_google.visibility = View.VISIBLE
override fun enableLoginByGitlab() {
button_gitlab.setVisibility(true)
}
private fun showLoginUsingLinkedinImageButton() {
button_linkedin.visibility = View.VISIBLE
override fun shouldShowSignUpView(show: Boolean) {
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() {
button_meteor.visibility = View.VISIBLE
override fun showLoading() {
view_loading.setVisibility(true)
}
private fun showLoginUsingTwitterImageButton() {
button_twitter.visibility = View.VISIBLE
override fun hideLoading() {
view_loading.setVisibility(false)
}
private fun showLoginUsingGitlabImageButton() {
button_gitlab.visibility = View.VISIBLE
override fun showMessage(message: String) {
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
}
private fun setupFabListener() {
button_fab.setOnClickListener({
showLoginUsingLinkedinImageButton()
showLoginUsingMeteorImageButton()
showLoginUsingTwitterImageButton()
showLoginUsingGitlabImageButton()
override fun shakeView(viewToShake: View) {
AnimationHelper.vibrate(viewToShake.context)
AnimationHelper.shakeView(viewToShake)
}
scrollToBottom()
hideFab()
})
private fun tintEditTextDrawableStart() {
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 isEditTextNullOrBlank(): Boolean {
return text_username_or_email.text.isNullOrBlank() && text_password.text.isNullOrBlank()
private fun shouldShowLoginButton(show: Boolean) {
if (show) {
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() {
......@@ -195,22 +215,4 @@ class LoginFragment : Fragment(), LoginView {
button_fab.hide()
}, 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