Introduce awaitRoom() and awaitRoomSummary()

This commit is contained in:
ganfra 2022-07-06 18:11:06 +02:00
parent d957e24747
commit ebd491c6f0
6 changed files with 37 additions and 1 deletions

View File

@ -65,6 +65,11 @@ interface Room {
*/
fun roomSummary(): RoomSummary?
/**
* Suspending version of [roomSummary] method
*/
suspend fun awaitRoomSummary(): RoomSummary?
/**
* Use this room as a Space, if the type is correct.
*/

View File

@ -92,6 +92,13 @@ interface RoomService {
*/
fun getRoom(roomId: String): Room?
/**
* Suspending version of [getRoom] method.
* @param roomId the roomId to look for.
* @return a room with roomId or null
*/
suspend fun awaitRoom(roomId: String): Room?
/**
* Get a roomSummary from a roomId or a room alias.
* @param roomIdOrAlias the roomId or the alias of a room to look for.

View File

@ -82,6 +82,10 @@ internal class DefaultRoom(
return roomSummaryDataSource.getRoomSummary(roomId)
}
override suspend fun awaitRoomSummary(): RoomSummary? {
return roomSummaryDataSource.awaitRoomSummary(roomId)
}
override fun asSpace(): Space? {
if (roomSummary()?.roomType != RoomType.SPACE) return null
return DefaultSpace(this, roomSummaryDataSource, viaParameterFinder)

View File

@ -82,6 +82,10 @@ internal class DefaultRoomService @Inject constructor(
return roomGetter.getRoom(roomId)
}
override suspend fun awaitRoom(roomId: String): Room? {
return roomGetter.awaitRoom(roomId)
}
override fun getExistingDirectRoomWithUser(otherUserId: String): String? {
return roomGetter.getDirectRoomWith(otherUserId)
}

View File

@ -17,6 +17,8 @@
package org.matrix.android.sdk.internal.session.room
import io.realm.Realm
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.internal.database.RealmSessionProvider
@ -30,13 +32,16 @@ import javax.inject.Inject
internal interface RoomGetter {
fun getRoom(roomId: String): Room?
suspend fun awaitRoom(roomId: String): Room?
fun getDirectRoomWith(otherUserId: String): String?
}
@SessionScope
internal class DefaultRoomGetter @Inject constructor(
private val realmSessionProvider: RealmSessionProvider,
private val roomFactory: RoomFactory
private val roomFactory: RoomFactory,
private val coroutineDispatchers: MatrixCoroutineDispatchers,
) : RoomGetter {
override fun getRoom(roomId: String): Room? {
@ -45,6 +50,10 @@ internal class DefaultRoomGetter @Inject constructor(
}
}
override suspend fun awaitRoom(roomId: String) = withContext(coroutineDispatchers.io) {
getRoom(roomId)
}
override fun getDirectRoomWith(otherUserId: String): String? {
return realmSessionProvider.withRealm { realm ->
RoomSummaryEntity.where(realm)

View File

@ -26,6 +26,8 @@ import com.zhuinden.monarchy.Monarchy
import io.realm.Realm
import io.realm.RealmQuery
import io.realm.kotlin.where
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.query.RoomCategoryFilter
import org.matrix.android.sdk.api.query.SpaceFilter
@ -58,6 +60,7 @@ internal class RoomSummaryDataSource @Inject constructor(
@SessionDatabase private val monarchy: Monarchy,
private val roomSummaryMapper: RoomSummaryMapper,
private val queryStringValueProcessor: QueryStringValueProcessor,
private val coroutineDispatchers: MatrixCoroutineDispatchers,
) {
fun getRoomSummary(roomIdOrAlias: String): RoomSummary? {
@ -75,6 +78,10 @@ internal class RoomSummaryDataSource @Inject constructor(
})
}
suspend fun awaitRoomSummary(roomIdOrAlias: String) = withContext(coroutineDispatchers.io) {
getRoomSummary(roomIdOrAlias)
}
fun getRoomSummaryLive(roomId: String): LiveData<Optional<RoomSummary>> {
val liveData = monarchy.findAllMappedWithChanges(
{ realm -> RoomSummaryEntity.where(realm, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) },