From 256df7e8699a84ee1973807d9d4de0fe0d9e9e1d Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 19 Jul 2021 19:18:49 +0200 Subject: [PATCH] Rework on how we dispose the subscriptions --- .../home/room/list/RoomListSectionBuilder.kt | 2 + .../room/list/RoomListSectionBuilderGroup.kt | 13 ++-- .../room/list/RoomListSectionBuilderSpace.kt | 15 +++-- .../home/room/list/RoomListViewModel.kt | 60 +++++++++---------- 4 files changed, 51 insertions(+), 39 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilder.kt b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilder.kt index 5267158000..019a6ceddf 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilder.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilder.kt @@ -20,4 +20,6 @@ import im.vector.app.features.home.RoomListDisplayMode interface RoomListSectionBuilder { fun buildSections(mode: RoomListDisplayMode) : List + + fun dispose() } diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderGroup.kt b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderGroup.kt index 1ca379882c..f101669af3 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderGroup.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderGroup.kt @@ -24,7 +24,7 @@ import im.vector.app.core.resources.StringProvider import im.vector.app.features.home.RoomListDisplayMode import im.vector.app.features.invite.AutoAcceptInvites import im.vector.app.features.invite.showInvites -import io.reactivex.disposables.Disposable +import io.reactivex.disposables.CompositeDisposable import io.reactivex.schedulers.Schedulers import org.matrix.android.sdk.api.query.RoomCategoryFilter import org.matrix.android.sdk.api.query.RoomTagQueryFilter @@ -39,10 +39,11 @@ class RoomListSectionBuilderGroup( private val stringProvider: StringProvider, private val appStateHandler: AppStateHandler, private val autoAcceptInvites: AutoAcceptInvites, - private val onDisposable: (Disposable) -> Unit, private val onUpdatable: (UpdatableLivePageResult) -> Unit ) : RoomListSectionBuilder { + private val disposables = CompositeDisposable() + override fun buildSections(mode: RoomListDisplayMode): List { val activeGroupAwareQueries = mutableListOf() val sections = mutableListOf() @@ -110,7 +111,7 @@ class RoomListSectionBuilderGroup( } } }.also { - onDisposable.invoke(it) + disposables.add(it) } return sections @@ -257,7 +258,7 @@ class RoomListSectionBuilderGroup( ?.notificationCount ?.postValue(session.getNotificationCountForRooms(roomQueryParams)) }.also { - onDisposable.invoke(it) + disposables.add(it) } sections.add( @@ -279,4 +280,8 @@ class RoomListSectionBuilderGroup( .build() .let { block(it) } } + + override fun dispose() { + disposables.dispose() + } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderSpace.kt b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderSpace.kt index 6edbb07992..13a6fc0d2d 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderSpace.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderSpace.kt @@ -30,7 +30,7 @@ import im.vector.app.features.invite.AutoAcceptInvites import im.vector.app.features.invite.showInvites import im.vector.app.space import io.reactivex.Observable -import io.reactivex.disposables.Disposable +import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.Observables import io.reactivex.schedulers.Schedulers import kotlinx.coroutines.CoroutineScope @@ -52,12 +52,13 @@ class RoomListSectionBuilderSpace( private val appStateHandler: AppStateHandler, private val viewModelScope: CoroutineScope, private val autoAcceptInvites: AutoAcceptInvites, - private val onDisposable: (Disposable) -> Unit, private val onUpdatable: (UpdatableLivePageResult) -> Unit, private val suggestedRoomJoiningState: LiveData>>, private val onlyOrphansInHome: Boolean = false ) : RoomListSectionBuilder { + private val disposables = CompositeDisposable() + private val pagedListConfig = PagedList.Config.Builder() .setPageSize(10) .setInitialLoadSizeHint(20) @@ -137,7 +138,7 @@ class RoomListSectionBuilderSpace( updater.updateForSpaceId(selectedSpace?.roomId) } }.also { - onDisposable.invoke(it) + disposables.add(it) } return sections @@ -252,7 +253,7 @@ class RoomListSectionBuilderSpace( }.subscribe { liveSuggestedRooms.postValue(it) }.also { - onDisposable.invoke(it) + disposables.add(it) } sections.add( RoomsSection( @@ -384,7 +385,7 @@ class RoomListSectionBuilderSpace( } ) }.also { - onDisposable.invoke(it) + disposables.add(it) } sections.add( @@ -428,4 +429,8 @@ class RoomListSectionBuilderSpace( RoomListViewModel.SpaceFilterStrategy.NONE -> this } } + + override fun dispose() { + disposables.dispose() + } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListViewModel.kt index e8e46acc2d..845be0b18b 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListViewModel.kt @@ -120,37 +120,32 @@ class RoomListViewModel @Inject constructor( } } - val sections: List by lazy { - if (appStateHandler.getCurrentRoomGroupingMethod() is RoomGroupingMethod.BySpace) { - RoomListSectionBuilderSpace( - session, - stringProvider, - appStateHandler, - viewModelScope, - autoAcceptInvites, - { - it.disposeOnClear() - }, - { - updatableQuery = it - }, - suggestedRoomJoiningState, - vectorPreferences.labsSpacesOnlyOrphansInHome() - ) - } else { - RoomListSectionBuilderGroup( - session, - stringProvider, - appStateHandler, - autoAcceptInvites, - { - it.disposeOnClear() - } - ) { - updatableQuery = it - } + private val roomListSectionBuilder = if (appStateHandler.getCurrentRoomGroupingMethod() is RoomGroupingMethod.BySpace) { + RoomListSectionBuilderSpace( + session, + stringProvider, + appStateHandler, + viewModelScope, + autoAcceptInvites, + { + updatableQuery = it + }, + suggestedRoomJoiningState, + vectorPreferences.labsSpacesOnlyOrphansInHome() + ) + } else { + RoomListSectionBuilderGroup( + session, + stringProvider, + appStateHandler, + autoAcceptInvites + ) { + updatableQuery = it } - .buildSections(initialState.displayMode) + } + + val sections: List by lazy { + roomListSectionBuilder.buildSections(initialState.displayMode) } override fun handle(action: RoomListAction) { @@ -340,4 +335,9 @@ class RoomListViewModel @Inject constructor( _viewEvents.post(value) } } + + override fun onCleared() { + super.onCleared() + roomListSectionBuilder.dispose() + } }