From 0b6f35b2565f41fb8246aab36e53c808a3764f32 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 24 May 2022 17:09:21 +0200 Subject: [PATCH] Create SpaceFilter.OrphanRooms to improve the API. Not 100% of the side effect. There is probably some (fixed?) bugs here. --- .../android/sdk/api/query/SpaceFilter.kt | 20 +++++++++++++++---- .../session/room/RoomSummaryQueryParams.kt | 4 ++-- .../room/summary/RoomSummaryDataSource.kt | 15 ++++++-------- .../app/features/home/HomeDetailViewModel.kt | 4 ++-- .../home/UnreadMessagesSharedViewModel.kt | 9 ++++----- .../room/list/RoomListSectionBuilderSpace.kt | 4 ++-- .../app/features/spaces/SpaceListViewModel.kt | 4 ++-- 7 files changed, 34 insertions(+), 26 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 2e59d03665..237f9388f2 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 @@ -17,10 +17,22 @@ package org.matrix.android.sdk.api.query /** - * Filter to be used to do room queries. + * Filter to be used to do room queries regarding the space hierarchy. * @see [org.matrix.android.sdk.api.session.room.RoomSummaryQueryParams] */ -sealed class SpaceFilter { - data class ActiveSpace(val currentSpaceId: String?) : SpaceFilter() - data class ExcludeSpace(val spaceId: String) : SpaceFilter() +sealed interface SpaceFilter { + /** + * Used to get all the rooms that are not in any space. + */ + object OrphanRooms : SpaceFilter + + /** + * Used to get all the rooms that have the provided space in their parent hierarchy. + */ + data class ActiveSpace(val spaceId: String) : SpaceFilter + + /** + * Used to get all the rooms that do not have the provided space in their parent hierarchy. + */ + data class ExcludeSpace(val spaceId: String) : SpaceFilter } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomSummaryQueryParams.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomSummaryQueryParams.kt index 7682e6c348..8afcfa9ac6 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomSummaryQueryParams.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomSummaryQueryParams.kt @@ -25,7 +25,7 @@ import org.matrix.android.sdk.api.session.room.model.RoomType import org.matrix.android.sdk.api.session.space.SpaceSummaryQueryParams /** - * Create a [RoomSummaryQueryParams] object, calling [init] with a [RoomSummaryQueryParams.Builder] + * Create a [RoomSummaryQueryParams] object, calling [init] with a [RoomSummaryQueryParams.Builder]. */ fun roomSummaryQueryParams(init: (RoomSummaryQueryParams.Builder.() -> Unit) = {}): RoomSummaryQueryParams { return RoomSummaryQueryParams.Builder() @@ -34,7 +34,7 @@ fun roomSummaryQueryParams(init: (RoomSummaryQueryParams.Builder.() -> Unit) = { } /** - * Create a [SpaceSummaryQueryParams] object (which is a [RoomSummaryQueryParams]), calling [init] with a [RoomSummaryQueryParams.Builder] + * Create a [SpaceSummaryQueryParams] object (which is a [RoomSummaryQueryParams]), calling [init] with a [RoomSummaryQueryParams.Builder]. * This is specific for spaces, other filters will be applied after invoking [init] */ fun spaceSummaryQueryParams(init: (RoomSummaryQueryParams.Builder.() -> Unit) = {}): SpaceSummaryQueryParams { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt index 81bfb7af0f..4b453908e7 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt @@ -301,22 +301,19 @@ internal class RoomSummaryDataSource @Inject constructor( // Timber.w("VAL: activeSpaceId : ${queryParams.activeSpaceId}") when (queryParams.spaceFilter) { + SpaceFilter.OrphanRooms -> { + // orphan rooms + query.isNull(RoomSummaryEntityFields.FLATTEN_PARENT_IDS) + } is SpaceFilter.ActiveSpace -> { // It's annoying but for now realm java does not support querying in primitive list :/ // https://github.com/realm/realm-java/issues/5361 - if (queryParams.spaceFilter.currentSpaceId == null) { - // orphan rooms - query.isNull(RoomSummaryEntityFields.FLATTEN_PARENT_IDS) - } else { - query.contains(RoomSummaryEntityFields.FLATTEN_PARENT_IDS, queryParams.spaceFilter.currentSpaceId) - } + query.contains(RoomSummaryEntityFields.FLATTEN_PARENT_IDS, queryParams.spaceFilter.spaceId) } is SpaceFilter.ExcludeSpace -> { query.not().contains(RoomSummaryEntityFields.FLATTEN_PARENT_IDS, queryParams.spaceFilter.spaceId) } - else -> { - // nop - } + null -> Unit // nop } queryParams.activeGroupId?.let { activeGroupId -> 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 8015504810..5044e71a50 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 @@ -249,7 +249,7 @@ class HomeDetailViewModel @AssistedInject constructor( roomSummaryQueryParams { memberships = listOf(Membership.INVITE) roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS - spaceFilter = SpaceFilter.ActiveSpace(groupingMethod.spaceSummary?.roomId) + spaceFilter = groupingMethod.spaceSummary?.roomId?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms } ).size } @@ -266,7 +266,7 @@ class HomeDetailViewModel @AssistedInject constructor( roomSummaryQueryParams { memberships = listOf(Membership.JOIN) roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS - spaceFilter = SpaceFilter.ActiveSpace(groupingMethod.spaceSummary?.roomId) + spaceFilter = groupingMethod.spaceSummary?.roomId?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms } ) diff --git a/vector/src/main/java/im/vector/app/features/home/UnreadMessagesSharedViewModel.kt b/vector/src/main/java/im/vector/app/features/home/UnreadMessagesSharedViewModel.kt index a6f5c2f718..09e2d11f84 100644 --- a/vector/src/main/java/im/vector/app/features/home/UnreadMessagesSharedViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/UnreadMessagesSharedViewModel.kt @@ -74,11 +74,10 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia private val roomService = session.roomService() init { - roomService.getPagedRoomSummariesLive( roomSummaryQueryParams { this.memberships = listOf(Membership.JOIN) - this.spaceFilter = SpaceFilter.ActiveSpace(null) + this.spaceFilter = SpaceFilter.OrphanRooms }, sortOrder = RoomSortOrder.NONE ).asFlow() .throttleFirst(300) @@ -86,7 +85,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia val counts = roomService.getNotificationCountForRooms( roomSummaryQueryParams { this.memberships = listOf(Membership.JOIN) - this.spaceFilter = SpaceFilter.ActiveSpace(null) + this.spaceFilter = SpaceFilter.OrphanRooms } ) val invites = if (autoAcceptInvites.hideInvites) { @@ -95,7 +94,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia roomService.getRoomSummaries( roomSummaryQueryParams { this.memberships = listOf(Membership.INVITE) - this.spaceFilter = SpaceFilter.ActiveSpace(null) + this.spaceFilter = SpaceFilter.OrphanRooms } ).size } @@ -151,7 +150,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia val totalCount = roomService.getNotificationCountForRooms( roomSummaryQueryParams { this.memberships = listOf(Membership.JOIN) - this.spaceFilter = SpaceFilter.ActiveSpace(null).takeIf { + this.spaceFilter = SpaceFilter.OrphanRooms.takeIf { !vectorPreferences.prefSpacesShowAllRoomInHome() } } 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 75c5867693..c001b69121 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 @@ -370,7 +370,7 @@ class RoomListSectionBuilderSpace( activeSpaceUpdaters.add(object : RoomListViewModel.ActiveSpaceQueryUpdater { override fun updateForSpaceId(roomId: String?) { filteredPagedRoomSummariesLive.queryParams = roomQueryParams.copy( - spaceFilter = SpaceFilter.ActiveSpace(roomId) + spaceFilter = roomId?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms ) liveQueryParams.update { filteredPagedRoomSummariesLive.queryParams } } @@ -436,7 +436,7 @@ class RoomListSectionBuilderSpace( return when (spaceFilter) { RoomListViewModel.SpaceFilterStrategy.ORPHANS_IF_SPACE_NULL -> { copy( - spaceFilter = SpaceFilter.ActiveSpace(currentSpace) + spaceFilter = currentSpace?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms ) } RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL -> { diff --git a/vector/src/main/java/im/vector/app/features/spaces/SpaceListViewModel.kt b/vector/src/main/java/im/vector/app/features/spaces/SpaceListViewModel.kt index 3daa2e0adb..086ab08c3b 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/SpaceListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/SpaceListViewModel.kt @@ -110,7 +110,7 @@ class SpaceListViewModel @AssistedInject constructor(@Assisted initialState: Spa session.roomService().getPagedRoomSummariesLive( roomSummaryQueryParams { this.memberships = listOf(Membership.JOIN) - this.spaceFilter = SpaceFilter.ActiveSpace(null).takeIf { + this.spaceFilter = SpaceFilter.OrphanRooms.takeIf { !vectorPreferences.prefSpacesShowAllRoomInHome() } }, sortOrder = RoomSortOrder.NONE @@ -127,7 +127,7 @@ class SpaceListViewModel @AssistedInject constructor(@Assisted initialState: Spa val totalCount = session.roomService().getNotificationCountForRooms( roomSummaryQueryParams { this.memberships = listOf(Membership.JOIN) - this.spaceFilter = SpaceFilter.ActiveSpace(null).takeIf { + this.spaceFilter = SpaceFilter.OrphanRooms.takeIf { !vectorPreferences.prefSpacesShowAllRoomInHome() } }