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
/**
* 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
}

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
/**
* 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 {

View File

@ -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 ->

View File

@ -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
}
)

View File

@ -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()
}
}

View File

@ -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 -> {

View File

@ -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()
}
}