Commit 8344d9ee authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Improving code readability.

parent 1eda85ee
...@@ -30,12 +30,15 @@ import chat.rocket.android.util.extensions.* ...@@ -30,12 +30,15 @@ import chat.rocket.android.util.extensions.*
import chat.rocket.android.widget.emoji.* import chat.rocket.android.widget.emoji.*
import chat.rocket.core.internal.realtime.socket.model.State import chat.rocket.core.internal.realtime.socket.model.State
import dagger.android.support.AndroidSupportInjection import dagger.android.support.AndroidSupportInjection
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.Consumer import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_chat_room.* import kotlinx.android.synthetic.main.fragment_chat_room.*
import kotlinx.android.synthetic.main.message_attachment_options.* import kotlinx.android.synthetic.main.message_attachment_options.*
import kotlinx.android.synthetic.main.message_composer.* import kotlinx.android.synthetic.main.message_composer.*
import kotlinx.android.synthetic.main.message_list.* import kotlinx.android.synthetic.main.message_list.*
import timber.log.Timber
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
import javax.inject.Inject import javax.inject.Inject
...@@ -149,7 +152,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -149,7 +152,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
presenter.unsubscribeMessages(chatRoomId) presenter.unsubscribeMessages(chatRoomId)
handler.removeCallbacksAndMessages(null) handler.removeCallbacksAndMessages(null)
unsubscribeTextMessage() unsubscribeComposeTextMessage()
// Hides the keyboard (if it's opened) before going to any view. // Hides the keyboard (if it's opened) before going to any view.
activity?.apply { activity?.apply {
...@@ -568,7 +571,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -568,7 +571,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
button_show_attachment_options.alpha = 1f button_show_attachment_options.alpha = 1f
button_show_attachment_options.setVisible(true) button_show_attachment_options.setVisible(true)
subscribeTextMessage() subscribeComposeTextMessage()
emojiKeyboardPopup = EmojiKeyboardPopup(activity!!, activity!!.findViewById(R.id.fragment_container)) emojiKeyboardPopup = EmojiKeyboardPopup(activity!!, activity!!.findViewById(R.id.fragment_container))
emojiKeyboardPopup.listener = this emojiKeyboardPopup.listener = this
text_message.listener = object : ComposerEditText.ComposerEditTextListener { text_message.listener = object : ComposerEditText.ComposerEditTextListener {
...@@ -673,21 +676,30 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -673,21 +676,30 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
}) })
} }
private fun subscribeTextMessage() { private fun subscribeComposeTextMessage() {
val composeButtonsDisposable = text_message.asObservable(0) val editTextObservable = text_message.asObservable()
.subscribe { t -> setupComposeMessageButtons(t) }
val typingStatusDisposable = text_message.asObservable(300) compositeDisposable.addAll(
.subscribe { t -> sendTypingStatus(t) } subscribeComposeButtons(editTextObservable),
subscribeComposeTypingStatus(editTextObservable)
compositeDisposable.addAll(composeButtonsDisposable, typingStatusDisposable) )
} }
private fun unsubscribeTextMessage() { private fun unsubscribeComposeTextMessage() {
compositeDisposable.clear() compositeDisposable.clear()
} }
private fun setupComposeMessageButtons(charSequence: CharSequence) { private fun subscribeComposeButtons(observable: Observable<CharSequence>): Disposable {
return observable.subscribe { t -> setupComposeButtons(t) }
}
private fun subscribeComposeTypingStatus(observable: Observable<CharSequence>): Disposable {
return observable.debounce(300, TimeUnit.MILLISECONDS)
.skip(1)
.subscribe { t -> sendTypingStatus(t) }
}
private fun setupComposeButtons(charSequence: CharSequence) {
if (charSequence.isNotEmpty() && playComposeMessageButtonsAnimation) { if (charSequence.isNotEmpty() && playComposeMessageButtonsAnimation) {
button_show_attachment_options.fadeOut(1F, 0F, 120) button_show_attachment_options.fadeOut(1F, 0F, 120)
button_send.fadeIn(0F, 1F, 120) button_send.fadeIn(0F, 1F, 120)
...@@ -702,9 +714,11 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -702,9 +714,11 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
} }
private fun sendTypingStatus(charSequence: CharSequence) { private fun sendTypingStatus(charSequence: CharSequence) {
if (charSequence.isNotEmpty()) { if (charSequence.isNotBlank()) {
Timber.i("SENDING $charSequence")
presenter.sendTyping() presenter.sendTyping()
} else { } else {
Timber.i("SENDING FOR NOT $charSequence")
presenter.sendNotTyping() presenter.sendNotTyping()
} }
} }
......
...@@ -6,9 +6,8 @@ import io.reactivex.Observable ...@@ -6,9 +6,8 @@ import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
fun EditText.asObservable(debounceTimeout: Long = 100): Observable<CharSequence> { fun EditText.asObservable(): Observable<CharSequence> {
return RxTextView.textChanges(this) return RxTextView.textChanges(this)
.debounce(debounceTimeout, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread()) .subscribeOn(AndroidSchedulers.mainThread())
} }
\ 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