feat: collect bondedDevices flow in BTScanModel

pull/610/head
andrekir 2023-03-27 15:27:26 -03:00
rodzic 84c85a8c3e
commit 057f94e423
4 zmienionych plików z 13 dodań i 19 usunięć

Wyświetl plik

@ -93,6 +93,10 @@ class BTScanModel @Inject constructor(
private val context: Context get() = application.applicationContext
init {
bluetoothRepository.state.value.bondedDevices.onEach {
setupScan() // TODO clean up device list updates
}.launchIn(viewModelScope)
debug("BTScanModel created")
}

Wyświetl plik

@ -13,7 +13,6 @@ import com.geeksville.mesh.android.hasBluetoothPermission
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton
@ -95,19 +94,13 @@ class BluetoothRepository @Inject constructor(
* Creates a cold Flow used to obtain the set of bonded devices.
*/
@SuppressLint("MissingPermission") // Already checked prior to calling
private suspend fun createBondedDevicesFlow(adapter: BluetoothAdapter): Flow<Set<BluetoothDevice>>? {
return if (adapter.isEnabled) {
flow<Set<BluetoothDevice>> {
withContext(dispatchers.default) {
while (true) {
emit(adapter.bondedDevices)
delay(REFRESH_DELAY_MS)
}
}
}.flowOn(dispatchers.default)
} else {
null
}
private suspend fun createBondedDevicesFlow(adapter: BluetoothAdapter): Flow<Set<BluetoothDevice>> {
return flow<Set<BluetoothDevice>> {
while (true) {
emit(adapter.bondedDevices ?: emptySet())
delay(REFRESH_DELAY_MS)
}
}.flowOn(dispatchers.default).distinctUntilChanged()
}
companion object {

Wyświetl plik

@ -2,6 +2,7 @@ package com.geeksville.mesh.repository.bluetooth
import android.bluetooth.BluetoothDevice
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
/**
* A snapshot in time of the state of the bluetooth subsystem.
@ -12,5 +13,5 @@ data class BluetoothState(
/** If we have adequate permissions and bluetooth is enabled */
val enabled: Boolean = false,
/** If enabled, a cold flow of the currently bonded devices */
val bondedDevices: Flow<Set<BluetoothDevice>>? = null
val bondedDevices: Flow<Set<BluetoothDevice>> = flowOf(emptySet())
)

Wyświetl plik

@ -290,10 +290,6 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = regionAdapter
bluetoothViewModel.enabled.observe(viewLifecycleOwner) { enabled ->
if (enabled) scanModel.setupScan()
}
model.ownerName.observe(viewLifecycleOwner) { name ->
binding.usernameEditText.isEnabled = !name.isNullOrEmpty()
binding.usernameEditText.setText(name)