Joel Denke
07/17/2023, 3:51 PMkevin.cianfarini
07/17/2023, 3:52 PMephemient
07/17/2023, 3:53 PMJoel Denke
07/17/2023, 3:55 PMkevin.cianfarini
07/17/2023, 3:55 PMJoel Denke
07/17/2023, 3:55 PMJoel Denke
07/17/2023, 3:56 PMJoel Denke
07/17/2023, 3:56 PMJoel Denke
07/17/2023, 3:56 PMephemient
07/17/2023, 3:56 PMkevin.cianfarini
07/17/2023, 3:56 PMJoel Denke
07/17/2023, 3:57 PMJoel Denke
07/17/2023, 3:57 PMkevin.cianfarini
07/17/2023, 3:59 PMJoel Denke
07/17/2023, 3:59 PMkevin.cianfarini
07/17/2023, 4:00 PMMutex
or StateFlow
to ensure atomic operations of a mutable list that can be accessed from multiple coroutines.Joel Denke
07/17/2023, 4:00 PMJoel Denke
07/17/2023, 4:00 PMephemient
07/17/2023, 4:02 PMephemient
07/17/2023, 4:03 PMJoel Denke
07/17/2023, 4:03 PMTodd
07/17/2023, 4:03 PMJoel Denke
07/17/2023, 4:04 PMephemient
07/17/2023, 4:04 PMephemient
07/17/2023, 4:04 PMJoel Denke
07/17/2023, 4:05 PMval items = Collections.synchronizedList(listOf<MyItem>())
Joel Denke
07/17/2023, 4:06 PMephemient
07/17/2023, 4:06 PMJoel Denke
07/17/2023, 4:07 PMJoel Denke
07/17/2023, 4:08 PMJoel Denke
07/17/2023, 4:09 PMrocketraman
07/17/2023, 4:12 PMJoel Denke
07/17/2023, 4:13 PMJoel Denke
07/17/2023, 4:16 PMJoel Denke
07/17/2023, 4:17 PMrocketraman
07/17/2023, 4:21 PMStateFlow<List<MyDevice>>
, and that state, or some derivation of it, is used for whatever logic you need.rocketraman
07/17/2023, 4:22 PMephemient
07/17/2023, 4:24 PMkevin.cianfarini
07/17/2023, 4:24 PMJoel Denke
07/17/2023, 4:25 PMJoel Denke
07/17/2023, 4:26 PMJoel Denke
07/17/2023, 4:28 PMkevin.cianfarini
07/17/2023, 4:29 PMfun BluetoothThingy.connectionFlow(deviceThingy: DeviceThingy, connectionStatus: Flow<Boolean>): Flow<Device> {
connectionStatus.distinctUntilChanged().flatMapLatest { connected ->
if (connected) connectionFlow(deviceThingy) else emptyFlow()
}
}
private fun BluetoothThingy.connectionFlow(deviceThingy: DeviceThingy) = callbackFlow {
val callback = /* register the Android SDK bluetooth callback */
awaitClose { unregister(callback) }
}
kevin.cianfarini
07/17/2023, 4:29 PMn + 1
deviceskevin.cianfarini
07/17/2023, 4:30 PMkevin.cianfarini
07/17/2023, 4:30 PMJoel Denke
07/17/2023, 4:31 PMn + 1
devices" yeah this is the complexity, how is the best way of compose all devices into one flow, but be able to orchestrate each indivudal devide as well?kevin.cianfarini
07/17/2023, 4:32 PMconnectionStatus: Flow<Boolean>
and then you could combine all of those flows into one streamkevin.cianfarini
07/17/2023, 4:32 PMJoel Denke
07/17/2023, 4:32 PMJoel Denke
07/17/2023, 4:36 PMrocketraman
07/17/2023, 4:36 PMupdate
function lets you update it atomically.rocketraman
07/17/2023, 4:38 PMJoel Denke
07/17/2023, 4:38 PMscanningRequests.update {
it + ScanRequest(
config = config,
scanCallback = scanCallback
)
}
This is the way I usually prefer, but cant control how all code is written 🙂Joel Denke
07/17/2023, 4:39 PM