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