forked from upstream-mirrors/quik
Fix potential memory leak from chip selection/deselection
This commit is contained in:
@@ -21,6 +21,8 @@ import io.reactivex.android.schedulers.AndroidSchedulers
|
|||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import io.reactivex.rxkotlin.withLatestFrom
|
import io.reactivex.rxkotlin.withLatestFrom
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
import io.reactivex.subjects.PublishSubject
|
||||||
|
import io.reactivex.subjects.Subject
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(ComposeState(editingMode = threadId == 0L)) {
|
class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(ComposeState(editingMode = threadId == 0L)) {
|
||||||
@@ -32,13 +34,20 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
@Inject lateinit var markRead: MarkRead
|
@Inject lateinit var markRead: MarkRead
|
||||||
@Inject lateinit var deleteMessage: DeleteMessage
|
@Inject lateinit var deleteMessage: DeleteMessage
|
||||||
|
|
||||||
|
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
|
||||||
|
|
||||||
private val contacts: List<Contact>
|
private val contacts: List<Contact>
|
||||||
|
|
||||||
|
private val selectedContacts: Observable<List<Contact>>
|
||||||
private val conversation: Observable<Conversation>
|
private val conversation: Observable<Conversation>
|
||||||
|
|
||||||
init {
|
init {
|
||||||
AppComponentManager.appComponent.inject(this)
|
AppComponentManager.appComponent.inject(this)
|
||||||
|
|
||||||
|
selectedContacts = contactsReducer
|
||||||
|
.scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) })
|
||||||
|
.doOnNext { contacts -> newState { it.copy(selectedContacts = contacts) } }
|
||||||
|
|
||||||
conversation = messageRepo.getConversationAsync(threadId)
|
conversation = messageRepo.getConversationAsync(threadId)
|
||||||
.asObservable<Conversation>()
|
.asObservable<Conversation>()
|
||||||
.filter { conversation -> conversation.isLoaded }
|
.filter { conversation -> conversation.isLoaded }
|
||||||
@@ -49,6 +58,7 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
disposables += sendMessage
|
disposables += sendMessage
|
||||||
disposables += markRead
|
disposables += markRead
|
||||||
disposables += conversation.subscribe()
|
disposables += conversation.subscribe()
|
||||||
|
disposables += selectedContacts.subscribe()
|
||||||
|
|
||||||
// When the conversation changes, update the messages for the adapter
|
// When the conversation changes, update the messages for the adapter
|
||||||
// When the message list changes, make sure to mark them read
|
// When the message list changes, make sure to mark them read
|
||||||
@@ -80,16 +90,13 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe { contacts -> newState { it.copy(contacts = contacts) } }
|
.subscribe { contacts -> newState { it.copy(contacts = contacts) } }
|
||||||
|
|
||||||
intents += Observable.merge<(List<Contact>) -> List<Contact>>(
|
intents += view.chipDeletedIntent.subscribe { contact ->
|
||||||
view.chipDeletedIntent.map { contact ->
|
contactsReducer.onNext { contacts -> contacts.filterNot { it == contact } }
|
||||||
{ contacts: List<Contact> -> contacts.filterNot { it == contact } }
|
}
|
||||||
},
|
|
||||||
view.chipSelectedIntent.map { contact ->
|
intents += view.chipSelectedIntent.subscribe { contact ->
|
||||||
{ contacts: List<Contact> -> contacts.toMutableList().apply { add(contact) } }
|
contactsReducer.onNext { contacts -> contacts.toMutableList().apply { add(contact) } }
|
||||||
})
|
}
|
||||||
.scan(state.value!!.contacts, { previousState, reducer -> reducer(previousState) })
|
|
||||||
.doOnNext { contacts -> newState { it.copy(selectedContacts = contacts) } }
|
|
||||||
.subscribe()
|
|
||||||
|
|
||||||
intents += view.textChangedIntent.subscribe { text ->
|
intents += view.textChangedIntent.subscribe { text ->
|
||||||
newState { it.copy(draft = text.toString(), canSend = text.isNotEmpty()) }
|
newState { it.copy(draft = text.toString(), canSend = text.isNotEmpty()) }
|
||||||
|
|||||||
Reference in New Issue
Block a user