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 contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
private val selectedContacts: Observable<List<Contact>>
private val conversation: Observable<Conversation>
private val messages: Observable<List<Message>>
private val conversation: Subject<Conversation> = BehaviorSubject.create()
private val messages: Subject<List<Message>> = BehaviorSubject.create()
init {
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
// stream of conversations. If the conversation was deleted, notify the activity to shut down
conversation = selectedContacts
disposables += selectedContacts
.skipUntil(state.filter { state -> state.editingMode })
.takeUntil(state.filter { state -> !state.editingMode })
.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.id != 0L }
.distinctUntilChanged()
.subscribe { conversation.onNext(it) }
// When the conversation changes, update the threadId and the messages for the adapter
messages = conversation
disposables += conversation
.distinctUntilChanged { conversation -> conversation.id }
.observeOn(AndroidSchedulers.mainThread())
.map { conversation ->
@@ -166,20 +167,15 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
messages
}
.switchMap { messages -> messages.asObservable() }
disposables += conversation.subscribe()
disposables += messages.subscribe()
.subscribe { messages.onNext(it) }
disposables += conversation
.distinctUntilChanged { conversation -> conversation.getTitle() }
.subscribe { conversation ->
newState { it.copy(conversationtitle = conversation.getTitle()) }
}
.map { conversation -> conversation.getTitle() }
.distinctUntilChanged()
.subscribe { title -> newState { it.copy(conversationtitle = title) } }
disposables += attachments
.subscribe { attachments ->
newState { it.copy(attachments = attachments) }
}
.subscribe { attachments -> newState { it.copy(attachments = attachments) } }
if (threadId == 0L) {
syncContacts.execute(Unit)