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
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<Unit>)
suspend fun markAsRead(params: MarkAsReadParams = MarkAsReadParams.BOTH)
/**
* 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.
*/
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.

View file

@ -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<Unit>) {
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<Unit>) {
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<Unit>) {
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 {

View file

@ -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) {

View file

@ -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)
}
}
}
}