mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-02-05 13:22:59 -05:00
refactor: getContacts() DAO using Map return type
This commit is contained in:
@@ -18,6 +18,8 @@ class PacketRepository @Inject constructor(private val packetDaoLazy: dagger.Laz
|
||||
packetDao.getAllPackets()
|
||||
}
|
||||
|
||||
fun getContacts(): Flow<Map<String, Packet>> = packetDao.getContactKeys()
|
||||
|
||||
suspend fun getQueuedPackets(): List<DataPacket>? = withContext(Dispatchers.IO) {
|
||||
packetDao.getQueuedPackets()
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.geeksville.mesh.database.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.MapColumn
|
||||
import androidx.room.Update
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
@@ -16,6 +17,9 @@ interface PacketDao {
|
||||
@Query("Select * from packet order by received_time asc")
|
||||
fun getAllPackets(): Flow<List<Packet>>
|
||||
|
||||
@Query("Select * from packet where port_num = 1 order by received_time desc")
|
||||
fun getContactKeys(): Flow<Map<@MapColumn(columnName = "contact_key") String, Packet>>
|
||||
|
||||
@Insert
|
||||
fun insert(packet: Packet)
|
||||
|
||||
|
||||
@@ -198,9 +198,14 @@ class UIViewModel @Inject constructor(
|
||||
}.asLiveData()
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
val contacts: LiveData<Map<String, Packet>> = _packets.mapLatest { list ->
|
||||
list.filter { it.port_num == Portnums.PortNum.TEXT_MESSAGE_APP_VALUE }
|
||||
.associateBy { packet -> packet.contact_key }
|
||||
val contacts: LiveData<Map<String, Packet>> = packetRepository.getContacts().mapLatest {
|
||||
// Add empty channel placeholders (always show Broadcast contacts, even when empty)
|
||||
val placeholder = (0 until channelSet.settingsCount).associate { ch ->
|
||||
val contactKey = "$ch${DataPacket.ID_BROADCAST}"
|
||||
val data = DataPacket(bytes = null, dataType = 1, time = 0L, channel = ch)
|
||||
contactKey to Packet(0L, 1, contactKey, 0L, data)
|
||||
}
|
||||
it + (placeholder - it.keys)
|
||||
}.asLiveData()
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
|
||||
@@ -171,16 +171,7 @@ class ContactsFragment : ScreenFragment("Messages"), Logging {
|
||||
}
|
||||
|
||||
fun onContactsChanged(contacts: Map<String, Packet>) {
|
||||
// Add empty channel placeholders (always show Broadcast contacts, even when empty)
|
||||
val mutableMap = contacts.toMutableMap()
|
||||
for (ch in 0 until model.channelSet.settingsCount) {
|
||||
val contactKey = "$ch${DataPacket.ID_BROADCAST}"
|
||||
if (mutableMap[contactKey] == null) mutableMap[contactKey] = Packet(
|
||||
0L, 1, contactKey, 0L,
|
||||
DataPacket(bytes = null, dataType = 1, time = 0L, channel = ch)
|
||||
)
|
||||
}
|
||||
this.contacts = mutableMap.values.sortedByDescending { it.received_time }.toTypedArray()
|
||||
this.contacts = contacts.values.toTypedArray()
|
||||
notifyDataSetChanged() // FIXME, this is super expensive and redraws all nodes
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user