mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
Dispose of all Rx Streams and Intents in ViewModel
This commit is contained in:
@@ -7,7 +7,7 @@ import io.reactivex.schedulers.Schedulers
|
||||
|
||||
abstract class Interactor<T, in Params> {
|
||||
|
||||
private val disposables: CompositeDisposable = CompositeDisposable()
|
||||
val disposables: CompositeDisposable = CompositeDisposable()
|
||||
|
||||
abstract fun buildObservable(params: Params): Flowable<T>
|
||||
|
||||
@@ -18,6 +18,4 @@ abstract class Interactor<T, in Params> {
|
||||
.subscribe(observer))
|
||||
}
|
||||
|
||||
fun dispose() = disposables.dispose()
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.moez.QKSMS.presentation.base
|
||||
|
||||
import android.arch.lifecycle.MutableLiveData
|
||||
import android.arch.lifecycle.ViewModel
|
||||
import android.support.annotation.CallSuper
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
|
||||
abstract class QkViewModel<in View : QkView<State>, State>(initialState: State) : ViewModel() {
|
||||
@@ -10,6 +12,9 @@ abstract class QkViewModel<in View : QkView<State>, State>(initialState: State)
|
||||
|
||||
private val stateReducer: PublishSubject<(State) -> State> = PublishSubject.create()
|
||||
|
||||
protected val disposables = CompositeDisposable()
|
||||
protected val intents = CompositeDisposable()
|
||||
|
||||
init {
|
||||
stateReducer
|
||||
.scan(initialState, { previousState, reducer -> reducer(previousState) })
|
||||
@@ -20,10 +25,19 @@ abstract class QkViewModel<in View : QkView<State>, State>(initialState: State)
|
||||
bindIntents(view)
|
||||
}
|
||||
|
||||
open fun bindIntents(view: View) {}
|
||||
@CallSuper
|
||||
open fun bindIntents(view: View) {
|
||||
intents.clear()
|
||||
}
|
||||
|
||||
protected fun newState(reducer: (State) -> State) {
|
||||
stateReducer.onNext(reducer)
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
disposables.dispose()
|
||||
intents.dispose()
|
||||
}
|
||||
|
||||
}
|
||||
+8
-10
@@ -6,6 +6,7 @@ import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.domain.interactor.MarkAllSeen
|
||||
import com.moez.QKSMS.presentation.Navigator
|
||||
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.realm.RealmResults
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -20,6 +21,8 @@ class ConversationsViewModel : QkViewModel<ConversationsView, ConversationsState
|
||||
init {
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
|
||||
disposables += markAllSeen.disposables
|
||||
|
||||
conversations = messageRepo.getConversationMessagesAsync()
|
||||
newState { it.copy(conversations = conversations) }
|
||||
|
||||
@@ -29,16 +32,11 @@ class ConversationsViewModel : QkViewModel<ConversationsView, ConversationsState
|
||||
override fun bindIntents(view: ConversationsView) {
|
||||
super.bindIntents(view)
|
||||
|
||||
view.composeIntent.subscribe()
|
||||
view.archivedIntent.subscribe()
|
||||
view.scheduledIntent.subscribe()
|
||||
view.blockedIntent.subscribe()
|
||||
view.settingsIntent.subscribe { navigator.showSettings() }
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
markAllSeen.dispose()
|
||||
intents += view.composeIntent.subscribe()
|
||||
intents += view.archivedIntent.subscribe()
|
||||
intents += view.scheduledIntent.subscribe()
|
||||
intents += view.blockedIntent.subscribe()
|
||||
intents += view.settingsIntent.subscribe { navigator.showSettings() }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.domain.interactor.MarkRead
|
||||
import com.moez.QKSMS.domain.interactor.SendMessage
|
||||
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import javax.inject.Inject
|
||||
|
||||
class MessagesViewModel(val threadId: Long) : QkViewModel<MessagesView, MessagesState>(MessagesState()) {
|
||||
@@ -19,6 +20,9 @@ class MessagesViewModel(val threadId: Long) : QkViewModel<MessagesView, Messages
|
||||
init {
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
|
||||
disposables += sendMessage.disposables
|
||||
disposables += markRead.disposables
|
||||
|
||||
dataChanged()
|
||||
conversation = messageRepo.getConversationAsync(threadId)
|
||||
conversation.addChangeListener { conversation: Conversation ->
|
||||
@@ -36,11 +40,11 @@ class MessagesViewModel(val threadId: Long) : QkViewModel<MessagesView, Messages
|
||||
override fun bindIntents(view: MessagesView) {
|
||||
super.bindIntents(view)
|
||||
|
||||
view.textChangedIntent.subscribe { text ->
|
||||
intents += view.textChangedIntent.subscribe { text ->
|
||||
newState { it.copy(draft = text.toString(), canSend = text.isNotEmpty()) }
|
||||
}
|
||||
|
||||
view.sendIntent.subscribe {
|
||||
intents += view.sendIntent.subscribe {
|
||||
val previousState = state.value!!
|
||||
sendMessage.execute(SendMessage.Params(threadId, conversation.contacts[0].address, previousState.draft))
|
||||
newState { it.copy(draft = "", canSend = false) }
|
||||
@@ -53,8 +57,6 @@ class MessagesViewModel(val threadId: Long) : QkViewModel<MessagesView, Messages
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
sendMessage.dispose()
|
||||
markRead.dispose()
|
||||
conversation.removeAllChangeListeners()
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.provider.Telephony
|
||||
import com.moez.QKSMS.common.di.AppComponentManager
|
||||
import com.moez.QKSMS.domain.interactor.SyncConversations
|
||||
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -16,12 +17,14 @@ class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState
|
||||
|
||||
init {
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
|
||||
disposables += syncConversations.disposables
|
||||
}
|
||||
|
||||
override fun bindIntents(view: SettingsView) {
|
||||
super.bindIntents(view)
|
||||
|
||||
view.preferenceClickIntent.subscribe {
|
||||
intents += view.preferenceClickIntent.subscribe {
|
||||
Timber.v("Preference click: ${it.key}")
|
||||
|
||||
when (it.key) {
|
||||
@@ -42,9 +45,4 @@ class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
syncConversations.dispose()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user