Create QkViewModel, remove need for PartialState objects

This commit is contained in:
Moez Bhatti
2017-10-31 01:05:52 -07:00
parent 64035e85e9
commit a43ce96719
5 changed files with 39 additions and 74 deletions
@@ -1,37 +1,29 @@
package com.moez.QKSMS.presentation.messages
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import com.moez.QKSMS.common.di.AppComponentManager
import com.moez.QKSMS.data.model.Conversation
import com.moez.QKSMS.data.repository.MessageRepository
import com.moez.QKSMS.domain.interactor.MarkRead
import com.moez.QKSMS.domain.interactor.SendMessage
import io.reactivex.subjects.PublishSubject
import com.moez.QKSMS.presentation.base.QkViewModel
import javax.inject.Inject
class MessageListViewModel : ViewModel() {
class MessageListViewModel : QkViewModel<MessageListViewState>(MessageListViewState()) {
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var sendMessage: SendMessage
@Inject lateinit var markRead: MarkRead
val state: MutableLiveData<MessageListViewState> = MutableLiveData()
var threadId: Long = 0
set(value) {
field = value
newThreadId()
}
private val partialStates: PublishSubject<PartialState> = PublishSubject.create()
private var conversation: Conversation? = null
init {
AppComponentManager.appComponent.inject(this)
partialStates
.scan(MessageListViewState(), { previous, changes -> changes.reduce(previous) })
.subscribe { newState -> state.value = newState }
}
private fun newThreadId() {
@@ -43,9 +35,9 @@ class MessageListViewModel : ViewModel() {
true -> {
val title = conversation.getTitle()
val messages = messageRepo.getMessages(threadId)
partialStates.onNext(PartialState.ConversationLoaded(title, messages))
newState { it.copy(title = title, messages = messages) }
}
false -> partialStates.onNext(PartialState.ConversationError(true))
false -> newState { it.copy(hasError = true) }
}
}
}
@@ -53,7 +45,7 @@ class MessageListViewModel : ViewModel() {
fun sendMessage(body: String) {
conversation?.takeIf { conversation -> conversation.isValid }?.let { conversation ->
sendMessage.execute(SendMessage.Params(threadId, conversation.contacts[0].address, body))
partialStates.onNext(PartialState.TextChanged(""))
newState { it.copy(draft = "", canSend = false) }
}
}
@@ -62,7 +54,7 @@ class MessageListViewModel : ViewModel() {
}
fun textChanged(text: String) {
partialStates.onNext(PartialState.TextChanged(text))
newState { it.copy(draft = text, canSend = text.isNotEmpty()) }
}
override fun onCleared() {
@@ -7,31 +7,6 @@ data class MessageListViewState(
val title: String = "",
val messages: RealmResults<Message>? = null,
val draft: String = "",
val canSend: Boolean = false,
val hasError: Boolean = false
) {
val canSend: Boolean get() = draft.isNotBlank()
}
sealed class PartialState {
abstract fun reduce(previousState: MessageListViewState): MessageListViewState
data class ConversationLoaded(private val title: String, private val messages: RealmResults<Message>) : PartialState() {
override fun reduce(previousState: MessageListViewState): MessageListViewState {
return previousState.copy(title = title, messages = messages)
}
}
data class ConversationError(private val hasError: Boolean) : PartialState() {
override fun reduce(previousState: MessageListViewState): MessageListViewState {
return previousState.copy(hasError = hasError)
}
}
data class TextChanged(private val text: String) : PartialState() {
override fun reduce(previousState: MessageListViewState): MessageListViewState {
return previousState.copy(draft = text)
}
}
}
)