Fixed freezing when typing fast or using auto complete

This commit is contained in:
Moez Bhatti
2017-12-25 22:27:22 -05:00
parent e56d754379
commit 3a7ed6ecf6
4 changed files with 20 additions and 6 deletions
@@ -178,12 +178,15 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
}
if (title != state.title) title = state.title
if (message.text.toString() != state.draft) message.setText(state.draft)
send.isEnabled = state.canSend
sendIcon.isEnabled = state.canSend
}
override fun setDraft(draft: String) {
message.setText(draft)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.compose, menu)
return super.onCreateOptionsMenu(menu)
@@ -13,6 +13,5 @@ data class ComposeState(
val title: String = "",
val archived: Boolean = false,
val messages: RealmResults<Message>? = null,
val draft: String = "",
val canSend: Boolean = false
)
@@ -22,4 +22,6 @@ interface ComposeView : QkView<ComposeState> {
val attachIntent: Observable<Unit>
val sendIntent: Observable<Unit>
fun setDraft(draft: String)
}
@@ -24,8 +24,8 @@ import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import javax.inject.Inject
class ComposeViewModel(threadId: Long, body: String)
: QkViewModel<ComposeView, ComposeState>(ComposeState(editingMode = threadId == 0L, draft = body)) {
class ComposeViewModel(threadId: Long, var draft: String)
: QkViewModel<ComposeView, ComposeState>(ComposeState(editingMode = threadId == 0L)) {
@Inject lateinit var context: Context
@Inject lateinit var contactFilter: ContactFilter
@@ -108,6 +108,11 @@ class ComposeViewModel(threadId: Long, body: String)
override fun bindView(view: ComposeView) {
super.bindView(view)
if (draft.isNotEmpty()) {
view.setDraft(draft)
draft = ""
}
// Set the contact suggestions list to visible at all times when in editing mode and there are no contacts
// selected yet, and also visible while in editing mode and there is text entered in the query field
intents += Observables
@@ -177,7 +182,7 @@ class ComposeViewModel(threadId: Long, body: String)
// Enable the send button when there is text input into the new message body, disable otherwise
intents += view.textChangedIntent.subscribe { text ->
newState { it.copy(draft = text.toString(), canSend = text.isNotEmpty()) }
newState { it.copy(canSend = text.isNotEmpty()) }
}
// Send a message when the send button is clicked, and disable editing mode if it's enabled
@@ -188,7 +193,12 @@ class ComposeViewModel(threadId: Long, body: String)
val threadId = conversation.id
val address = conversation.recipients.first()?.address.orEmpty()
sendMessage.execute(SendMessage.Params(threadId, address, body))
newState { it.copy(editingMode = false, draft = "", canSend = false) }
view.setDraft("")
})
.withLatestFrom(state, {_, state ->
if (state.editingMode) {
newState { it.copy(editingMode = false) }
}
})
.subscribe()