forked from upstream-mirrors/quik
Create QkViewModel, remove need for PartialState objects
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
package com.moez.QKSMS.presentation.base
|
||||||
|
|
||||||
|
import android.arch.lifecycle.MutableLiveData
|
||||||
|
import android.arch.lifecycle.ViewModel
|
||||||
|
import io.reactivex.subjects.PublishSubject
|
||||||
|
|
||||||
|
abstract class QkViewModel<State>(initialState: State) : ViewModel() {
|
||||||
|
|
||||||
|
val state = MutableLiveData<State>()
|
||||||
|
|
||||||
|
private val stateReducer: PublishSubject<(State) -> State> = PublishSubject.create()
|
||||||
|
|
||||||
|
init {
|
||||||
|
stateReducer
|
||||||
|
.scan(initialState, { previousState, reducer -> reducer(previousState) })
|
||||||
|
.subscribe { newState -> state.value = newState }
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun newState(reducer: (State) -> State) {
|
||||||
|
stateReducer.onNext(reducer)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+7
-14
@@ -1,43 +1,36 @@
|
|||||||
package com.moez.QKSMS.presentation.conversations
|
package com.moez.QKSMS.presentation.conversations
|
||||||
|
|
||||||
import android.arch.lifecycle.MutableLiveData
|
|
||||||
import android.arch.lifecycle.ViewModel
|
|
||||||
import com.moez.QKSMS.common.di.AppComponentManager
|
import com.moez.QKSMS.common.di.AppComponentManager
|
||||||
import com.moez.QKSMS.data.model.Message
|
import com.moez.QKSMS.data.model.Message
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import com.moez.QKSMS.domain.interactor.MarkAllSeen
|
import com.moez.QKSMS.domain.interactor.MarkAllSeen
|
||||||
import com.moez.QKSMS.domain.interactor.SyncConversations
|
import com.moez.QKSMS.domain.interactor.SyncConversations
|
||||||
import io.reactivex.subjects.PublishSubject
|
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ConversationListViewModel : ViewModel() {
|
class ConversationListViewModel : QkViewModel<ConversationListViewState>(ConversationListViewState()) {
|
||||||
|
|
||||||
@Inject lateinit var messageRepo: MessageRepository
|
@Inject lateinit var messageRepo: MessageRepository
|
||||||
@Inject lateinit var syncConversations: SyncConversations
|
@Inject lateinit var syncConversations: SyncConversations
|
||||||
@Inject lateinit var markAllSeen: MarkAllSeen
|
@Inject lateinit var markAllSeen: MarkAllSeen
|
||||||
|
|
||||||
val state: MutableLiveData<ConversationListViewState> = MutableLiveData()
|
|
||||||
|
|
||||||
private val partialStates: PublishSubject<PartialState> = PublishSubject.create()
|
|
||||||
private val conversations: RealmResults<Message>
|
private val conversations: RealmResults<Message>
|
||||||
|
|
||||||
init {
|
init {
|
||||||
AppComponentManager.appComponent.inject(this)
|
AppComponentManager.appComponent.inject(this)
|
||||||
|
|
||||||
partialStates
|
|
||||||
.scan(ConversationListViewState(), { previous, changes -> changes.reduce(previous) })
|
|
||||||
.subscribe { newState -> state.value = newState }
|
|
||||||
|
|
||||||
conversations = messageRepo.getConversationMessagesAsync()
|
conversations = messageRepo.getConversationMessagesAsync()
|
||||||
partialStates.onNext(PartialState.ConversationsLoaded(conversations))
|
newState { it.copy(conversations = conversations) }
|
||||||
|
|
||||||
markAllSeen.execute(Unit)
|
markAllSeen.execute(Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onRefresh() {
|
fun onRefresh() {
|
||||||
partialStates.onNext(PartialState.Refreshing(true))
|
newState { it.copy(refreshing = true) }
|
||||||
syncConversations.execute(Unit, { partialStates.onNext(PartialState.Refreshing(false)) })
|
syncConversations.execute(Unit, {
|
||||||
|
newState { it.copy(refreshing = false) }
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
|
|||||||
+1
-19
@@ -6,22 +6,4 @@ import io.realm.OrderedRealmCollection
|
|||||||
data class ConversationListViewState(
|
data class ConversationListViewState(
|
||||||
val conversations: OrderedRealmCollection<Message>? = null,
|
val conversations: OrderedRealmCollection<Message>? = null,
|
||||||
val refreshing: Boolean = false
|
val refreshing: Boolean = false
|
||||||
)
|
)
|
||||||
|
|
||||||
sealed class PartialState {
|
|
||||||
|
|
||||||
abstract fun reduce(previousState: ConversationListViewState): ConversationListViewState
|
|
||||||
|
|
||||||
data class ConversationsLoaded(val conversations: OrderedRealmCollection<Message>) : PartialState() {
|
|
||||||
override fun reduce(previousState: ConversationListViewState): ConversationListViewState {
|
|
||||||
return previousState.copy(conversations = conversations)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class Refreshing(val refreshing: Boolean) : PartialState (){
|
|
||||||
override fun reduce(previousState: ConversationListViewState): ConversationListViewState {
|
|
||||||
return previousState.copy(refreshing = refreshing)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,37 +1,29 @@
|
|||||||
package com.moez.QKSMS.presentation.messages
|
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.common.di.AppComponentManager
|
||||||
import com.moez.QKSMS.data.model.Conversation
|
import com.moez.QKSMS.data.model.Conversation
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import com.moez.QKSMS.domain.interactor.MarkRead
|
import com.moez.QKSMS.domain.interactor.MarkRead
|
||||||
import com.moez.QKSMS.domain.interactor.SendMessage
|
import com.moez.QKSMS.domain.interactor.SendMessage
|
||||||
import io.reactivex.subjects.PublishSubject
|
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class MessageListViewModel : ViewModel() {
|
class MessageListViewModel : QkViewModel<MessageListViewState>(MessageListViewState()) {
|
||||||
|
|
||||||
@Inject lateinit var messageRepo: MessageRepository
|
@Inject lateinit var messageRepo: MessageRepository
|
||||||
@Inject lateinit var sendMessage: SendMessage
|
@Inject lateinit var sendMessage: SendMessage
|
||||||
@Inject lateinit var markRead: MarkRead
|
@Inject lateinit var markRead: MarkRead
|
||||||
|
|
||||||
val state: MutableLiveData<MessageListViewState> = MutableLiveData()
|
|
||||||
var threadId: Long = 0
|
var threadId: Long = 0
|
||||||
set(value) {
|
set(value) {
|
||||||
field = value
|
field = value
|
||||||
newThreadId()
|
newThreadId()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val partialStates: PublishSubject<PartialState> = PublishSubject.create()
|
|
||||||
private var conversation: Conversation? = null
|
private var conversation: Conversation? = null
|
||||||
|
|
||||||
init {
|
init {
|
||||||
AppComponentManager.appComponent.inject(this)
|
AppComponentManager.appComponent.inject(this)
|
||||||
|
|
||||||
partialStates
|
|
||||||
.scan(MessageListViewState(), { previous, changes -> changes.reduce(previous) })
|
|
||||||
.subscribe { newState -> state.value = newState }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun newThreadId() {
|
private fun newThreadId() {
|
||||||
@@ -43,9 +35,9 @@ class MessageListViewModel : ViewModel() {
|
|||||||
true -> {
|
true -> {
|
||||||
val title = conversation.getTitle()
|
val title = conversation.getTitle()
|
||||||
val messages = messageRepo.getMessages(threadId)
|
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) {
|
fun sendMessage(body: String) {
|
||||||
conversation?.takeIf { conversation -> conversation.isValid }?.let { conversation ->
|
conversation?.takeIf { conversation -> conversation.isValid }?.let { conversation ->
|
||||||
sendMessage.execute(SendMessage.Params(threadId, conversation.contacts[0].address, body))
|
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) {
|
fun textChanged(text: String) {
|
||||||
partialStates.onNext(PartialState.TextChanged(text))
|
newState { it.copy(draft = text, canSend = text.isNotEmpty()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
|
|||||||
@@ -7,31 +7,6 @@ data class MessageListViewState(
|
|||||||
val title: String = "",
|
val title: String = "",
|
||||||
val messages: RealmResults<Message>? = null,
|
val messages: RealmResults<Message>? = null,
|
||||||
val draft: String = "",
|
val draft: String = "",
|
||||||
|
val canSend: Boolean = false,
|
||||||
val hasError: 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user