diff --git a/presentation/src/main/java/feature/compose/ComposeViewModel.kt b/presentation/src/main/java/feature/compose/ComposeViewModel.kt index 61118bfa..f259bf1b 100644 --- a/presentation/src/main/java/feature/compose/ComposeViewModel.kt +++ b/presentation/src/main/java/feature/compose/ComposeViewModel.kt @@ -85,8 +85,8 @@ class ComposeViewModel(intent: Intent) : QkViewModel( private val contacts: Observable> by lazy { contactsRepo.getUnmanagedContacts().toObservable() } private val contactsReducer: Subject<(List) -> List> = PublishSubject.create() private val selectedContacts: Observable> - private val conversation: Observable - private val messages: Observable> + private val conversation: Subject = BehaviorSubject.create() + private val messages: Subject> = BehaviorSubject.create() init { appComponent.inject(this) @@ -140,7 +140,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel( // 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( .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( 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)