mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor messages to Room database
This commit is contained in:
parent
ab7bf4922b
commit
65e982ddd5
12 changed files with 191 additions and 279 deletions
|
|
@ -1,5 +1,7 @@
|
|||
package com.geeksville.mesh.database
|
||||
|
||||
import com.geeksville.mesh.DataPacket
|
||||
import com.geeksville.mesh.MessageStatus
|
||||
import com.geeksville.mesh.database.dao.PacketDao
|
||||
import com.geeksville.mesh.database.entity.Packet
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
|
@ -12,7 +14,7 @@ class PacketRepository @Inject constructor(private val packetDaoLazy: dagger.Laz
|
|||
packetDaoLazy.get()
|
||||
}
|
||||
|
||||
suspend fun getAll(): Flow<List<Packet>> = withContext(Dispatchers.IO) {
|
||||
suspend fun getAllPackets(): Flow<List<Packet>> = withContext(Dispatchers.IO) {
|
||||
packetDao.getAllPackets()
|
||||
}
|
||||
|
||||
|
|
@ -20,10 +22,21 @@ class PacketRepository @Inject constructor(private val packetDaoLazy: dagger.Laz
|
|||
packetDao.insert(packet)
|
||||
}
|
||||
|
||||
suspend fun deleteAll() = withContext(Dispatchers.IO) {
|
||||
packetDao.deleteAll()
|
||||
suspend fun getMessagesFrom(contact: String) = withContext(Dispatchers.IO) {
|
||||
packetDao.getMessagesFrom(contact)
|
||||
}
|
||||
|
||||
suspend fun updateMessageStatus(d: DataPacket, m: MessageStatus) = withContext(Dispatchers.IO) {
|
||||
packetDao.updateMessageStatus(d, m)
|
||||
}
|
||||
|
||||
suspend fun deleteAllMessages() = withContext(Dispatchers.IO) {
|
||||
packetDao.deleteAllMessages()
|
||||
}
|
||||
|
||||
suspend fun deleteMessages(uuidList: List<Long>) = withContext(Dispatchers.IO) {
|
||||
packetDao.deleteMessages(uuidList)
|
||||
}
|
||||
suspend fun delete(packet: Packet) = withContext(Dispatchers.IO) {
|
||||
packetDao.delete(packet)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import androidx.room.Insert
|
|||
import androidx.room.Update
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import com.geeksville.mesh.DataPacket
|
||||
import com.geeksville.mesh.MessageStatus
|
||||
import com.geeksville.mesh.database.entity.Packet
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
|
|
@ -17,8 +19,17 @@ interface PacketDao {
|
|||
@Insert
|
||||
fun insert(packet: Packet)
|
||||
|
||||
@Query("Delete from packet")
|
||||
fun deleteAll()
|
||||
@Query("Select * from packet where 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")
|
||||
fun findDataPacket(data: DataPacket): Packet
|
||||
|
||||
@Query("Delete from packet where port_num = 1")
|
||||
fun deleteAllMessages()
|
||||
|
||||
@Query("Delete from packet where uuid in (:uuidList)")
|
||||
fun deleteMessages(uuidList: List<Long>)
|
||||
|
||||
@Query("Delete from packet where uuid=:uuid")
|
||||
fun _delete(uuid: Long)
|
||||
|
|
@ -31,4 +42,9 @@ interface PacketDao {
|
|||
@Update
|
||||
fun update(packet: Packet)
|
||||
|
||||
@Transaction
|
||||
fun updateMessageStatus(data: DataPacket, m: MessageStatus) {
|
||||
val new = data.copy(status = m)
|
||||
update(findDataPacket(data).copy(data = new))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,16 +4,13 @@ import androidx.room.ColumnInfo
|
|||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import com.geeksville.mesh.DataPacket
|
||||
import com.geeksville.mesh.MessageStatus
|
||||
|
||||
@Entity(tableName = "packet")
|
||||
data class Packet(
|
||||
@PrimaryKey(autoGenerate = true) val uuid: Long,
|
||||
@ColumnInfo(name = "port_num") val port_num: Int,
|
||||
@ColumnInfo(name = "contact_id") val contact_id: String?,
|
||||
@ColumnInfo(name = "channel") val channel: Int,
|
||||
@ColumnInfo(name = "status") val status: MessageStatus = MessageStatus.UNKNOWN,
|
||||
@ColumnInfo(name = "contact_key") val contact_key: String,
|
||||
@ColumnInfo(name = "received_time") val received_time: Long,
|
||||
@ColumnInfo(name = "packet") val packet: DataPacket
|
||||
@ColumnInfo(name = "data") val data: DataPacket
|
||||
) {
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue