mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-23 06:08:52 +01:00
Full implementation of MVVM/MVI in MessageList
This commit is contained in:
@@ -4,6 +4,7 @@ import android.arch.lifecycle.Observer
|
|||||||
import android.arch.lifecycle.ViewModelProviders
|
import android.arch.lifecycle.ViewModelProviders
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.support.v7.widget.LinearLayoutManager
|
import android.support.v7.widget.LinearLayoutManager
|
||||||
|
import com.jakewharton.rxbinding2.view.RxView
|
||||||
import com.jakewharton.rxbinding2.widget.RxTextView
|
import com.jakewharton.rxbinding2.widget.RxTextView
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.moez.QKSMS.ui.base.QkActivity
|
import com.moez.QKSMS.ui.base.QkActivity
|
||||||
@@ -22,47 +23,38 @@ class MessageListActivity : QkActivity(), Observer<MessageListViewState> {
|
|||||||
|
|
||||||
viewModel = ViewModelProviders.of(this)[MessageListViewModel::class.java]
|
viewModel = ViewModelProviders.of(this)[MessageListViewModel::class.java]
|
||||||
viewModel.setThreadId(intent.getLongExtra("thread_id", 0))
|
viewModel.setThreadId(intent.getLongExtra("thread_id", 0))
|
||||||
|
viewModel.state.observe(this)
|
||||||
|
|
||||||
val layoutManager = LinearLayoutManager(this)
|
val layoutManager = LinearLayoutManager(this)
|
||||||
layoutManager.stackFromEnd = true
|
layoutManager.stackFromEnd = true
|
||||||
messageList.layoutManager = layoutManager
|
messageList.layoutManager = layoutManager
|
||||||
|
|
||||||
RxTextView.textChanges(message).subscribe { text ->
|
RxTextView.textChanges(message).subscribe { text -> viewModel.textChanged(text.toString()) }
|
||||||
val enabled = text.isNotEmpty()
|
RxView.clicks(attach).subscribe { }
|
||||||
val color = if (enabled) R.color.colorPrimary else R.color.textTertiary
|
RxView.clicks(send).subscribe { viewModel.sendMessage(message.text.toString()) }
|
||||||
send.setTint(resources.getColor(color))
|
|
||||||
send.isEnabled = enabled
|
|
||||||
}
|
|
||||||
|
|
||||||
attach.setOnClickListener {}
|
|
||||||
send.setOnClickListener { viewModel.sendMessage(message.text.toString()) }
|
|
||||||
|
|
||||||
viewModel.state.observe(this)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onChanged(state: MessageListViewState?) {
|
override fun onChanged(state: MessageListViewState?) {
|
||||||
when (state) {
|
state?.let {
|
||||||
is MessageListViewState.ConversationLoaded -> onConversationLoaded(state)
|
if (title != state.title) {
|
||||||
is MessageListViewState.ConversationError -> onConversationError(state)
|
title = state.title
|
||||||
is MessageListViewState.MessagesLoaded -> onMessagesLoaded(state)
|
}
|
||||||
is MessageListViewState.DraftLoaded -> onDraftLoaded(state)
|
|
||||||
|
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.model.Message
|
||||||
import com.moez.QKSMS.data.repository.ConversationRepository
|
import com.moez.QKSMS.data.repository.ConversationRepository
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
|
import io.reactivex.subjects.PublishSubject
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ class MessageListViewModel : ViewModel() {
|
|||||||
@Inject lateinit var conversationRepo: ConversationRepository
|
@Inject lateinit var conversationRepo: ConversationRepository
|
||||||
@Inject lateinit var messageRepo: MessageRepository
|
@Inject lateinit var messageRepo: MessageRepository
|
||||||
|
|
||||||
|
val partialStates: PublishSubject<PartialState> = PublishSubject.create()
|
||||||
val state: MutableLiveData<MessageListViewState> = MutableLiveData()
|
val state: MutableLiveData<MessageListViewState> = MutableLiveData()
|
||||||
|
|
||||||
private var threadId: Long = 0
|
private var threadId: Long = 0
|
||||||
@@ -24,6 +26,11 @@ class MessageListViewModel : ViewModel() {
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
AppComponentManager.appComponent.inject(this)
|
AppComponentManager.appComponent.inject(this)
|
||||||
|
|
||||||
|
val initialState = MessageListViewState()
|
||||||
|
partialStates
|
||||||
|
.scan(initialState, { previousState, changes -> changes.reduce(previousState) })
|
||||||
|
.subscribe { newState -> state.value = newState }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setThreadId(threadId: Long) {
|
fun setThreadId(threadId: Long) {
|
||||||
@@ -33,15 +40,15 @@ class MessageListViewModel : ViewModel() {
|
|||||||
|
|
||||||
messageRepo.getMessages(threadId).let {
|
messageRepo.getMessages(threadId).let {
|
||||||
messages = it
|
messages = it
|
||||||
state.value = MessageListViewState.MessagesLoaded(it)
|
partialStates.onNext(PartialState.MessagesLoaded(it))
|
||||||
}
|
}
|
||||||
|
|
||||||
conversationRepo.getConversation(threadId).let {
|
conversationRepo.getConversation(threadId).let {
|
||||||
conversation = it
|
conversation = it
|
||||||
it.addChangeListener { realmResults ->
|
it.addChangeListener { realmResults ->
|
||||||
when (realmResults.size) {
|
when (realmResults.size) {
|
||||||
0 -> state.value = MessageListViewState.ConversationError(0)
|
0 -> partialStates.onNext(PartialState.ConversationError(true))
|
||||||
else -> state.value = MessageListViewState.ConversationLoaded(realmResults[0])
|
else -> partialStates.onNext(PartialState.ConversationLoaded(realmResults[0]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,10 +57,14 @@ class MessageListViewModel : ViewModel() {
|
|||||||
fun sendMessage(body: String) {
|
fun sendMessage(body: String) {
|
||||||
conversation?.get(0)?.let { conversation ->
|
conversation?.get(0)?.let { conversation ->
|
||||||
messageRepo.sendMessage(threadId, conversation.contacts[0].address, body)
|
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() {
|
override fun onCleared() {
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
conversation?.removeAllChangeListeners()
|
conversation?.removeAllChangeListeners()
|
||||||
|
|||||||
@@ -4,9 +4,41 @@ import com.moez.QKSMS.data.model.Conversation
|
|||||||
import com.moez.QKSMS.data.model.Message
|
import com.moez.QKSMS.data.model.Message
|
||||||
import io.realm.OrderedRealmCollection
|
import io.realm.OrderedRealmCollection
|
||||||
|
|
||||||
sealed class MessageListViewState {
|
data class MessageListViewState(
|
||||||
data class ConversationLoaded(val conversation: Conversation) : MessageListViewState()
|
val title: String = "",
|
||||||
data class ConversationError(val errorCode: Int) : MessageListViewState()
|
val messages: OrderedRealmCollection<Message>? = null,
|
||||||
data class MessagesLoaded(val messages: OrderedRealmCollection<Message>) : MessageListViewState()
|
val draft: String = "",
|
||||||
data class DraftLoaded(val draft: String) : MessageListViewState()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user