fix #86 the first message sent is duplicated bug

This commit is contained in:
geeksville 2020-08-12 12:48:43 -07:00
parent 93d4cd99fd
commit bc5d6cf808
2 changed files with 16 additions and 12 deletions

View file

@ -750,7 +750,7 @@ class MainActivity : AppCompatActivity(), Logging,
// Init our messages table with the service's record of past text messages
val msgs = service.oldMessages
debug("Service provided ${msgs.size} messages")
model.messagesState.messages.value = msgs
model.messagesState.setMessages(msgs)
val connectionState =
MeshService.ConnectionState.valueOf(service.connectionState())

View file

@ -20,40 +20,44 @@ class MessagesState(private val ui: UIViewModel) : Logging {
)
)
/// This is the inner storage for messages
private val messagesList = (if (isEmulator) testTexts else emptyList()).toMutableList()
// 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 emptyList()) {
object : MutableLiveData<List<DataPacket>>(messagesList) {
}
fun setMessages(m: List<DataPacket>) {
messagesList.clear()
messagesList.addAll(m)
messages.value = messagesList
}
/// add a message our GUI list of past msgs
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.
// 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
messagesList.add(m)
messages.value = messagesList
}
fun updateStatus(id: Int, status: MessageStatus) {
// Super inefficent but this is rare
debug("Handling message status change $id: $status")
val msgs = messages.value!!
msgs.find { it.id == id }?.let { p ->
messagesList.find { it.id == id }?.let { p ->
// Note: it seems that the service is keeping only a reference to our original packet (so it has already updated p.status)
// This seems to be an AIDL optimization when both the service and the client are in the same process. But we still want to trigger
// a GUI update
// if (p.status != status) {
p.status = status
// Trigger an expensive complete redraw FIXME
messages.value = msgs
messages.value = messagesList
// }
}
}