Skip to content

Commit c80c982

Browse files
committed
fix(discovery): stop scan immediately without blocking on radio reconnect
- Moves into a detached CoroutineScope within . - Prevents the UI from being stuck in 'Restoring home preset...' (and making the user think the scan is still in progress) for up to 60 seconds while the radio reboots and reconnects over BLE. - Removes state manipulation from to prevent it from overwriting the state when stopped manually.
1 parent 69ca714 commit c80c982

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

feature/discovery/src/commonMain/kotlin/org/meshtastic/feature/discovery/DiscoveryScanEngine.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package org.meshtastic.feature.discovery
2020

2121
import co.touchlab.kermit.Logger
2222
import kotlinx.coroutines.CoroutineScope
23+
import kotlinx.coroutines.Dispatchers
2324
import kotlinx.coroutines.Job
2425
import kotlinx.coroutines.SupervisorJob
2526
import kotlinx.coroutines.cancel
@@ -183,10 +184,14 @@ class DiscoveryScanEngine(
183184
if (!isActive) return
184185
Logger.i { "DiscoveryScanEngine: stopping scan" }
185186
cancelScanInternal()
186-
persistCurrentDwellResults()
187+
}
188+
persistCurrentDwellResults()
189+
finalizeSession("stopped")
190+
_scanState.value = DiscoveryScanState.Idle
191+
192+
// Restore home preset in the background so we don't block the UI with the connection wait
193+
CoroutineScope(Dispatchers.Default).launch {
187194
restoreHomePreset()
188-
finalizeSession("stopped")
189-
_scanState.value = DiscoveryScanState.Idle
190195
}
191196
}
192197

@@ -505,7 +510,6 @@ class DiscoveryScanEngine(
505510

506511
private suspend fun restoreHomePreset() {
507512
val preset = homePreset ?: return
508-
_scanState.value = DiscoveryScanState.Restoring
509513
shiftPreset(preset)
510514
// Wait briefly for reconnection after restoring
511515
waitForConnection()

0 commit comments

Comments
 (0)