Create SpaceFilter.OrphanRooms to improve the API.

Not 100% of the side effect. There is probably some (fixed?) bugs here.
This commit is contained in:
Benoit Marty 2022-05-24 17:09:21 +02:00 committed by Benoit Marty
parent c7997edf9a
commit 0b6f35b256
7 changed files with 34 additions and 26 deletions

View File

@ -17,10 +17,22 @@
package org.matrix.android.sdk.api.query 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] * @see [org.matrix.android.sdk.api.session.room.RoomSummaryQueryParams]
*/ */
sealed class SpaceFilter { sealed interface SpaceFilter {
data class ActiveSpace(val currentSpaceId: String?) : SpaceFilter() /**
data class ExcludeSpace(val spaceId: String) : 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
} }

View File

@ -25,7 +25,7 @@ import org.matrix.android.sdk.api.session.room.model.RoomType
import org.matrix.android.sdk.api.session.space.SpaceSummaryQueryParams 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 { fun roomSummaryQueryParams(init: (RoomSummaryQueryParams.Builder.() -> Unit) = {}): RoomSummaryQueryParams {
return RoomSummaryQueryParams.Builder() 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] * This is specific for spaces, other filters will be applied after invoking [init]
*/ */
fun spaceSummaryQueryParams(init: (RoomSummaryQueryParams.Builder.() -> Unit) = {}): SpaceSummaryQueryParams { fun spaceSummaryQueryParams(init: (RoomSummaryQueryParams.Builder.() -> Unit) = {}): SpaceSummaryQueryParams {

View File

@ -301,22 +301,19 @@ internal class RoomSummaryDataSource @Inject constructor(
// Timber.w("VAL: activeSpaceId : ${queryParams.activeSpaceId}") // Timber.w("VAL: activeSpaceId : ${queryParams.activeSpaceId}")
when (queryParams.spaceFilter) { when (queryParams.spaceFilter) {
SpaceFilter.OrphanRooms -> {
// orphan rooms
query.isNull(RoomSummaryEntityFields.FLATTEN_PARENT_IDS)
}
is SpaceFilter.ActiveSpace -> { is SpaceFilter.ActiveSpace -> {
// It's annoying but for now realm java does not support querying in primitive list :/ // It's annoying but for now realm java does not support querying in primitive list :/
// https://github.com/realm/realm-java/issues/5361 // https://github.com/realm/realm-java/issues/5361
if (queryParams.spaceFilter.currentSpaceId == null) { query.contains(RoomSummaryEntityFields.FLATTEN_PARENT_IDS, queryParams.spaceFilter.spaceId)
// orphan rooms
query.isNull(RoomSummaryEntityFields.FLATTEN_PARENT_IDS)
} else {
query.contains(RoomSummaryEntityFields.FLATTEN_PARENT_IDS, queryParams.spaceFilter.currentSpaceId)
}
} }
is SpaceFilter.ExcludeSpace -> { is SpaceFilter.ExcludeSpace -> {
query.not().contains(RoomSummaryEntityFields.FLATTEN_PARENT_IDS, queryParams.spaceFilter.spaceId) query.not().contains(RoomSummaryEntityFields.FLATTEN_PARENT_IDS, queryParams.spaceFilter.spaceId)
} }
else -> { null -> Unit // nop
// nop
}
} }
queryParams.activeGroupId?.let { activeGroupId -> queryParams.activeGroupId?.let { activeGroupId ->

View File

@ -249,7 +249,7 @@ class HomeDetailViewModel @AssistedInject constructor(
roomSummaryQueryParams { roomSummaryQueryParams {
memberships = listOf(Membership.INVITE) memberships = listOf(Membership.INVITE)
roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS
spaceFilter = SpaceFilter.ActiveSpace(groupingMethod.spaceSummary?.roomId) spaceFilter = groupingMethod.spaceSummary?.roomId?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms
} }
).size ).size
} }
@ -266,7 +266,7 @@ class HomeDetailViewModel @AssistedInject constructor(
roomSummaryQueryParams { roomSummaryQueryParams {
memberships = listOf(Membership.JOIN) memberships = listOf(Membership.JOIN)
roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS
spaceFilter = SpaceFilter.ActiveSpace(groupingMethod.spaceSummary?.roomId) spaceFilter = groupingMethod.spaceSummary?.roomId?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms
} }
) )

View File

@ -74,11 +74,10 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia
private val roomService = session.roomService() private val roomService = session.roomService()
init { init {
roomService.getPagedRoomSummariesLive( roomService.getPagedRoomSummariesLive(
roomSummaryQueryParams { roomSummaryQueryParams {
this.memberships = listOf(Membership.JOIN) this.memberships = listOf(Membership.JOIN)
this.spaceFilter = SpaceFilter.ActiveSpace(null) this.spaceFilter = SpaceFilter.OrphanRooms
}, sortOrder = RoomSortOrder.NONE }, sortOrder = RoomSortOrder.NONE
).asFlow() ).asFlow()
.throttleFirst(300) .throttleFirst(300)
@ -86,7 +85,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia
val counts = roomService.getNotificationCountForRooms( val counts = roomService.getNotificationCountForRooms(
roomSummaryQueryParams { roomSummaryQueryParams {
this.memberships = listOf(Membership.JOIN) this.memberships = listOf(Membership.JOIN)
this.spaceFilter = SpaceFilter.ActiveSpace(null) this.spaceFilter = SpaceFilter.OrphanRooms
} }
) )
val invites = if (autoAcceptInvites.hideInvites) { val invites = if (autoAcceptInvites.hideInvites) {
@ -95,7 +94,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia
roomService.getRoomSummaries( roomService.getRoomSummaries(
roomSummaryQueryParams { roomSummaryQueryParams {
this.memberships = listOf(Membership.INVITE) this.memberships = listOf(Membership.INVITE)
this.spaceFilter = SpaceFilter.ActiveSpace(null) this.spaceFilter = SpaceFilter.OrphanRooms
} }
).size ).size
} }
@ -151,7 +150,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia
val totalCount = roomService.getNotificationCountForRooms( val totalCount = roomService.getNotificationCountForRooms(
roomSummaryQueryParams { roomSummaryQueryParams {
this.memberships = listOf(Membership.JOIN) this.memberships = listOf(Membership.JOIN)
this.spaceFilter = SpaceFilter.ActiveSpace(null).takeIf { this.spaceFilter = SpaceFilter.OrphanRooms.takeIf {
!vectorPreferences.prefSpacesShowAllRoomInHome() !vectorPreferences.prefSpacesShowAllRoomInHome()
} }
} }

View File

@ -370,7 +370,7 @@ class RoomListSectionBuilderSpace(
activeSpaceUpdaters.add(object : RoomListViewModel.ActiveSpaceQueryUpdater { activeSpaceUpdaters.add(object : RoomListViewModel.ActiveSpaceQueryUpdater {
override fun updateForSpaceId(roomId: String?) { override fun updateForSpaceId(roomId: String?) {
filteredPagedRoomSummariesLive.queryParams = roomQueryParams.copy( filteredPagedRoomSummariesLive.queryParams = roomQueryParams.copy(
spaceFilter = SpaceFilter.ActiveSpace(roomId) spaceFilter = roomId?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms
) )
liveQueryParams.update { filteredPagedRoomSummariesLive.queryParams } liveQueryParams.update { filteredPagedRoomSummariesLive.queryParams }
} }
@ -436,7 +436,7 @@ class RoomListSectionBuilderSpace(
return when (spaceFilter) { return when (spaceFilter) {
RoomListViewModel.SpaceFilterStrategy.ORPHANS_IF_SPACE_NULL -> { RoomListViewModel.SpaceFilterStrategy.ORPHANS_IF_SPACE_NULL -> {
copy( copy(
spaceFilter = SpaceFilter.ActiveSpace(currentSpace) spaceFilter = currentSpace?.let { SpaceFilter.ActiveSpace(it) } ?: SpaceFilter.OrphanRooms
) )
} }
RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL -> { RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL -> {

View File

@ -110,7 +110,7 @@ class SpaceListViewModel @AssistedInject constructor(@Assisted initialState: Spa
session.roomService().getPagedRoomSummariesLive( session.roomService().getPagedRoomSummariesLive(
roomSummaryQueryParams { roomSummaryQueryParams {
this.memberships = listOf(Membership.JOIN) this.memberships = listOf(Membership.JOIN)
this.spaceFilter = SpaceFilter.ActiveSpace(null).takeIf { this.spaceFilter = SpaceFilter.OrphanRooms.takeIf {
!vectorPreferences.prefSpacesShowAllRoomInHome() !vectorPreferences.prefSpacesShowAllRoomInHome()
} }
}, sortOrder = RoomSortOrder.NONE }, sortOrder = RoomSortOrder.NONE
@ -127,7 +127,7 @@ class SpaceListViewModel @AssistedInject constructor(@Assisted initialState: Spa
val totalCount = session.roomService().getNotificationCountForRooms( val totalCount = session.roomService().getNotificationCountForRooms(
roomSummaryQueryParams { roomSummaryQueryParams {
this.memberships = listOf(Membership.JOIN) this.memberships = listOf(Membership.JOIN)
this.spaceFilter = SpaceFilter.ActiveSpace(null).takeIf { this.spaceFilter = SpaceFilter.OrphanRooms.takeIf {
!vectorPreferences.prefSpacesShowAllRoomInHome() !vectorPreferences.prefSpacesShowAllRoomInHome()
} }
} }