mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
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
|
||||
|
||||
import android.arch.lifecycle.MutableLiveData
|
||||
import android.arch.lifecycle.ViewModel
|
||||
import com.moez.QKSMS.common.di.AppComponentManager
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.domain.interactor.MarkAllSeen
|
||||
import com.moez.QKSMS.domain.interactor.SyncConversations
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||
import io.realm.RealmResults
|
||||
import javax.inject.Inject
|
||||
|
||||
class ConversationListViewModel : ViewModel() {
|
||||
class ConversationListViewModel : QkViewModel<ConversationListViewState>(ConversationListViewState()) {
|
||||
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var syncConversations: SyncConversations
|
||||
@Inject lateinit var markAllSeen: MarkAllSeen
|
||||
|
||||
val state: MutableLiveData<ConversationListViewState> = MutableLiveData()
|
||||
|
||||
private val partialStates: PublishSubject<PartialState> = PublishSubject.create()
|
||||
private val conversations: RealmResults<Message>
|
||||
|
||||
init {
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
|
||||
partialStates
|
||||
.scan(ConversationListViewState(), { previous, changes -> changes.reduce(previous) })
|
||||
.subscribe { newState -> state.value = newState }
|
||||
|
||||
conversations = messageRepo.getConversationMessagesAsync()
|
||||
partialStates.onNext(PartialState.ConversationsLoaded(conversations))
|
||||
newState { it.copy(conversations = conversations) }
|
||||
|
||||
markAllSeen.execute(Unit)
|
||||
}
|
||||
|
||||
fun onRefresh() {
|
||||
partialStates.onNext(PartialState.Refreshing(true))
|
||||
syncConversations.execute(Unit, { partialStates.onNext(PartialState.Refreshing(false)) })
|
||||
newState { it.copy(refreshing = true) }
|
||||
syncConversations.execute(Unit, {
|
||||
newState { it.copy(refreshing = false) }
|
||||
})
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
|
||||
+1
-19
@@ -6,22 +6,4 @@ import io.realm.OrderedRealmCollection
|
||||
data class ConversationListViewState(
|
||||
val conversations: OrderedRealmCollection<Message>? = null,
|
||||
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
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user