Convert DefaultTypingService to coroutines

This commit is contained in:
Benoit Marty 2021-06-01 10:48:55 +02:00 committed by Benoit Marty
parent 209792a9ec
commit f99600f115
2 changed files with 35 additions and 56 deletions

View File

@ -18,13 +18,13 @@ package org.matrix.android.sdk.internal.session.room.typing
import android.os.SystemClock
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import dagger.assisted.AssistedFactory
import org.matrix.android.sdk.api.MatrixCallback
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.room.typing.TypingService
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
import timber.log.Timber
/**
@ -35,7 +35,6 @@ import timber.log.Timber
*/
internal class DefaultTypingService @AssistedInject constructor(
@Assisted private val roomId: String,
private val taskExecutor: TaskExecutor,
private val sendTypingTask: SendTypingTask
) : TypingService {
@ -44,8 +43,8 @@ internal class DefaultTypingService @AssistedInject constructor(
fun create(roomId: String): DefaultTypingService
}
private var currentTask: Cancelable? = null
private var currentAutoStopTask: Cancelable? = null
private var job = Job()
private var currentTask: Job? = null
// What the homeserver knows
private var userIsTyping = false
@ -53,26 +52,24 @@ internal class DefaultTypingService @AssistedInject constructor(
// Last time the user is typing event has been sent
private var lastRequestTimestamp: Long = 0
/**
* Notify to the server that the user is typing and schedule the auto typing off
*/
override fun userIsTyping() {
scheduleAutoStop()
val now = SystemClock.elapsedRealtime()
currentTask?.cancel()
currentTask = CoroutineScope(job).launch {
if (userIsTyping && now < lastRequestTimestamp + MIN_DELAY_BETWEEN_TWO_USER_IS_TYPING_REQUESTS_MILLIS) {
Timber.d("Typing: Skip start request")
return
}
} else {
Timber.d("Typing: Send start request")
userIsTyping = true
lastRequestTimestamp = now
currentTask?.cancel()
val params = SendTypingTask.Params(roomId, true)
currentTask = sendTypingTask
.configureWith(params)
.executeBy(taskExecutor)
sendRequest(true)
}
delay(MIN_DELAY_TO_SEND_STOP_TYPING_REQUEST_WHEN_NO_USER_ACTIVITY_MILLIS)
Timber.d("Typing: auto stop")
sendRequest(false)
}
}
override fun userStopsTyping() {
@ -82,36 +79,23 @@ internal class DefaultTypingService @AssistedInject constructor(
}
Timber.d("Typing: Send stop request")
userIsTyping = false
lastRequestTimestamp = 0
currentAutoStopTask?.cancel()
currentTask?.cancel()
val params = SendTypingTask.Params(roomId, false)
currentTask = sendTypingTask
.configureWith(params)
.executeBy(taskExecutor)
currentTask = CoroutineScope(job).launch {
sendRequest(false)
}
}
private fun scheduleAutoStop() {
Timber.d("Typing: Schedule auto stop")
currentAutoStopTask?.cancel()
val params = SendTypingTask.Params(
roomId,
false,
delay = MIN_DELAY_TO_SEND_STOP_TYPING_REQUEST_WHEN_NO_USER_ACTIVITY_MILLIS)
currentAutoStopTask = sendTypingTask
.configureWith(params) {
callback = object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
userIsTyping = false
private suspend fun sendRequest(isTyping: Boolean) {
try {
sendTypingTask.execute(SendTypingTask.Params(roomId, isTyping))
userIsTyping = isTyping
} catch (failure: Throwable) {
// Ignore network error, etc...
Timber.w(failure, "Unable to send typing request")
}
}
}
.executeBy(taskExecutor)
}
companion object {
private const val MIN_DELAY_BETWEEN_TWO_USER_IS_TYPING_REQUESTS_MILLIS = 10_000L

View File

@ -17,11 +17,10 @@
package org.matrix.android.sdk.internal.session.room.typing
import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.session.room.RoomAPI
import org.matrix.android.sdk.internal.task.Task
import kotlinx.coroutines.delay
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import javax.inject.Inject
internal interface SendTypingTask : Task<SendTypingTask.Params, Unit> {
@ -29,9 +28,7 @@ internal interface SendTypingTask : Task<SendTypingTask.Params, Unit> {
data class Params(
val roomId: String,
val isTyping: Boolean,
val typingTimeoutMillis: Int? = 30_000,
// Optional delay before sending the request to the homeserver
val delay: Long? = null
val typingTimeoutMillis: Int? = 30_000
)
}
@ -42,8 +39,6 @@ internal class DefaultSendTypingTask @Inject constructor(
) : SendTypingTask {
override suspend fun execute(params: SendTypingTask.Params) {
delay(params.delay ?: -1)
executeRequest(globalErrorReceiver) {
roomAPI.sendTypingState(
params.roomId,