Lilly
12/19/2020, 2:04 PMval devices = remember { mutableStateListOf<DeviceModel>() }
LaunchedEffect(subject = viewModel.devices, block = {
viewModel.devices.collect { model ->
if (!devices.contains(model)) devices.add(model)
}
})
and
val devices by produceState(initialValue = mutableStateListOf<DeviceModel>(), viewModel) {
viewModel.devices.collect { model ->
if (!value.contains(model)) value.add(model)
}
}
allan.conda
12/19/2020, 2:17 PMLilly
12/19/2020, 3:55 PMmutableStateList
?Dominaezzz
12/19/2020, 5:09 PMList<DeviceModel>
for the second case, then they become more comparable.Adam Powell
12/19/2020, 5:12 PMPersistentList
, I think MutableStateList
uses the persistent collections under the hoodDominaezzz
12/19/2020, 5:12 PMLinkedHashSet
.Lilly
12/20/2020, 4:14 AMlinkedSetOf
or mutableListOf
for second snippt, no items are shown which makes sense, because I don't do value = device
so devices
doesn't change but when I use mutableStateListOf
and do value.add(device)
, devices
changed. Or am I wrong? Btw...it seems that PersistentList
doesn't exist, same for MutableStateList
?!Adam Powell
12/20/2020, 4:22 AMvar list = mutableListOf()
- you don't need both the list and the reference to the list to be mutableproduceState
is about that leading var
Lilly
12/20/2020, 4:27 AMis about that leadingproduceState
var
Adam Powell
12/20/2020, 4:30 AMproduceState
is just a convenience around
val state = remember { mutableStateOf(initialValue) }
LaunchedEffect {
state.block()
}
return state
Lilly
12/20/2020, 4:33 AMAdam Powell
12/20/2020, 4:33 AMLilly
12/20/2020, 4:34 AMAdam Powell
12/20/2020, 4:34 AMLilly
12/20/2020, 4:36 AMAdam Powell
12/20/2020, 5:00 AMmutableStateListOf
and mutableStateMapOf
for snapshot-observable collections at the moment, we don't have a set. If you combine PersistentSet
from kotlinx.collections.immutable and produceState
from compose you should have what you needPersistentSet
is an efficient immutable set type that potentially returns a new set instance whenever you add or remove from it, and if you store that reference in a mutableStateOf
holder, you get the observability you need with an efficient immutable set implementationLilly
12/21/2020, 3:56 AMAdam Powell
12/21/2020, 4:01 AMLilly
12/21/2020, 9:43 PMAdam Powell
12/21/2020, 10:42 PMwithMutableSnapshot
. Any changes you make within an explicit mutable snapshot all happen together or not at all, and changes aren't made visible to other threads unless you commit all changes together or otherwise pass the snapshot between threads explicitly.Lilly
12/21/2020, 11:12 PMval devices = remember { mutableStateOf(persistentListOf<DeviceModel>()) }
LaunchedEffect(subject = viewModel.deviceFlow, block = {
viewModel.deviceFlow.collect { model ->
if (!devices.value.contains(model)) {
devices.value = devices.value.add(model)
}
}
})
BluetoothDeviceListComponent(devices.value, onConnectDevice)
What do I wrong? :/Dominaezzz
12/21/2020, 11:15 PMDeviceModel
have an equals
implementation?Lilly
12/21/2020, 11:18 PMdata class DeviceModel(val bluetoothDevice: BluetoothDevice, val rssi: Int)
data classes generate equals functions right?Adam Powell
12/21/2020, 11:20 PMLilly
12/22/2020, 1:03 AMdata class BluetoothDeviceModel(val bluetoothDevice: BluetoothDevice, val rssi: Int) {
override fun equals(other: Any?): Boolean {
if (other is BluetoothDeviceModel) {
return this.bluetoothDevice.address == other.bluetoothDevice.address
}
return false
}
}
Additionally when I want to update the list with newer devices there are multiple options. To preserve the order of the items I came up with this solution:
val devices = remember { mutableStateListOf<DiscoverBluetoothDevicesUseCase.BluetoothDeviceModel>() }
LaunchedEffect(subject = viewModel.deviceFlow, block = {
viewModel.deviceFlow.collect { model ->
withMutableSnapshot {
devices.indexOf(model).takeIf { it != -1 }?.let { i ->
devices.remove(devices[i])
devices.add(i, model)
} ?: devices.add(model)
}
}
})
@Adam Powell Is this implementation legit regarding performance and do I use withMutableSnapshot
properly?Adam Powell
12/22/2020, 1:47 AM