Convert ReadService to suspend functions

Signed-off-by: Dominic Fischer <dominicfischer7@gmail.com>
This commit is contained in:
Dominic Fischer 2020-11-08 13:06:50 +00:00
parent 2045a164c1
commit f8718e397c
4 changed files with 29 additions and 30 deletions

View file

@ -17,7 +17,6 @@
package org.matrix.android.sdk.api.session.room.read package org.matrix.android.sdk.api.session.room.read
import androidx.lifecycle.LiveData 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.session.room.model.ReadReceipt
import org.matrix.android.sdk.api.util.Optional 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. * Force the read marker to be set on the latest event.
*/ */
fun markAsRead(params: MarkAsReadParams = MarkAsReadParams.BOTH, callback: MatrixCallback<Unit>) suspend fun markAsRead(params: MarkAsReadParams = MarkAsReadParams.BOTH)
/** /**
* Set the read receipt on the event with provided eventId. * Set the read receipt on the event with provided eventId.
*/ */
fun setReadReceipt(eventId: String, callback: MatrixCallback<Unit>) suspend fun setReadReceipt(eventId: String)
/** /**
* Set the read marker on the event with provided eventId. * Set the read marker on the event with provided eventId.
*/ */
fun setReadMarker(fullyReadEventId: String, callback: MatrixCallback<Unit>) suspend fun setReadMarker(fullyReadEventId: String)
/** /**
* Check if an event is already read, ie. your read receipt is set on a more recent event. * Check if an event is already read, ie. your read receipt is set on a more recent event.

View file

@ -22,7 +22,6 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import dagger.assisted.AssistedFactory import dagger.assisted.AssistedFactory
import com.zhuinden.monarchy.Monarchy 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.model.ReadReceipt
import org.matrix.android.sdk.api.session.room.read.ReadService import org.matrix.android.sdk.api.session.room.read.ReadService
import org.matrix.android.sdk.api.util.Optional 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.SessionDatabase
import org.matrix.android.sdk.internal.di.UserId import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.task.TaskExecutor import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
internal class DefaultReadService @AssistedInject constructor( internal class DefaultReadService @AssistedInject constructor(
@Assisted private val roomId: String, @Assisted private val roomId: String,
@ -52,35 +50,23 @@ internal class DefaultReadService @AssistedInject constructor(
fun create(roomId: String): DefaultReadService fun create(roomId: String): DefaultReadService
} }
override fun markAsRead(params: ReadService.MarkAsReadParams, callback: MatrixCallback<Unit>) { override suspend fun markAsRead(params: ReadService.MarkAsReadParams) {
val taskParams = SetReadMarkersTask.Params( val taskParams = SetReadMarkersTask.Params(
roomId = roomId, roomId = roomId,
forceReadMarker = params.forceReadMarker(), forceReadMarker = params.forceReadMarker(),
forceReadReceipt = params.forceReadReceipt() forceReadReceipt = params.forceReadReceipt()
) )
setReadMarkersTask setReadMarkersTask.execute(taskParams)
.configureWith(taskParams) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
override fun setReadReceipt(eventId: String, callback: MatrixCallback<Unit>) { override suspend fun setReadReceipt(eventId: String) {
val params = SetReadMarkersTask.Params(roomId, fullyReadEventId = null, readReceiptEventId = eventId) val params = SetReadMarkersTask.Params(roomId, fullyReadEventId = null, readReceiptEventId = eventId)
setReadMarkersTask setReadMarkersTask.execute(params)
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
override fun setReadMarker(fullyReadEventId: String, callback: MatrixCallback<Unit>) { override suspend fun setReadMarker(fullyReadEventId: String) {
val params = SetReadMarkersTask.Params(roomId, fullyReadEventId = fullyReadEventId, readReceiptEventId = null) val params = SetReadMarkersTask.Params(roomId, fullyReadEventId = fullyReadEventId, readReceiptEventId = null)
setReadMarkersTask setReadMarkersTask.execute(params)
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
override fun isEventRead(eventId: String): Boolean { override fun isEventRead(eventId: String): Boolean {

View file

@ -181,7 +181,9 @@ class RoomDetailViewModel @AssistedInject constructor(
observePowerLevel() observePowerLevel()
updateShowDialerOptionState() updateShowDialerOptionState()
room.getRoomSummaryLive() 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 // Inform the SDK that the room is displayed
session.onRoomDisplayed(initialState.roomId) session.onRoomDisplayed(initialState.roomId)
callManager.addPstnSupportListener(this) callManager.addPstnSupportListener(this)
@ -546,7 +548,9 @@ class RoomDetailViewModel @AssistedInject constructor(
private fun stopTrackingUnreadMessages() { private fun stopTrackingUnreadMessages() {
if (trackUnreadMessages.getAndSet(false)) { if (trackUnreadMessages.getAndSet(false)) {
mostRecentDisplayedEvent?.root?.eventId?.also { mostRecentDisplayedEvent?.root?.eventId?.also {
room.setReadMarker(it, callback = NoOpMatrixCallback()) viewModelScope.launch {
room.setReadMarker(it)
}
} }
mostRecentDisplayedEvent = null mostRecentDisplayedEvent = null
} }
@ -1248,14 +1252,18 @@ class RoomDetailViewModel @AssistedInject constructor(
} }
} }
bufferedMostRecentDisplayedEvent.root.eventId?.let { eventId -> bufferedMostRecentDisplayedEvent.root.eventId?.let { eventId ->
room.setReadReceipt(eventId, callback = NoOpMatrixCallback()) viewModelScope.launch {
room.setReadReceipt(eventId)
}
} }
}) })
.disposeOnClear() .disposeOnClear()
} }
private fun handleMarkAllAsRead() { private fun handleMarkAllAsRead() {
room.markAsRead(ReadService.MarkAsReadParams.BOTH, NoOpMatrixCallback()) viewModelScope.launch {
room.markAsRead(ReadService.MarkAsReadParams.BOTH)
}
} }
private fun handleReportContent(action: RoomDetailAction.ReportContent) { private fun handleReportContent(action: RoomDetailAction.ReportContent) {

View file

@ -23,6 +23,8 @@ import androidx.core.app.RemoteInput
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.extensions.vectorComponent 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.NoOpMatrixCallback
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.Room import org.matrix.android.sdk.api.session.room.Room
@ -88,8 +90,12 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
private fun handleMarkAsRead(roomId: String) { private fun handleMarkAsRead(roomId: String) {
activeSessionHolder.getActiveSession().let { session -> activeSessionHolder.getActiveSession().let { session ->
session.getRoom(roomId) val room = session.getRoom(roomId)
?.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT, NoOpMatrixCallback()) if (room != null) {
GlobalScope.launch {
room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT)
}
}
} }
} }