From c071dc502e084c48ffabd91ccacb614b85af4b85 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 30 May 2022 11:33:46 +0200 Subject: [PATCH] Create extension `String?.toActiveSpaceOrOrphanRooms()` to reduce noise. --- .../java/org/matrix/android/sdk/api/query/SpaceFilter.kt | 5 +++++ .../im/vector/app/features/home/HomeDetailViewModel.kt | 9 +++++++-- .../home/room/list/RoomListSectionBuilderSpace.kt | 5 +++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/query/SpaceFilter.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/query/SpaceFilter.kt index 237f9388f2..6383412ffb 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/query/SpaceFilter.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/query/SpaceFilter.kt @@ -36,3 +36,8 @@ sealed interface SpaceFilter { */ data class ExcludeSpace(val spaceId: String) : SpaceFilter } + +/** + * Return a [SpaceFilter.ActiveSpace] if the String is not null, or [SpaceFilter.OrphanRooms]. + */ +fun String?.toActiveSpaceOrOrphanRooms(): SpaceFilter = this?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms diff --git a/vector/src/main/java/im/vector/app/features/home/HomeDetailViewModel.kt b/vector/src/main/java/im/vector/app/features/home/HomeDetailViewModel.kt index 5044e71a50..30bff45cbd 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeDetailViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeDetailViewModel.kt @@ -47,6 +47,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import org.matrix.android.sdk.api.query.RoomCategoryFilter import org.matrix.android.sdk.api.query.SpaceFilter +import org.matrix.android.sdk.api.query.toActiveSpaceOrOrphanRooms import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.crypto.NewSessionListener import org.matrix.android.sdk.api.session.initsync.SyncStatusService @@ -249,7 +250,7 @@ class HomeDetailViewModel @AssistedInject constructor( roomSummaryQueryParams { memberships = listOf(Membership.INVITE) roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS - spaceFilter = groupingMethod.spaceSummary?.roomId?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms + spaceFilter = groupingMethod.toActiveSpaceOrOrphanRooms() } ).size } @@ -266,7 +267,7 @@ class HomeDetailViewModel @AssistedInject constructor( roomSummaryQueryParams { memberships = listOf(Membership.JOIN) roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS - spaceFilter = groupingMethod.spaceSummary?.roomId?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms + spaceFilter = groupingMethod.toActiveSpaceOrOrphanRooms() } ) @@ -287,4 +288,8 @@ class HomeDetailViewModel @AssistedInject constructor( } .launchIn(viewModelScope) } + + private fun RoomGroupingMethod.BySpace.toActiveSpaceOrOrphanRooms(): SpaceFilter? { + return spaceSummary?.roomId?.toActiveSpaceOrOrphanRooms() + } } 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 c001b69121..08ecca8c6a 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 @@ -46,6 +46,7 @@ import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.query.RoomCategoryFilter import org.matrix.android.sdk.api.query.RoomTagQueryFilter import org.matrix.android.sdk.api.query.SpaceFilter +import org.matrix.android.sdk.api.query.toActiveSpaceOrOrphanRooms import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.getRoomSummary import org.matrix.android.sdk.api.session.room.RoomSummaryQueryParams @@ -370,7 +371,7 @@ class RoomListSectionBuilderSpace( activeSpaceUpdaters.add(object : RoomListViewModel.ActiveSpaceQueryUpdater { override fun updateForSpaceId(roomId: String?) { filteredPagedRoomSummariesLive.queryParams = roomQueryParams.copy( - spaceFilter = roomId?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms + spaceFilter = roomId?.toActiveSpaceOrOrphanRooms() ) liveQueryParams.update { filteredPagedRoomSummariesLive.queryParams } } @@ -436,7 +437,7 @@ class RoomListSectionBuilderSpace( return when (spaceFilter) { RoomListViewModel.SpaceFilterStrategy.ORPHANS_IF_SPACE_NULL -> { copy( - spaceFilter = currentSpace?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms + spaceFilter = currentSpace?.toActiveSpaceOrOrphanRooms() ) } RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL -> {