workaround a kotlin(?!) bug

// FIXME - possible kotlin bug in 1.3.72 - it seems that if we start with the (globally shared) emptyList,
// then adding items are affecting that shared list rather than a copy.   This was causing aliasing of
// recentDataPackets with messages.value in the GUI.  So if the current list is empty we are careful to make a new list
This commit is contained in:
geeksville 2020-06-04 08:53:37 -07:00
parent ef61c9acff
commit 6a3a763f71
2 changed files with 22 additions and 5 deletions

View file

@ -691,7 +691,10 @@ class MeshService : Service(), Logging {
}.build()
}.build()
private val recentDataPackets = mutableListOf<DataPacket>()
// FIXME - possible kotlin bug in 1.3.72 - it seems that if we start with the (globally shared) emptyList,
// then adding items are affecting that shared list rather than a copy. This was causing aliasing of
// recentDataPackets with messages.value in the GUI. So if the current list is empty we are careful to make a new list
private var recentDataPackets = mutableListOf<DataPacket>()
/// Generate a DataPacket from a MeshPacket, or null if we didn't have enough data to do so
private fun toDataPacket(packet: MeshPacket): DataPacket? {
@ -741,9 +744,16 @@ class MeshService : Service(), Logging {
private fun rememberDataPacket(dataPacket: DataPacket) {
// discard old messages if needed then add the new one
while (recentDataPackets.size > 20) // FIXME, we should instead serialize this list to flash on shutdown
while (recentDataPackets.size > 50)
recentDataPackets.removeAt(0)
recentDataPackets.add(dataPacket)
// FIXME - possible kotlin bug in 1.3.72 - it seems that if we start with the (globally shared) emptyList,
// then adding items are affecting that shared list rather than a copy. This was causing aliasing of
// recentDataPackets with messages.value in the GUI. So if the current list is empty we are careful to make a new list
if (recentDataPackets.isEmpty())
recentDataPackets = mutableListOf(dataPacket)
else
recentDataPackets.add(dataPacket)
}
/// Update our model and resend as needed for a MeshPacket we just received from the radio