Don't redundantly load conversation and messages

Entire observable chains for messages and conversation would be called for every subscription, instead of just once
This commit is contained in:
Moez Bhatti
2018-05-07 15:04:10 -04:00
parent dc4663ee14
commit 8119674a44
@@ -85,8 +85,8 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() } private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create() private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
private val selectedContacts: Observable<List<Contact>> private val selectedContacts: Observable<List<Contact>>
private val conversation: Observable<Conversation> private val conversation: Subject<Conversation> = BehaviorSubject.create()
private val messages: Observable<List<Message>> private val messages: Subject<List<Message>> = BehaviorSubject.create()
init { init {
appComponent.inject(this) appComponent.inject(this)
@@ -140,7 +140,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
// Merges two potential conversation sources (threadId from constructor and contact selection) into a single // Merges two potential conversation sources (threadId from constructor and contact selection) into a single
// stream of conversations. If the conversation was deleted, notify the activity to shut down // stream of conversations. If the conversation was deleted, notify the activity to shut down
conversation = selectedContacts disposables += selectedContacts
.skipUntil(state.filter { state -> state.editingMode }) .skipUntil(state.filter { state -> state.editingMode })
.takeUntil(state.filter { state -> !state.editingMode }) .takeUntil(state.filter { state -> !state.editingMode })
.map { contacts -> contacts.map { it.numbers.firstOrNull()?.address ?: "" } } .map { contacts -> contacts.map { it.numbers.firstOrNull()?.address ?: "" } }
@@ -155,9 +155,10 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
.filter { conversation -> conversation.isValid } .filter { conversation -> conversation.isValid }
.filter { conversation -> conversation.id != 0L } .filter { conversation -> conversation.id != 0L }
.distinctUntilChanged() .distinctUntilChanged()
.subscribe { conversation.onNext(it) }
// When the conversation changes, update the threadId and the messages for the adapter // When the conversation changes, update the threadId and the messages for the adapter
messages = conversation disposables += conversation
.distinctUntilChanged { conversation -> conversation.id } .distinctUntilChanged { conversation -> conversation.id }
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.map { conversation -> .map { conversation ->
@@ -166,20 +167,15 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
messages messages
} }
.switchMap { messages -> messages.asObservable() } .switchMap { messages -> messages.asObservable() }
.subscribe { messages.onNext(it) }
disposables += conversation.subscribe()
disposables += messages.subscribe()
disposables += conversation disposables += conversation
.distinctUntilChanged { conversation -> conversation.getTitle() } .map { conversation -> conversation.getTitle() }
.subscribe { conversation -> .distinctUntilChanged()
newState { it.copy(conversationtitle = conversation.getTitle()) } .subscribe { title -> newState { it.copy(conversationtitle = title) } }
}
disposables += attachments disposables += attachments
.subscribe { attachments -> .subscribe { attachments -> newState { it.copy(attachments = attachments) } }
newState { it.copy(attachments = attachments) }
}
if (threadId == 0L) { if (threadId == 0L) {
syncContacts.execute(Unit) syncContacts.execute(Unit)