From 223973805f434e2dc848a92dcc98703e2b95c3c8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 11 Aug 2026 16:30:11 +0200 Subject: [PATCH] Create function to limit code duplication. --- .../linknewdevice/impl/LinkNewDesktopHandler.kt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/features/linknewdevice/impl/src/main/kotlin/io/element/android/features/linknewdevice/impl/LinkNewDesktopHandler.kt b/features/linknewdevice/impl/src/main/kotlin/io/element/android/features/linknewdevice/impl/LinkNewDesktopHandler.kt index 1b0883ec4e..7afb9a7623 100644 --- a/features/linknewdevice/impl/src/main/kotlin/io/element/android/features/linknewdevice/impl/LinkNewDesktopHandler.kt +++ b/features/linknewdevice/impl/src/main/kotlin/io/element/android/features/linknewdevice/impl/LinkNewDesktopHandler.kt @@ -46,11 +46,15 @@ class LinkNewDesktopHandler( private var timerJob: Job? = null private var handler: LinkDesktopHandler? = null - fun createNewHandler() { + private fun resetJobs() { currentJob?.cancel() currentJob = null timerJob?.cancel() timerJob = null + } + + fun createNewHandler() { + resetJobs() handler = matrixClient.createLinkDesktopHandler().getOrNull() timerJob = sessionScope.launch { delay(2.minutes) @@ -59,20 +63,14 @@ class LinkNewDesktopHandler( } fun reset() { - currentJob?.cancel() - currentJob = null - timerJob?.cancel() - timerJob = null + resetJobs() sessionScope.launch { linkDesktopStepFlow.emit(LinkDesktopStep.Uninitialized) } } fun onScannedCode(data: ByteArray) { - currentJob?.cancel() - currentJob = null - timerJob?.cancel() - timerJob = null + resetJobs() val currentHandler = handler if (currentHandler == null) { Timber.tag(loggerTag.value).e("onScannedCode: Handler is not initialized. Call createNewHandler() first.")