diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt index 5d2769ac3c..4e94e8835d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt @@ -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. */ diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomService.kt index 5dfb8961e3..5f69708f08 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomService.kt @@ -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. diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoom.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoom.kt index abea2d34cd..134f9c4fd6 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoom.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoom.kt @@ -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) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomService.kt index 5e6d052443..16ab103260 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomService.kt @@ -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) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomGetter.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomGetter.kt index e3f4732cc1..4b3680d694 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomGetter.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomGetter.kt @@ -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) 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 cb7dc270e8..d434a6f3d2 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 @@ -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> { val liveData = monarchy.findAllMappedWithChanges( { realm -> RoomSummaryEntity.where(realm, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) },