Fix: Device selection lags (#2086)

This commit is contained in:
Jeremiah K 2025-06-13 06:34:44 -05:00 committed by GitHub
parent faf9f7b0e1
commit 4de1b1516f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 20 additions and 2 deletions

View file

@ -47,7 +47,10 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import javax.inject.Inject
@HiltViewModel
@ -158,7 +161,15 @@ class BTScanModel @Inject constructor(
val selectedBluetooth: Boolean get() = selectedAddress?.getOrNull(0) == 'x'
// / Use the string for the NopInterface
val selectedNotNull: String get() = selectedAddress ?: NO_DEVICE_SELECTED
val selectedAddressFlow: StateFlow<String?> = radioInterfaceService.currentDeviceAddressFlow
val selectedNotNullFlow: StateFlow<String> = selectedAddressFlow
.map { it ?: NO_DEVICE_SELECTED }
.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(SHARING_STARTED_TIMEOUT_MS),
selectedAddressFlow.value ?: NO_DEVICE_SELECTED
)
val scanResult = MutableLiveData<MutableMap<String, DeviceListEntry>>(mutableMapOf())
@ -282,3 +293,4 @@ class BTScanModel @Inject constructor(
}
const val NO_DEVICE_SELECTED = "n"
private const val SHARING_STARTED_TIMEOUT_MS = 5000L

View file

@ -41,6 +41,7 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@ -74,6 +75,10 @@ class RadioInterfaceService @Inject constructor(
private val _receivedData = MutableSharedFlow<ByteArray>()
val receivedData: SharedFlow<ByteArray> = _receivedData
// Thread-safe StateFlow for tracking device address changes
private val _currentDeviceAddressFlow = MutableStateFlow<String?>(prefs.getString(DEVADDR_KEY, null))
val currentDeviceAddressFlow: StateFlow<String?> = _currentDeviceAddressFlow.asStateFlow()
private val logSends = false
private val logReceives = false
private lateinit var sentPacketsLog: BinaryLogFile // inited in onCreate
@ -301,6 +306,7 @@ class RadioInterfaceService @Inject constructor(
putString(DEVADDR_KEY, address)
}
}
_currentDeviceAddressFlow.value = address
// Force the service to reconnect
startInterface()

View file

@ -141,7 +141,7 @@ fun ConnectionsScreen(
val context = LocalContext.current
val app = (context.applicationContext as GeeksvilleApplication)
val info by uiViewModel.myNodeInfo.collectAsState()
val selectedDevice = scanModel.selectedNotNull
val selectedDevice by scanModel.selectedNotNullFlow.collectAsStateWithLifecycle()
val bluetoothEnabled by bluetoothViewModel.enabled.observeAsState()
val regionUnset = currentRegion == ConfigProtos.Config.LoRaConfig.RegionCode.UNSET &&
connectionState == MeshService.ConnectionState.CONNECTED