Full implementation of MVVM/MVI in MessageList

This commit is contained in:
Moez Bhatti
2017-09-21 00:56:12 -04:00
parent b53895e4c8
commit b323005425
3 changed files with 77 additions and 42 deletions
@@ -4,6 +4,7 @@ import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.jakewharton.rxbinding2.view.RxView
import com.jakewharton.rxbinding2.widget.RxTextView
import com.moez.QKSMS.R
import com.moez.QKSMS.ui.base.QkActivity
@@ -22,47 +23,38 @@ class MessageListActivity : QkActivity(), Observer<MessageListViewState> {
viewModel = ViewModelProviders.of(this)[MessageListViewModel::class.java]
viewModel.setThreadId(intent.getLongExtra("thread_id", 0))
viewModel.state.observe(this)
val layoutManager = LinearLayoutManager(this)
layoutManager.stackFromEnd = true
messageList.layoutManager = layoutManager
RxTextView.textChanges(message).subscribe { text ->
val enabled = text.isNotEmpty()
val color = if (enabled) R.color.colorPrimary else R.color.textTertiary
send.setTint(resources.getColor(color))
send.isEnabled = enabled
}
attach.setOnClickListener {}
send.setOnClickListener { viewModel.sendMessage(message.text.toString()) }
viewModel.state.observe(this)
RxTextView.textChanges(message).subscribe { text -> viewModel.textChanged(text.toString()) }
RxView.clicks(attach).subscribe { }
RxView.clicks(send).subscribe { viewModel.sendMessage(message.text.toString()) }
}
override fun onChanged(state: MessageListViewState?) {
when (state) {
is MessageListViewState.ConversationLoaded -> onConversationLoaded(state)
is MessageListViewState.ConversationError -> onConversationError(state)
is MessageListViewState.MessagesLoaded -> onMessagesLoaded(state)
is MessageListViewState.DraftLoaded -> onDraftLoaded(state)
state?.let {
if (title != state.title) {
title = state.title
}
if (messageList.adapter == null) {
messageList.adapter = MessageAdapter(this, state.messages)
}
if (message.text.toString() != state.draft) {
message.setText(state.draft)
}
val color = if (state.canSend) R.color.colorPrimary else R.color.textTertiary
send.setTint(resources.getColor(color))
send.isEnabled = state.canSend
if (state.hasError) {
finish()
}
}
}
private fun onConversationLoaded(conversationLoaded: MessageListViewState.ConversationLoaded) {
title = conversationLoaded.conversation.getTitle()
}
private fun onConversationError(conversationError: MessageListViewState.ConversationError) {
finish()
}
private fun onMessagesLoaded(messagesLoaded: MessageListViewState.MessagesLoaded) {
messageList.adapter = MessageAdapter(this, messagesLoaded.messages)
}
private fun onDraftLoaded(draftLoaded: MessageListViewState.DraftLoaded) {
message.setText(draftLoaded.draft)
}
}
@@ -7,6 +7,7 @@ import com.moez.QKSMS.data.model.Conversation
import com.moez.QKSMS.data.model.Message
import com.moez.QKSMS.data.repository.ConversationRepository
import com.moez.QKSMS.data.repository.MessageRepository
import io.reactivex.subjects.PublishSubject
import io.realm.RealmResults
import javax.inject.Inject
@@ -15,6 +16,7 @@ class MessageListViewModel : ViewModel() {
@Inject lateinit var conversationRepo: ConversationRepository
@Inject lateinit var messageRepo: MessageRepository
val partialStates: PublishSubject<PartialState> = PublishSubject.create()
val state: MutableLiveData<MessageListViewState> = MutableLiveData()
private var threadId: Long = 0
@@ -24,6 +26,11 @@ class MessageListViewModel : ViewModel() {
init {
AppComponentManager.appComponent.inject(this)
val initialState = MessageListViewState()
partialStates
.scan(initialState, { previousState, changes -> changes.reduce(previousState) })
.subscribe { newState -> state.value = newState }
}
fun setThreadId(threadId: Long) {
@@ -33,15 +40,15 @@ class MessageListViewModel : ViewModel() {
messageRepo.getMessages(threadId).let {
messages = it
state.value = MessageListViewState.MessagesLoaded(it)
partialStates.onNext(PartialState.MessagesLoaded(it))
}
conversationRepo.getConversation(threadId).let {
conversation = it
it.addChangeListener { realmResults ->
when (realmResults.size) {
0 -> state.value = MessageListViewState.ConversationError(0)
else -> state.value = MessageListViewState.ConversationLoaded(realmResults[0])
0 -> partialStates.onNext(PartialState.ConversationError(true))
else -> partialStates.onNext(PartialState.ConversationLoaded(realmResults[0]))
}
}
}
@@ -50,10 +57,14 @@ class MessageListViewModel : ViewModel() {
fun sendMessage(body: String) {
conversation?.get(0)?.let { conversation ->
messageRepo.sendMessage(threadId, conversation.contacts[0].address, body)
state.value = MessageListViewState.DraftLoaded("")
partialStates.onNext(PartialState.TextChanged(""))
}
}
fun textChanged(text: String) {
partialStates.onNext(PartialState.TextChanged(text))
}
override fun onCleared() {
super.onCleared()
conversation?.removeAllChangeListeners()
@@ -4,9 +4,41 @@ import com.moez.QKSMS.data.model.Conversation
import com.moez.QKSMS.data.model.Message
import io.realm.OrderedRealmCollection
sealed class MessageListViewState {
data class ConversationLoaded(val conversation: Conversation) : MessageListViewState()
data class ConversationError(val errorCode: Int) : MessageListViewState()
data class MessagesLoaded(val messages: OrderedRealmCollection<Message>) : MessageListViewState()
data class DraftLoaded(val draft: String) : MessageListViewState()
data class MessageListViewState(
val title: String = "",
val messages: OrderedRealmCollection<Message>? = null,
val draft: String = "",
val hasError: Boolean = false
) {
val canSend: Boolean get() = draft.isNotBlank()
}
sealed class PartialState {
abstract fun reduce(previousState: MessageListViewState): MessageListViewState
data class ConversationLoaded(private val conversation: Conversation) : PartialState() {
override fun reduce(previousState: MessageListViewState): MessageListViewState {
return previousState.copy(title = conversation.getTitle())
}
}
data class ConversationError(private val hasError: Boolean) : PartialState() {
override fun reduce(previousState: MessageListViewState): MessageListViewState {
return previousState.copy(hasError = hasError)
}
}
data class MessagesLoaded(private val messages: OrderedRealmCollection<Message>) : PartialState() {
override fun reduce(previousState: MessageListViewState): MessageListViewState {
return previousState.copy(messages = messages)
}
}
data class TextChanged(private val text: String) : PartialState() {
override fun reduce(previousState: MessageListViewState): MessageListViewState {
return previousState.copy(draft = text)
}
}
}