diff --git a/app/src/main/java/com/geeksville/mesh/model/MessagesState.kt b/app/src/main/java/com/geeksville/mesh/model/MessagesState.kt index cdb4c353b..a69541dd2 100644 --- a/app/src/main/java/com/geeksville/mesh/model/MessagesState.kt +++ b/app/src/main/java/com/geeksville/mesh/model/MessagesState.kt @@ -23,7 +23,7 @@ class MessagesState(private val ui: UIViewModel) : Logging { // If the following (unused otherwise) line is commented out, the IDE preview window works. // if left in the preview always renders as empty. val messages = - object : MutableLiveData>(if (isEmulator) testTexts else listOf()) { + object : MutableLiveData>(if (isEmulator) testTexts else emptyList()) { } @@ -31,7 +31,14 @@ class MessagesState(private val ui: UIViewModel) : Logging { fun addMessage(m: DataPacket) { debug("Adding message to view id=${m.id}") // FIXME - don't just slam in a new list each time, it probably causes extra drawing. - messages.value = messages.value!! + m + + // 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 + messages.value = if (messages.value.isNullOrEmpty()) + listOf(m) + else + messages.value!! + m } fun updateStatus(id: Int, status: MessageStatus) { diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index 6ee667a09..32cb2638c 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -691,7 +691,10 @@ class MeshService : Service(), Logging { }.build() }.build() - private val recentDataPackets = mutableListOf() + // 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() /// 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