From e8476882fca97d8c470767025f91502272d5fb9a Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 28 Jul 2022 10:22:11 +0100 Subject: [PATCH] providing a type for the NoFilter to avoid the ambiguity of the nullable param --- .../org/matrix/android/sdk/api/query/SpaceFilter.kt | 10 ++++++++++ .../sdk/api/session/room/RoomSummaryQueryParams.kt | 4 ++-- .../session/room/summary/RoomSummaryDataSource.kt | 2 +- .../im/vector/app/features/home/HomeDetailViewModel.kt | 5 +++-- .../app/features/home/UnreadMessagesSharedViewModel.kt | 2 +- .../features/home/room/list/RoomListSectionBuilder.kt | 5 +++-- .../home/room/list/home/HomeRoomListViewModel.kt | 7 ++++--- .../vector/app/features/spaces/SpaceListViewModel.kt | 4 ++-- 8 files changed, 26 insertions(+), 13 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 6383412ffb..ccefd5855f 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 @@ -35,9 +35,19 @@ sealed interface 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 + + /** + * Used to apply no filtering to the space. + */ + object NoFilter : 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 + +/** + * Return a [SpaceFilter.ActiveSpace] if the String is not null, or [SpaceFilter.NoFilter]. + */ +fun String?.toActiveSpaceOrNoFilter(): SpaceFilter = this?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.NoFilter 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 00c6da00b7..60963ef25a 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 @@ -86,7 +86,7 @@ data class RoomSummaryQueryParams( /** * Used to filter room using the current space. */ - val spaceFilter: SpaceFilter?, + val spaceFilter: SpaceFilter, ) { /** @@ -101,7 +101,7 @@ data class RoomSummaryQueryParams( var roomTagQueryFilter: RoomTagQueryFilter? = null var excludeType: List? = listOf(RoomType.SPACE) var includeType: List? = null - var spaceFilter: SpaceFilter? = null + var spaceFilter: SpaceFilter = SpaceFilter.NoFilter fun build() = RoomSummaryQueryParams( displayName = displayName, 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 9d14ebffdd..82fc94df7c 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 @@ -317,7 +317,7 @@ internal class RoomSummaryDataSource @Inject constructor( is SpaceFilter.ExcludeSpace -> { query.not().contains(RoomSummaryEntityFields.FLATTEN_PARENT_IDS, queryParams.spaceFilter.spaceId) } - null -> Unit // nop + SpaceFilter.NoFilter -> Unit // nop } return query 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 787458993f..6471eb5141 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 @@ -46,6 +46,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.toActiveSpaceOrNoFilter 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 @@ -235,7 +236,7 @@ class HomeDetailViewModel @AssistedInject constructor( roomSummaryQueryParams { memberships = listOf(Membership.INVITE) roomCategoryFilter = RoomCategoryFilter.ONLY_DM - spaceFilter = activeSpaceRoomId?.let { SpaceFilter.ActiveSpace(it) } + spaceFilter = activeSpaceRoomId.toActiveSpaceOrNoFilter() } ).size @@ -252,7 +253,7 @@ class HomeDetailViewModel @AssistedInject constructor( roomSummaryQueryParams { memberships = listOf(Membership.JOIN) roomCategoryFilter = RoomCategoryFilter.ONLY_DM - spaceFilter = activeSpaceRoomId?.let { SpaceFilter.ActiveSpace(it) } + spaceFilter = activeSpaceRoomId.toActiveSpaceOrNoFilter() } ) 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 f2e6b7e064..3ce425ba6f 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 @@ -144,7 +144,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor( this.memberships = listOf(Membership.JOIN) this.spaceFilter = SpaceFilter.OrphanRooms.takeIf { !vectorPreferences.prefSpacesShowAllRoomInHome() - } + } ?: SpaceFilter.NoFilter } ) 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 46591a0ca6..b2187fb365 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 @@ -45,6 +45,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.toActiveSpaceOrNoFilter import org.matrix.android.sdk.api.query.toActiveSpaceOrOrphanRooms import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.getRoomSummary @@ -393,7 +394,7 @@ class RoomListSectionBuilder( ) } else { filteredPagedRoomSummariesLive.queryParams = roomQueryParams.copy( - spaceFilter = null + spaceFilter = SpaceFilter.NoFilter ) } liveQueryParams.update { filteredPagedRoomSummariesLive.queryParams } @@ -449,7 +450,7 @@ class RoomListSectionBuilder( } RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL -> { copy( - spaceFilter = currentSpace?.let { SpaceFilter.ActiveSpace(it) } + spaceFilter = currentSpace.toActiveSpaceOrNoFilter() ) } RoomListViewModel.SpaceFilterStrategy.NONE -> this diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt index 55d955cb37..d74d235dc2 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt @@ -39,6 +39,7 @@ import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.launch import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.query.SpaceFilter +import org.matrix.android.sdk.api.query.toActiveSpaceOrNoFilter import org.matrix.android.sdk.api.query.toActiveSpaceOrOrphanRooms import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.getRoom @@ -121,15 +122,15 @@ class HomeRoomListViewModel @AssistedInject constructor( ) } - private fun getSpaceFilter(selectedSpaceId: String?, strategy: RoomListViewModel.SpaceFilterStrategy): SpaceFilter? { + private fun getSpaceFilter(selectedSpaceId: String?, strategy: RoomListViewModel.SpaceFilterStrategy): SpaceFilter { return when (strategy) { RoomListViewModel.SpaceFilterStrategy.ORPHANS_IF_SPACE_NULL -> { selectedSpaceId.toActiveSpaceOrOrphanRooms() } RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL -> { - selectedSpaceId?.let { SpaceFilter.ActiveSpace(it) } + selectedSpaceId.toActiveSpaceOrNoFilter() } - RoomListViewModel.SpaceFilterStrategy.NONE -> null + RoomListViewModel.SpaceFilterStrategy.NONE -> SpaceFilter.NoFilter } } 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 3b2fb31b74..ca6bd7a993 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 @@ -100,7 +100,7 @@ class SpaceListViewModel @AssistedInject constructor( this.memberships = listOf(Membership.JOIN) this.spaceFilter = SpaceFilter.OrphanRooms.takeIf { !vectorPreferences.prefSpacesShowAllRoomInHome() - } + } ?: SpaceFilter.NoFilter }, sortOrder = RoomSortOrder.NONE ).asFlow() .sample(300) @@ -117,7 +117,7 @@ class SpaceListViewModel @AssistedInject constructor( this.memberships = listOf(Membership.JOIN) this.spaceFilter = SpaceFilter.OrphanRooms.takeIf { !vectorPreferences.prefSpacesShowAllRoomInHome() - } + } ?: SpaceFilter.NoFilter } ) val counts = RoomAggregateNotificationCount(