feat: support for switching between devices (#1078)

This commit is contained in:
Andre K 2024-06-08 10:25:47 -03:00 committed by GitHub
parent 9ba44ad087
commit 5b3c78316b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 934 additions and 206 deletions

View file

@ -31,8 +31,9 @@ import com.geeksville.mesh.database.entity.QuickChatAction
AutoMigration (from = 4, to = 5),
AutoMigration (from = 5, to = 6),
AutoMigration (from = 6, to = 7),
AutoMigration (from = 7, to = 8),
],
version = 7,
version = 8,
exportSchema = true,
)
@TypeConverters(Converters::class)

View file

@ -2,6 +2,7 @@ package com.geeksville.mesh.database
import com.geeksville.mesh.DataPacket
import com.geeksville.mesh.MessageStatus
import com.geeksville.mesh.Portnums.PortNum
import com.geeksville.mesh.database.dao.PacketDao
import com.geeksville.mesh.database.entity.ContactSettings
import com.geeksville.mesh.database.entity.Packet
@ -15,12 +16,14 @@ class PacketRepository @Inject constructor(private val packetDaoLazy: dagger.Laz
packetDaoLazy.get()
}
suspend fun getAllPackets(): Flow<List<Packet>> = withContext(Dispatchers.IO) {
packetDao.getAllPackets()
}
fun getWaypoints(): Flow<List<Packet>> = packetDao.getAllPackets(PortNum.WAYPOINT_APP_VALUE)
fun getContacts(): Flow<Map<String, Packet>> = packetDao.getContactKeys()
suspend fun getMessageCount(contact: String): Int = withContext(Dispatchers.IO) {
packetDao.getMessageCount(contact)
}
suspend fun getQueuedPackets(): List<DataPacket>? = withContext(Dispatchers.IO) {
packetDao.getQueuedPackets()
}
@ -29,9 +32,7 @@ class PacketRepository @Inject constructor(private val packetDaoLazy: dagger.Laz
packetDao.insert(packet)
}
suspend fun getMessagesFrom(contact: String) = withContext(Dispatchers.IO) {
packetDao.getMessagesFrom(contact)
}
fun getMessagesFrom(contact: String) = packetDao.getMessagesFrom(contact)
suspend fun updateMessageStatus(d: DataPacket, m: MessageStatus) = withContext(Dispatchers.IO) {
packetDao.updateMessageStatus(d, m)
@ -45,16 +46,16 @@ class PacketRepository @Inject constructor(private val packetDaoLazy: dagger.Laz
packetDao.getDataPacketById(requestId)
}
suspend fun deleteAllMessages() = withContext(Dispatchers.IO) {
packetDao.deleteAllMessages()
}
suspend fun deleteMessages(uuidList: List<Long>) = withContext(Dispatchers.IO) {
for (chunk in uuidList.chunked(500)) { // limit number of UUIDs per query
packetDao.deleteMessages(chunk)
}
}
suspend fun deleteContacts(contactList: List<String>) = withContext(Dispatchers.IO) {
packetDao.deleteContacts(contactList)
}
suspend fun deleteWaypoint(id: Int) = withContext(Dispatchers.IO) {
packetDao.deleteWaypoint(id)
}

View file

@ -16,28 +16,70 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface PacketDao {
@Query("Select * from packet order by received_time asc")
fun getAllPackets(): Flow<List<Packet>>
@Query(
"""
SELECT * FROM packet
WHERE (myNodeNum = 0 OR myNodeNum = (SELECT myNodeNum FROM MyNodeInfo))
AND port_num = :portNum
ORDER BY received_time ASC
"""
)
fun getAllPackets(portNum: Int): Flow<List<Packet>>
@Query("Select * from packet where port_num = 1 order by received_time desc")
@Query(
"""
SELECT * FROM packet
WHERE (myNodeNum = 0 OR myNodeNum = (SELECT myNodeNum FROM MyNodeInfo))
AND port_num = 1
ORDER BY received_time DESC
"""
)
fun getContactKeys(): Flow<Map<@MapColumn(columnName = "contact_key") String, Packet>>
@Query(
"""
SELECT COUNT(*) FROM packet
WHERE (myNodeNum = 0 OR myNodeNum = (SELECT myNodeNum FROM MyNodeInfo))
AND port_num = 1 AND contact_key = :contact
"""
)
suspend fun getMessageCount(contact: String): Int
@Insert
fun insert(packet: Packet)
@Query("Select * from packet where port_num = 1 and contact_key = :contact order by received_time asc")
@Query(
"""
SELECT * FROM packet
WHERE (myNodeNum = 0 OR myNodeNum = (SELECT myNodeNum FROM MyNodeInfo))
AND port_num = 1 AND contact_key = :contact
ORDER BY received_time ASC
"""
)
fun getMessagesFrom(contact: String): Flow<List<Packet>>
@Query("Select * from packet where data = :data")
@Query(
"""
SELECT * FROM packet
WHERE (myNodeNum = 0 OR myNodeNum = (SELECT myNodeNum FROM MyNodeInfo))
AND data = :data
"""
)
fun findDataPacket(data: DataPacket): Packet?
@Query("Delete from packet where port_num = 1")
fun deleteAllMessages()
@Query("Delete from packet where uuid in (:uuidList)")
@Query("DELETE FROM packet WHERE uuid in (:uuidList)")
fun deleteMessages(uuidList: List<Long>)
@Query("Delete from packet where uuid=:uuid")
@Query(
"""
DELETE FROM packet
WHERE (myNodeNum = 0 OR myNodeNum = (SELECT myNodeNum FROM MyNodeInfo))
AND contact_key IN (:contactList)
"""
)
fun deleteContacts(contactList: List<String>)
@Query("DELETE FROM packet WHERE uuid=:uuid")
fun _delete(uuid: Long)
@Transaction
@ -60,7 +102,13 @@ interface PacketDao {
findDataPacket(data)?.let { update(it.copy(data = new)) }
}
@Query("Select data from packet order by received_time asc")
@Query(
"""
SELECT data FROM packet
WHERE (myNodeNum = 0 OR myNodeNum = (SELECT myNodeNum FROM MyNodeInfo))
ORDER BY received_time ASC
"""
)
fun getDataPackets(): List<DataPacket>
@Transaction
@ -72,7 +120,14 @@ interface PacketDao {
fun getQueuedPackets(): List<DataPacket>? =
getDataPackets().filter { it.status == MessageStatus.QUEUED }
@Query("Select * from packet where port_num = 8 order by received_time asc")
@Query(
"""
SELECT * FROM packet
WHERE (myNodeNum = 0 OR myNodeNum = (SELECT myNodeNum FROM MyNodeInfo))
AND port_num = 8
ORDER BY received_time ASC
"""
)
fun getAllWaypoints(): List<Packet>
@Transaction

View file

@ -2,15 +2,26 @@ package com.geeksville.mesh.database.entity
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import com.geeksville.mesh.DataPacket
@Entity(tableName = "packet")
@Entity(
tableName = "packet",
indices = [
Index(value = ["myNodeNum"]),
Index(value = ["port_num"]),
Index(value = ["contact_key"]),
]
)
data class Packet(
@PrimaryKey(autoGenerate = true) val uuid: Long,
@ColumnInfo(name = "myNodeNum", defaultValue = "0") val myNodeNum: Int,
@ColumnInfo(name = "port_num") val port_num: Int,
@ColumnInfo(name = "contact_key") val contact_key: String,
@ColumnInfo(name = "received_time") val received_time: Long,
@ColumnInfo(name = "read", defaultValue = "1") val read: Boolean,
@ColumnInfo(name = "data") val data: DataPacket
)