mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
feat: Add Jetpack Paging 3 support for messages and threads/contacts (#3795)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
parent
552097888f
commit
5e8c9794eb
12 changed files with 551 additions and 250 deletions
|
|
@ -17,14 +17,20 @@
|
|||
|
||||
package org.meshtastic.core.data.repository
|
||||
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.PagingData
|
||||
import androidx.paging.map
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.mapLatest
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.meshtastic.core.database.DatabaseManager
|
||||
import org.meshtastic.core.database.entity.ContactSettings
|
||||
import org.meshtastic.core.database.entity.Packet
|
||||
import org.meshtastic.core.database.entity.ReactionEntity
|
||||
import org.meshtastic.core.database.model.Message
|
||||
import org.meshtastic.core.database.model.Node
|
||||
import org.meshtastic.core.di.CoroutineDispatchers
|
||||
import org.meshtastic.core.model.DataPacket
|
||||
|
|
@ -45,12 +51,24 @@ constructor(
|
|||
fun getContacts(): Flow<Map<String, Packet>> =
|
||||
dbManager.currentDb.flatMapLatest { db -> db.packetDao().getContactKeys() }
|
||||
|
||||
fun getContactsPaged(): Flow<PagingData<Packet>> = Pager(
|
||||
config = PagingConfig(pageSize = 30, enablePlaceholders = false, initialLoadSize = 30),
|
||||
pagingSourceFactory = { dbManager.currentDb.value.packetDao().getContactKeysPaged() },
|
||||
)
|
||||
.flow
|
||||
|
||||
suspend fun getMessageCount(contact: String): Int =
|
||||
withContext(dispatchers.io) { dbManager.currentDb.value.packetDao().getMessageCount(contact) }
|
||||
|
||||
suspend fun getUnreadCount(contact: String): Int =
|
||||
withContext(dispatchers.io) { dbManager.currentDb.value.packetDao().getUnreadCount(contact) }
|
||||
|
||||
fun getFirstUnreadMessageUuid(contact: String): Flow<Long?> =
|
||||
dbManager.currentDb.flatMapLatest { db -> db.packetDao().getFirstUnreadMessageUuid(contact) }
|
||||
|
||||
fun hasUnreadMessages(contact: String): Flow<Boolean> =
|
||||
dbManager.currentDb.flatMapLatest { db -> db.packetDao().hasUnreadMessages(contact) }
|
||||
|
||||
fun getUnreadCountTotal(): Flow<Int> =
|
||||
dbManager.currentDb.flatMapLatest { db -> db.packetDao().getUnreadCountTotal() }
|
||||
|
||||
|
|
@ -92,6 +110,22 @@ constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun getMessagesFromPaged(contact: String, getNode: suspend (String?) -> Node): Flow<PagingData<Message>> = Pager(
|
||||
config = PagingConfig(pageSize = 50, enablePlaceholders = false, initialLoadSize = 50),
|
||||
pagingSourceFactory = { dbManager.currentDb.value.packetDao().getMessagesFromPaged(contact) },
|
||||
)
|
||||
.flow
|
||||
.map { pagingData ->
|
||||
pagingData.map { packet ->
|
||||
val message = packet.toMessage(getNode)
|
||||
message.replyId
|
||||
.takeIf { it != null && it != 0 }
|
||||
?.let { getPacketByPacketId(it) }
|
||||
?.toMessage(getNode)
|
||||
?.let { originalMessage -> message.copy(originalMessage = originalMessage) } ?: message
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateMessageStatus(d: DataPacket, m: MessageStatus) =
|
||||
withContext(dispatchers.io) { dbManager.currentDb.value.packetDao().updateMessageStatus(d, m) }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue