From 3a7ed6ecf65f7c474840adad191f7f798bfd32a5 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Mon, 25 Dec 2017 22:27:22 -0500 Subject: [PATCH] Fixed freezing when typing fast or using auto complete --- .../presentation/compose/ComposeActivity.kt | 5 ++++- .../QKSMS/presentation/compose/ComposeState.kt | 1 - .../QKSMS/presentation/compose/ComposeView.kt | 2 ++ .../presentation/compose/ComposeViewModel.kt | 18 ++++++++++++++---- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeActivity.kt b/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeActivity.kt index 23f0e650..3a4cd610 100644 --- a/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeActivity.kt +++ b/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeActivity.kt @@ -178,12 +178,15 @@ class ComposeActivity : QkActivity(), 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) diff --git a/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeState.kt b/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeState.kt index de9fef96..1c1b7b73 100644 --- a/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeState.kt +++ b/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeState.kt @@ -13,6 +13,5 @@ data class ComposeState( val title: String = "", val archived: Boolean = false, val messages: RealmResults? = null, - val draft: String = "", val canSend: Boolean = false ) \ No newline at end of file diff --git a/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeView.kt b/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeView.kt index 84b41b37..cb48c0c8 100644 --- a/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeView.kt +++ b/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeView.kt @@ -22,4 +22,6 @@ interface ComposeView : QkView { val attachIntent: Observable val sendIntent: Observable + fun setDraft(draft: String) + } \ No newline at end of file diff --git a/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeViewModel.kt b/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeViewModel.kt index 349450fa..ab53a225 100644 --- a/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeViewModel.kt +++ b/qksms/src/main/java/com/moez/QKSMS/presentation/compose/ComposeViewModel.kt @@ -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(ComposeState(editingMode = threadId == 0L, draft = body)) { +class ComposeViewModel(threadId: Long, var draft: String) + : QkViewModel(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()