diff --git a/changelog.d/1563.misc b/changelog.d/1563.misc new file mode 100644 index 0000000000..8de1c9b6d1 --- /dev/null +++ b/changelog.d/1563.misc @@ -0,0 +1 @@ +Remove usage of blocking methods. diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt index f557a0fc0f..14cf3cd17e 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt @@ -98,8 +98,8 @@ class RustMatrixClient constructor( private val innerRoomListService = syncService.roomListService() private val sessionDispatcher = dispatchers.io.limitedParallelism(64) private val sessionCoroutineScope = appCoroutineScope.childScope(dispatchers.main, "Session-${sessionId}") - private val rustSyncService = RustSyncService(syncService, dispatchers, sessionCoroutineScope) - private val verificationService = RustSessionVerificationService(rustSyncService, dispatchers) + private val rustSyncService = RustSyncService(syncService, sessionCoroutineScope) + private val verificationService = RustSessionVerificationService(rustSyncService) private val pushersService = RustPushersService( client = client, dispatchers = dispatchers, @@ -208,6 +208,7 @@ class RustMatrixClient constructor( private fun pairOfRoom(roomId: RoomId): Pair? { val cachedRoomListItem = innerRoomListService.roomOrNull(roomId.value) + // Keep using fullRoomBlocking for now as it's faster. val fullRoom = cachedRoomListItem?.fullRoomBlocking() return if (cachedRoomListItem == null || fullRoom == null) { Timber.d("No room cached for $roomId") diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt index bddf904533..b37266342e 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt @@ -53,7 +53,8 @@ class RustMatrixClientFactory @Inject constructor( client.restoreSession(sessionData.toSession()) - val syncService = client.syncService().finishBlocking() + val syncService = client.syncService() + .finish() RustMatrixClient( client = client, diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsService.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsService.kt index 618c12cc03..a2fffdbdfb 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsService.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notificationsettings/RustNotificationSettingsService.kt @@ -47,19 +47,15 @@ class RustNotificationSettingsService( notificationSettings.setDelegate(notificationSettingsDelegate) } - override suspend fun getRoomNotificationSettings(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean): Result = withContext( - dispatchers.io - ) { + override suspend fun getRoomNotificationSettings(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean): Result = runCatching { - notificationSettings.getRoomNotificationSettingsBlocking(roomId.value, isEncrypted, isOneToOne).let(RoomNotificationSettingsMapper::map) + notificationSettings.getRoomNotificationSettings(roomId.value, isEncrypted, isOneToOne).let(RoomNotificationSettingsMapper::map) } - } - override suspend fun getDefaultRoomNotificationMode(isEncrypted: Boolean, isOneToOne: Boolean): Result = withContext(dispatchers.io) { + override suspend fun getDefaultRoomNotificationMode(isEncrypted: Boolean, isOneToOne: Boolean): Result = runCatching { - notificationSettings.getDefaultRoomNotificationModeBlocking(isEncrypted, isOneToOne).let(RoomNotificationSettingsMapper::mapMode) + notificationSettings.getDefaultRoomNotificationMode(isEncrypted, isOneToOne).let(RoomNotificationSettingsMapper::mapMode) } - } override suspend fun setDefaultRoomNotificationMode( isEncrypted: Boolean, @@ -67,19 +63,19 @@ class RustNotificationSettingsService( isOneToOne: Boolean ): Result = withContext(dispatchers.io) { runCatching { - notificationSettings.setDefaultRoomNotificationModeBlocking(isEncrypted, isOneToOne, mode.let(RoomNotificationSettingsMapper::mapMode)) + notificationSettings.setDefaultRoomNotificationMode(isEncrypted, isOneToOne, mode.let(RoomNotificationSettingsMapper::mapMode)) } } override suspend fun setRoomNotificationMode(roomId: RoomId, mode: RoomNotificationMode): Result = withContext(dispatchers.io) { runCatching { - notificationSettings.setRoomNotificationModeBlocking(roomId.value, mode.let(RoomNotificationSettingsMapper::mapMode)) + notificationSettings.setRoomNotificationMode(roomId.value, mode.let(RoomNotificationSettingsMapper::mapMode)) } } override suspend fun restoreDefaultRoomNotificationMode(roomId: RoomId): Result = withContext(dispatchers.io) { runCatching { - notificationSettings.restoreDefaultRoomNotificationModeBlocking(roomId.value) + notificationSettings.restoreDefaultRoomNotificationMode(roomId.value) } } @@ -87,31 +83,31 @@ class RustNotificationSettingsService( override suspend fun unmuteRoom(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean) = withContext(dispatchers.io) { runCatching { - notificationSettings.unmuteRoomBlocking(roomId.value, isEncrypted, isOneToOne) + notificationSettings.unmuteRoom(roomId.value, isEncrypted, isOneToOne) } } override suspend fun isRoomMentionEnabled(): Result = withContext(dispatchers.io) { runCatching { - notificationSettings.isRoomMentionEnabledBlocking() + notificationSettings.isRoomMentionEnabled() } } override suspend fun setRoomMentionEnabled(enabled: Boolean): Result = withContext(dispatchers.io) { runCatching { - notificationSettings.setRoomMentionEnabledBlocking(enabled) + notificationSettings.setRoomMentionEnabled(enabled) } } override suspend fun isCallEnabled(): Result = withContext(dispatchers.io) { runCatching { - notificationSettings.isCallEnabledBlocking() + notificationSettings.isCallEnabled() } } override suspend fun setCallEnabled(enabled: Boolean): Result = withContext(dispatchers.io) { runCatching { - notificationSettings.setCallEnabledBlocking(enabled) + notificationSettings.setCallEnabled(enabled) } } } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt index 87cc742549..4dcdb6d88c 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt @@ -174,7 +174,7 @@ class RustMatrixRoom( _membersStateFlow.value = MatrixRoomMembersState.Pending(prevRoomMembers = currentMembers) var rustMembers: List? = null try { - rustMembers = innerRoom.membersBlocking().use { membersIterator -> + rustMembers = innerRoom.members().use { membersIterator -> buildList { while (true) { // Loading the whole membersIterator as a stop-gap measure. @@ -299,27 +299,27 @@ class RustMatrixRoom( } } - override suspend fun canUserInvite(userId: UserId): Result = withContext(roomMembersDispatcher) { - runCatching { - innerRoom.canUserInviteBlocking(userId.value) + override suspend fun canUserInvite(userId: UserId): Result { + return runCatching { + innerRoom.canUserInvite(userId.value) } } - override suspend fun canUserRedact(userId: UserId): Result = withContext(roomMembersDispatcher) { - runCatching { - innerRoom.canUserRedactBlocking(userId.value) + override suspend fun canUserRedact(userId: UserId): Result { + return runCatching { + innerRoom.canUserRedact(userId.value) } } - override suspend fun canUserSendState(userId: UserId, type: StateEventType): Result = withContext(roomMembersDispatcher) { - runCatching { - innerRoom.canUserSendStateBlocking(userId.value, type.map()) + override suspend fun canUserSendState(userId: UserId, type: StateEventType): Result { + return runCatching { + innerRoom.canUserSendState(userId.value, type.map()) } } - override suspend fun canUserSendMessage(userId: UserId, type: MessageEventType): Result = withContext(roomMembersDispatcher) { - runCatching { - innerRoom.canUserSendMessageBlocking(userId.value, type.map()) + override suspend fun canUserSendMessage(userId: UserId, type: MessageEventType): Result { + return runCatching { + innerRoom.canUserSendMessage(userId.value, type.map()) } } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/roomlist/RoomSummaryListProcessor.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/roomlist/RoomSummaryListProcessor.kt index 754e35ec66..d0e3d1c8cf 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/roomlist/RoomSummaryListProcessor.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/roomlist/RoomSummaryListProcessor.kt @@ -73,7 +73,7 @@ class RoomSummaryListProcessor( } } - private fun MutableList.applyUpdate(update: RoomListEntriesUpdate) { + private suspend fun MutableList.applyUpdate(update: RoomListEntriesUpdate) { when (update) { is RoomListEntriesUpdate.Append -> { val roomSummaries = update.values.map { @@ -119,7 +119,7 @@ class RoomSummaryListProcessor( } } - private fun buildSummaryForRoomListEntry(entry: RoomListEntry): RoomSummary { + private suspend fun buildSummaryForRoomListEntry(entry: RoomListEntry): RoomSummary { return when (entry) { RoomListEntry.Empty -> buildEmptyRoomSummary() is RoomListEntry.Filled -> buildAndCacheRoomSummaryForIdentifier(entry.roomId) @@ -133,9 +133,9 @@ class RoomSummaryListProcessor( return RoomSummary.Empty(UUID.randomUUID().toString()) } - private fun buildAndCacheRoomSummaryForIdentifier(identifier: String): RoomSummary { + private suspend fun buildAndCacheRoomSummaryForIdentifier(identifier: String): RoomSummary { val builtRoomSummary = roomListService.roomOrNull(identifier)?.use { roomListItem -> - roomListItem.roomInfoBlocking().use { roomInfo -> + roomListItem.roomInfo().use { roomInfo -> RoomSummary.Filled( details = roomSummaryDetailsFactory.create(roomInfo) ) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt index 259c2330b9..932da42afb 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt @@ -16,7 +16,6 @@ package io.element.android.libraries.matrix.impl.sync -import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.matrix.api.sync.SyncService import io.element.android.libraries.matrix.api.sync.SyncState import kotlinx.coroutines.CoroutineScope @@ -26,33 +25,27 @@ import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.stateIn -import kotlinx.coroutines.withContext import org.matrix.rustcomponents.sdk.SyncServiceInterface import org.matrix.rustcomponents.sdk.SyncServiceState import timber.log.Timber class RustSyncService( private val innerSyncService: SyncServiceInterface, - private val dispatchers: CoroutineDispatchers, sessionCoroutineScope: CoroutineScope ) : SyncService { - override suspend fun startSync() = withContext(dispatchers.io) { - runCatching { - Timber.i("Start sync") - innerSyncService.startBlocking() - }.onFailure { - Timber.d("Start sync failed: $it") - } + override suspend fun startSync() = runCatching { + Timber.i("Start sync") + innerSyncService.start() + }.onFailure { + Timber.d("Start sync failed: $it") } - override suspend fun stopSync() = withContext(dispatchers.io){ - runCatching { - Timber.i("Stop sync") - innerSyncService.stopBlocking() - }.onFailure { - Timber.d("Stop sync failed: $it") - } + override suspend fun stopSync() = runCatching { + Timber.i("Stop sync") + innerSyncService.stop() + }.onFailure { + Timber.d("Stop sync failed: $it") } override val syncState: StateFlow = diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt index 7b38ba6823..bddd2bc872 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt @@ -44,7 +44,7 @@ internal fun Room.timelineDiffFlow(onInitialList: suspend (List) - } val roomId = id() Timber.d("Open timelineDiffFlow for room $roomId") - val result = addTimelineListenerBlocking(listener) + val result = addTimelineListener(listener) try { onInitialList(result.items) } catch (exception: Exception) { diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt index e453019b52..1f0d13c9bf 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt @@ -124,7 +124,7 @@ class RustMatrixTimeline( private suspend fun fetchMembers() = withContext(dispatcher) { initLatch.await() - innerRoom.fetchMembersBlocking() + innerRoom.fetchMembers() } @OptIn(ExperimentalCoroutinesApi::class) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/verification/RustSessionVerificationService.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/verification/RustSessionVerificationService.kt index 01646b1e4b..29797ed3c4 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/verification/RustSessionVerificationService.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/verification/RustSessionVerificationService.kt @@ -16,7 +16,6 @@ package io.element.android.libraries.matrix.impl.verification -import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.core.data.tryOrNull import io.element.android.libraries.matrix.api.sync.SyncState import io.element.android.libraries.matrix.api.verification.SessionVerificationService @@ -28,7 +27,6 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.withContext import org.matrix.rustcomponents.sdk.SessionVerificationController import org.matrix.rustcomponents.sdk.SessionVerificationControllerDelegate import org.matrix.rustcomponents.sdk.SessionVerificationControllerInterface @@ -37,7 +35,6 @@ import javax.inject.Inject class RustSessionVerificationService @Inject constructor( private val syncService: RustSyncService, - private val dispatchers: CoroutineDispatchers, ) : SessionVerificationService, SessionVerificationControllerDelegate { var verificationController: SessionVerificationControllerInterface? = null @@ -64,31 +61,21 @@ class RustSessionVerificationService @Inject constructor( syncState == SyncState.Running && verificationStatus == SessionVerifiedStatus.NotVerified } - override suspend fun requestVerification() { - tryOrFail { - verificationController?.requestVerificationBlocking() - } + override suspend fun requestVerification() = tryOrFail { + verificationController?.requestVerification() } - override suspend fun cancelVerification() { - tryOrFail { verificationController?.cancelVerificationBlocking() } + override suspend fun cancelVerification() = tryOrFail { verificationController?.cancelVerification() } + + override suspend fun approveVerification() = tryOrFail { verificationController?.approveVerification() } + + override suspend fun declineVerification() = tryOrFail { verificationController?.declineVerification() } + + override suspend fun startVerification() = tryOrFail { + verificationController?.startSasVerification() } - override suspend fun approveVerification() { - tryOrFail { verificationController?.approveVerificationBlocking() } - } - - override suspend fun declineVerification() { - tryOrFail { verificationController?.declineVerificationBlocking() } - } - - override suspend fun startVerification() { - tryOrFail { - verificationController?.startSasVerificationBlocking() - } - } - - private suspend fun tryOrFail(block: suspend () -> Unit) = withContext(dispatchers.io) { + private suspend fun tryOrFail(block: suspend () -> Unit) { runCatching { block() }.onFailure { didFail() }