From f8718e397c27a18165bfe81f00a87b4a30d1a610 Mon Sep 17 00:00:00 2001 From: Dominic Fischer Date: Sun, 8 Nov 2020 13:06:50 +0000 Subject: [PATCH 1/2] Convert ReadService to suspend functions Signed-off-by: Dominic Fischer --- .../sdk/api/session/room/read/ReadService.kt | 7 +++-- .../session/room/read/DefaultReadService.kt | 26 +++++-------------- .../home/room/detail/RoomDetailViewModel.kt | 16 +++++++++--- .../NotificationBroadcastReceiver.kt | 10 +++++-- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/read/ReadService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/read/ReadService.kt index 4f44c9a912..b037a3f366 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/read/ReadService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/read/ReadService.kt @@ -17,7 +17,6 @@ package org.matrix.android.sdk.api.session.room.read import androidx.lifecycle.LiveData -import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.session.room.model.ReadReceipt import org.matrix.android.sdk.api.util.Optional @@ -35,17 +34,17 @@ interface ReadService { /** * Force the read marker to be set on the latest event. */ - fun markAsRead(params: MarkAsReadParams = MarkAsReadParams.BOTH, callback: MatrixCallback) + suspend fun markAsRead(params: MarkAsReadParams = MarkAsReadParams.BOTH) /** * Set the read receipt on the event with provided eventId. */ - fun setReadReceipt(eventId: String, callback: MatrixCallback) + suspend fun setReadReceipt(eventId: String) /** * Set the read marker on the event with provided eventId. */ - fun setReadMarker(fullyReadEventId: String, callback: MatrixCallback) + suspend fun setReadMarker(fullyReadEventId: String) /** * Check if an event is already read, ie. your read receipt is set on a more recent event. diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/DefaultReadService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/DefaultReadService.kt index 3cf8cfe5ee..d4d03dca05 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/DefaultReadService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/DefaultReadService.kt @@ -22,7 +22,6 @@ import dagger.assisted.Assisted import dagger.assisted.AssistedInject import dagger.assisted.AssistedFactory import com.zhuinden.monarchy.Monarchy -import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.session.room.model.ReadReceipt import org.matrix.android.sdk.api.session.room.read.ReadService import org.matrix.android.sdk.api.util.Optional @@ -36,7 +35,6 @@ import org.matrix.android.sdk.internal.database.query.where import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.di.UserId import org.matrix.android.sdk.internal.task.TaskExecutor -import org.matrix.android.sdk.internal.task.configureWith internal class DefaultReadService @AssistedInject constructor( @Assisted private val roomId: String, @@ -52,35 +50,23 @@ internal class DefaultReadService @AssistedInject constructor( fun create(roomId: String): DefaultReadService } - override fun markAsRead(params: ReadService.MarkAsReadParams, callback: MatrixCallback) { + override suspend fun markAsRead(params: ReadService.MarkAsReadParams) { val taskParams = SetReadMarkersTask.Params( roomId = roomId, forceReadMarker = params.forceReadMarker(), forceReadReceipt = params.forceReadReceipt() ) - setReadMarkersTask - .configureWith(taskParams) { - this.callback = callback - } - .executeBy(taskExecutor) + setReadMarkersTask.execute(taskParams) } - override fun setReadReceipt(eventId: String, callback: MatrixCallback) { + override suspend fun setReadReceipt(eventId: String) { val params = SetReadMarkersTask.Params(roomId, fullyReadEventId = null, readReceiptEventId = eventId) - setReadMarkersTask - .configureWith(params) { - this.callback = callback - } - .executeBy(taskExecutor) + setReadMarkersTask.execute(params) } - override fun setReadMarker(fullyReadEventId: String, callback: MatrixCallback) { + override suspend fun setReadMarker(fullyReadEventId: String) { val params = SetReadMarkersTask.Params(roomId, fullyReadEventId = fullyReadEventId, readReceiptEventId = null) - setReadMarkersTask - .configureWith(params) { - this.callback = callback - } - .executeBy(taskExecutor) + setReadMarkersTask.execute(params) } override fun isEventRead(eventId: String): Boolean { diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt index af3d5461ef..120e80a1b6 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt @@ -181,7 +181,9 @@ class RoomDetailViewModel @AssistedInject constructor( observePowerLevel() updateShowDialerOptionState() room.getRoomSummaryLive() - room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT, NoOpMatrixCallback()) + viewModelScope.launch { + room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT) + } // Inform the SDK that the room is displayed session.onRoomDisplayed(initialState.roomId) callManager.addPstnSupportListener(this) @@ -546,7 +548,9 @@ class RoomDetailViewModel @AssistedInject constructor( private fun stopTrackingUnreadMessages() { if (trackUnreadMessages.getAndSet(false)) { mostRecentDisplayedEvent?.root?.eventId?.also { - room.setReadMarker(it, callback = NoOpMatrixCallback()) + viewModelScope.launch { + room.setReadMarker(it) + } } mostRecentDisplayedEvent = null } @@ -1248,14 +1252,18 @@ class RoomDetailViewModel @AssistedInject constructor( } } bufferedMostRecentDisplayedEvent.root.eventId?.let { eventId -> - room.setReadReceipt(eventId, callback = NoOpMatrixCallback()) + viewModelScope.launch { + room.setReadReceipt(eventId) + } } }) .disposeOnClear() } private fun handleMarkAllAsRead() { - room.markAsRead(ReadService.MarkAsReadParams.BOTH, NoOpMatrixCallback()) + viewModelScope.launch { + room.markAsRead(ReadService.MarkAsReadParams.BOTH) + } } private fun handleReportContent(action: RoomDetailAction.ReportContent) { diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt index d79d16a052..c4d7376c55 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt @@ -23,6 +23,8 @@ import androidx.core.app.RemoteInput import im.vector.app.R import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.extensions.vectorComponent +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import org.matrix.android.sdk.api.NoOpMatrixCallback import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.room.Room @@ -88,8 +90,12 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { private fun handleMarkAsRead(roomId: String) { activeSessionHolder.getActiveSession().let { session -> - session.getRoom(roomId) - ?.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT, NoOpMatrixCallback()) + val room = session.getRoom(roomId) + if (room != null) { + GlobalScope.launch { + room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT) + } + } } } From 42166c1c0f8070e16f69a6a5b98f1807d6ed2fde Mon Sep 17 00:00:00 2001 From: Dominic Fischer Date: Mon, 29 Mar 2021 19:48:26 +0100 Subject: [PATCH 2/2] Error handling Signed-off-by: Dominic Fischer --- .../home/room/detail/RoomDetailViewModel.kt | 15 ++++++++++++--- .../NotificationBroadcastReceiver.kt | 5 ++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt index 120e80a1b6..81968ab13a 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt @@ -182,7 +182,10 @@ class RoomDetailViewModel @AssistedInject constructor( updateShowDialerOptionState() room.getRoomSummaryLive() viewModelScope.launch { - room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT) + try { + room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT) + } catch (_: Exception) { + } } // Inform the SDK that the room is displayed session.onRoomDisplayed(initialState.roomId) @@ -549,7 +552,10 @@ class RoomDetailViewModel @AssistedInject constructor( if (trackUnreadMessages.getAndSet(false)) { mostRecentDisplayedEvent?.root?.eventId?.also { viewModelScope.launch { - room.setReadMarker(it) + try { + room.setReadMarker(it) + } catch (_: Exception) { + } } } mostRecentDisplayedEvent = null @@ -1262,7 +1268,10 @@ class RoomDetailViewModel @AssistedInject constructor( private fun handleMarkAllAsRead() { viewModelScope.launch { - room.markAsRead(ReadService.MarkAsReadParams.BOTH) + try { + room.markAsRead(ReadService.MarkAsReadParams.BOTH) + } catch (_: Exception) { + } } } diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt index c4d7376c55..9dd41e4cea 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt @@ -93,7 +93,10 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { val room = session.getRoom(roomId) if (room != null) { GlobalScope.launch { - room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT) + try { + room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT) + } catch (_: Exception) { + } } } }