[LinkNewDevice] Add a 2 minutes timer to scan a QrCode

#7303
This commit is contained in:
Benoit Marty
2026-08-07 15:02:55 +02:00
committed by Benoit Marty
parent 044737a3a7
commit 33979b6574
@@ -12,10 +12,12 @@ import dev.zacsweers.metro.SingleIn
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.linknewdevice.ErrorType
import io.element.android.libraries.matrix.api.linknewdevice.LinkDesktopHandler
import io.element.android.libraries.matrix.api.linknewdevice.LinkDesktopStep
import io.element.android.libraries.matrix.api.logs.LoggerTags
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
@@ -23,6 +25,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import timber.log.Timber
import kotlin.time.Duration.Companion.minutes
private val loggerTag = LoggerTag("LinkNewDesktopHandler", LoggerTags.linkNewDevice)
@@ -40,17 +43,26 @@ class LinkNewDesktopHandler(
get() = linkDesktopStepFlow.asStateFlow()
private var currentJob: Job? = null
private var timerJob: Job? = null
private var handler: LinkDesktopHandler? = null
fun createNewHandler() {
currentJob?.cancel()
currentJob = null
timerJob?.cancel()
timerJob = null
handler = matrixClient.createLinkDesktopHandler().getOrNull()
timerJob = sessionScope.launch {
delay(2.minutes)
linkDesktopStepFlow.emit(LinkDesktopStep.Error(ErrorType.Expired("Scanning QrCode took too long.")))
}
}
fun reset() {
currentJob?.cancel()
currentJob = null
timerJob?.cancel()
timerJob = null
sessionScope.launch {
linkDesktopStepFlow.emit(LinkDesktopStep.Uninitialized)
}
@@ -59,6 +71,8 @@ class LinkNewDesktopHandler(
fun onScannedCode(data: ByteArray) {
currentJob?.cancel()
currentJob = null
timerJob?.cancel()
timerJob = null
val currentHandler = handler
if (currentHandler == null) {
Timber.tag(loggerTag.value).e("onScannedCode: Handler is not initialized. Call createNewHandler() first.")