refactor: remove NodeDB `users` StateFlow

pull/1243/head
andrekir 2024-09-11 18:53:50 -03:00
rodzic 84cfe4f990
commit eeb3363a88
2 zmienionych plików z 4 dodań i 11 usunięć

Wyświetl plik

@ -3,7 +3,6 @@ package com.geeksville.mesh.model
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope import androidx.lifecycle.coroutineScope
import com.geeksville.mesh.MeshProtos import com.geeksville.mesh.MeshProtos
import com.geeksville.mesh.MeshUser
import com.geeksville.mesh.MyNodeInfo import com.geeksville.mesh.MyNodeInfo
import com.geeksville.mesh.NodeInfo import com.geeksville.mesh.NodeInfo
import com.geeksville.mesh.database.dao.NodeInfoDao import com.geeksville.mesh.database.dao.NodeInfoDao
@ -44,9 +43,6 @@ class NodeDB @Inject constructor(
val nodeDBbyID: StateFlow<Map<String, NodeInfo>> get() = _nodeDBbyID val nodeDBbyID: StateFlow<Map<String, NodeInfo>> get() = _nodeDBbyID
val nodes get() = nodeDBbyID val nodes get() = nodeDBbyID
private val _users = MutableStateFlow<Map<String, MeshUser?>>(mapOf())
val users: StateFlow<Map<String, MeshUser?>> get() = _users
init { init {
nodeInfoDao.getMyNodeInfo().onEach { _myNodeInfo.value = it } nodeInfoDao.getMyNodeInfo().onEach { _myNodeInfo.value = it }
.launchIn(processLifecycle.coroutineScope) .launchIn(processLifecycle.coroutineScope)
@ -60,7 +56,6 @@ class NodeDB @Inject constructor(
nodeInfoDao.nodeDBbyID().onEach { nodeInfoDao.nodeDBbyID().onEach {
_nodeDBbyID.value = it _nodeDBbyID.value = it
_users.value = it.mapValues { node -> node.value.user }
} }
.launchIn(processLifecycle.coroutineScope) .launchIn(processLifecycle.coroutineScope)
} }

Wyświetl plik

@ -315,11 +315,9 @@ class UIViewModel @Inject constructor(
initialValue = emptyList(), initialValue = emptyList(),
) )
fun getMessagesFrom(contactKey: String) = combine( @OptIn(ExperimentalCoroutinesApi::class)
nodeDB.users, fun getMessagesFrom(contactKey: String) = packetRepository.getMessagesFrom(contactKey).mapLatest { list ->
packetRepository.getMessagesFrom(contactKey), list.map {
) { users, packets ->
packets.map {
val defaultUser = MeshUser( val defaultUser = MeshUser(
it.data.from ?: DataPacket.ID_LOCAL, it.data.from ?: DataPacket.ID_LOCAL,
app.getString(R.string.unknown_username), app.getString(R.string.unknown_username),
@ -329,7 +327,7 @@ class UIViewModel @Inject constructor(
Message( Message(
uuid = it.uuid, uuid = it.uuid,
receivedTime = it.received_time, receivedTime = it.received_time,
user = users[it.data.from] ?: defaultUser, user = nodeDB.nodes.value[it.data.from]?.user ?: defaultUser,
text = it.data.text.orEmpty(), text = it.data.text.orEmpty(),
time = it.data.time, time = it.data.time,
read = it.read, read = it.read,