Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert ReadService to suspend functions #3075

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ class RoomDetailViewModel @AssistedInject constructor(
observePowerLevel()
updateShowDialerOptionState()
room.getRoomSummaryLive()
room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT, NoOpMatrixCallback())
viewModelScope.launch {
try {
room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT)
} catch (_: Exception) {
}
}
// Inform the SDK that the room is displayed
session.onRoomDisplayed(initialState.roomId)
callManager.addPstnSupportListener(this)
Expand Down Expand Up @@ -546,7 +551,12 @@ class RoomDetailViewModel @AssistedInject constructor(
private fun stopTrackingUnreadMessages() {
if (trackUnreadMessages.getAndSet(false)) {
mostRecentDisplayedEvent?.root?.eventId?.also {
room.setReadMarker(it, callback = NoOpMatrixCallback())
viewModelScope.launch {
try {
room.setReadMarker(it)
} catch (_: Exception) {
}
}
}
mostRecentDisplayedEvent = null
}
Expand Down Expand Up @@ -1248,14 +1258,21 @@ 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 {
try {
room.markAsRead(ReadService.MarkAsReadParams.BOTH)
} catch (_: Exception) {
}
}
}

private fun handleReportContent(action: RoomDetailAction.ReportContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,8 +90,15 @@ 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 {
try {
room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT)
} catch (_: Exception) {
}
}
}
}
}

Expand Down