Remove roomId from RoomSummaryQueryParams.Builder().

Create a new API in RoomService to observe a room summary from a roomId.
This commit is contained in:
Benoit Marty 2022-05-24 12:26:29 +02:00 committed by Benoit Marty
parent 49a29fb56f
commit bfdc885d7f
6 changed files with 45 additions and 28 deletions

View File

@ -45,6 +45,13 @@ import org.matrix.android.sdk.api.util.toOptional
class FlowSession(private val session: Session) {
fun liveRoomSummary(roomId: String): Flow<Optional<RoomSummary>> {
return session.roomService().getRoomSummaryLive(roomId).asFlow()
.startWith(session.coroutineDispatchers.io) {
session.roomService().getRoomSummary(roomId).toOptional()
}
}
fun liveRoomSummaries(queryParams: RoomSummaryQueryParams, sortOrder: RoomSortOrder = RoomSortOrder.NONE): Flow<List<RoomSummary>> {
return session.roomService().getRoomSummariesLive(queryParams, sortOrder).asFlow()
.startWith(session.coroutineDispatchers.io) {

View File

@ -97,6 +97,12 @@ interface RoomService {
*/
fun getRoomSummary(roomIdOrAlias: String): RoomSummary?
/**
* A live [RoomSummary] associated with the room with id [roomId].
* You can observe this summary to get dynamic data from this room, even if the room is not joined yet
*/
fun getRoomSummaryLive(roomId: String): LiveData<Optional<RoomSummary>>
/**
* Get a snapshot list of room summaries.
* @return the immutable list of [RoomSummary]

View File

@ -51,7 +51,6 @@ fun spaceSummaryQueryParams(init: (RoomSummaryQueryParams.Builder.() -> Unit) =
* [org.matrix.android.sdk.api.session.room.Room] and [org.matrix.android.sdk.api.session.room.RoomService].
*/
data class RoomSummaryQueryParams(
val roomId: QueryStringValue,
val displayName: QueryStringValue,
val canonicalAlias: QueryStringValue,
val memberships: List<Membership>,
@ -64,7 +63,6 @@ data class RoomSummaryQueryParams(
) {
class Builder {
var roomId: QueryStringValue = QueryStringValue.IsNotEmpty
var displayName: QueryStringValue = QueryStringValue.IsNotEmpty
var canonicalAlias: QueryStringValue = QueryStringValue.NoCondition
var memberships: List<Membership> = Membership.all()
@ -76,7 +74,6 @@ data class RoomSummaryQueryParams(
var activeGroupId: String? = null
fun build() = RoomSummaryQueryParams(
roomId = roomId,
displayName = displayName,
canonicalAlias = canonicalAlias,
memberships = memberships,

View File

@ -20,7 +20,6 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import androidx.paging.PagedList
import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.identity.model.SignInvitationResult
import org.matrix.android.sdk.api.session.room.Room
@ -91,18 +90,25 @@ internal class DefaultRoomService @Inject constructor(
return roomSummaryDataSource.getRoomSummary(roomIdOrAlias)
}
override fun getRoomSummaryLive(roomId: String): LiveData<Optional<RoomSummary>> {
return roomSummaryDataSource.getRoomSummaryLive(roomId)
}
override fun getRoomSummaries(queryParams: RoomSummaryQueryParams,
sortOrder: RoomSortOrder): List<RoomSummary> {
return roomSummaryDataSource.getRoomSummaries(queryParams, sortOrder)
}
override fun refreshJoinedRoomSummaryPreviews(roomId: String?) {
val roomSummaries = getRoomSummaries(roomSummaryQueryParams {
if (roomId != null) {
this.roomId = QueryStringValue.Equals(roomId)
}
memberships = listOf(Membership.JOIN)
})
val roomSummaries = if (roomId == null) {
getRoomSummaries(roomSummaryQueryParams {
memberships = listOf(Membership.JOIN)
})
} else {
listOfNotNull(
getRoomSummary(roomId)?.takeIf { it.membership == Membership.JOIN }
)
}
if (roomSummaries.isNotEmpty()) {
monarchy.runTransactionSync { realm ->

View File

@ -27,6 +27,7 @@ import io.realm.Realm
import io.realm.RealmQuery
import io.realm.kotlin.where
import org.matrix.android.sdk.api.query.ActiveSpaceFilter
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.query.RoomCategoryFilter
import org.matrix.android.sdk.api.query.isNormalized
import org.matrix.android.sdk.api.session.room.ResultBoundaries
@ -266,7 +267,7 @@ internal class RoomSummaryDataSource @Inject constructor(
private fun roomSummariesQuery(realm: Realm, queryParams: RoomSummaryQueryParams): RealmQuery<RoomSummaryEntity> {
val query = with(queryStringValueProcessor) {
RoomSummaryEntity.where(realm)
.process(RoomSummaryEntityFields.ROOM_ID, queryParams.roomId)
.process(RoomSummaryEntityFields.ROOM_ID, QueryStringValue.IsNotEmpty)
.let {
if (queryParams.displayName.isNormalized()) {
it.process(RoomSummaryEntityFields.NORMALIZED_DISPLAY_NAME, queryParams.displayName)

View File

@ -35,7 +35,6 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.getRoomSummary
import org.matrix.android.sdk.api.session.identity.SharedState
@ -43,8 +42,8 @@ import org.matrix.android.sdk.api.session.identity.ThreePid
import org.matrix.android.sdk.api.session.room.members.ChangeMembershipState
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.peeking.PeekResult
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
import org.matrix.android.sdk.flow.flow
import org.matrix.android.sdk.flow.unwrap
import timber.log.Timber
class RoomPreviewViewModel @AssistedInject constructor(
@ -160,24 +159,25 @@ class RoomPreviewViewModel @AssistedInject constructor(
}
private fun observeRoomSummary() {
val queryParams = roomSummaryQueryParams {
roomId = QueryStringValue.Equals(initialState.roomId)
excludeType = null
}
session
.flow()
.liveRoomSummaries(queryParams)
.onEach { list ->
val isRoomJoined = list.any {
it.membership == Membership.JOIN
}
list.firstOrNull { it.roomId == initialState.roomId }?.roomType?.let {
setState {
copy(roomType = it)
}
}
.liveRoomSummary(initialState.roomId)
.unwrap()
.onEach { roomSummary ->
val isRoomJoined = roomSummary.membership == Membership.JOIN
if (isRoomJoined) {
setState { copy(roomJoinState = JoinState.JOINED) }
setState {
copy(
roomType = roomSummary.roomType,
roomJoinState = JoinState.JOINED
)
}
} else {
setState {
copy(
roomType = roomSummary.roomType
)
}
}
}
.launchIn(viewModelScope)