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

@@ -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<List<DataPacket>>(if (isEmulator) testTexts else listOf()) {
object : MutableLiveData<List<DataPacket>>(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) {

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