Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 62e19a15 authored by Moez Bhatti's avatar Moez Bhatti
Browse files

Use withLatestFrom to get message text for sending, instead of relying on state

parent 93762609
Loading
Loading
Loading
Loading
+17 −10
Original line number Diff line number Diff line
@@ -18,10 +18,11 @@ import io.reactivex.BackpressureStrategy
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class ComposeViewModel(val threadId: Long) : QkViewModel<ComposeView, ComposeState>(ComposeState(editingMode = threadId == 0L)) {
class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(ComposeState(editingMode = threadId == 0L)) {

    @Inject lateinit var context: Context
    @Inject lateinit var contactsRepo: ContactRepository
@@ -80,17 +81,21 @@ class ComposeViewModel(val threadId: Long) : QkViewModel<ComposeView, ComposeSta
                view.chipSelectedIntent.map { contact ->
                    { contacts: List<Contact> -> contacts.toMutableList().apply { add(contact) } }
                })
                .scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) })
                .subscribe { contacts -> newState { it.copy(selectedContacts = contacts) } }
                .scan(state.value!!.contacts, { previousState, reducer -> reducer(previousState) })
                .doOnNext { contacts -> newState { it.copy(selectedContacts = contacts) } }
                .subscribe()

        intents += view.textChangedIntent.subscribe { text ->
            newState { it.copy(draft = text.toString(), canSend = text.isNotEmpty()) }
        }

        intents += view.sendIntent.subscribe {
            val previousState = state.value!!
        intents += view.sendIntent
                .withLatestFrom(view.textChangedIntent, { _, body -> body })
                .map { body -> body.toString() }
                .subscribe { body ->
                    val threadId = conversation?.id ?: 0
                    val address = conversation?.contacts?.first()?.address.orEmpty()
            sendMessage.execute(SendMessage.Params(threadId, address, previousState.draft))
                    sendMessage.execute(SendMessage.Params(threadId, address, body))
                    newState { it.copy(draft = "", canSend = false) }
                }

@@ -105,7 +110,9 @@ class ComposeViewModel(val threadId: Long) : QkViewModel<ComposeView, ComposeSta
    }

    fun dataChanged() {
        conversation?.id?.let { threadId ->
            markRead.execute(threadId)
        }
    }

}
 No newline at end of file