From 84939a74d2fe0902b8a783db800bf0e8b6290366 Mon Sep 17 00:00:00 2001 From: andrekir Date: Wed, 11 Sep 2024 20:01:16 -0300 Subject: [PATCH] refactor: replace `nodeDBbyID` with `getUser()` function --- .../mesh/database/dao/NodeInfoDao.kt | 3 --- .../java/com/geeksville/mesh/model/NodeDB.kt | 13 +++------- .../java/com/geeksville/mesh/model/UIState.kt | 25 +++++++++++-------- .../datastore/RadioConfigRepository.kt | 3 --- .../com/geeksville/mesh/ui/map/MapFragment.kt | 4 +-- 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/database/dao/NodeInfoDao.kt b/app/src/main/java/com/geeksville/mesh/database/dao/NodeInfoDao.kt index 8f2b55b0..eda4a116 100644 --- a/app/src/main/java/com/geeksville/mesh/database/dao/NodeInfoDao.kt +++ b/app/src/main/java/com/geeksville/mesh/database/dao/NodeInfoDao.kt @@ -26,9 +26,6 @@ interface NodeInfoDao { @Query("SELECT * FROM NodeInfo ORDER BY CASE WHEN num = (SELECT myNodeNum FROM MyNodeInfo LIMIT 1) THEN 0 ELSE 1 END, lastHeard DESC") fun nodeDBbyNum(): Flow> - @Query("SELECT * FROM NodeInfo") - fun nodeDBbyID(): Flow> - @Query( """ WITH OurNode AS ( diff --git a/app/src/main/java/com/geeksville/mesh/model/NodeDB.kt b/app/src/main/java/com/geeksville/mesh/model/NodeDB.kt index 67633435..8c14a7c5 100644 --- a/app/src/main/java/com/geeksville/mesh/model/NodeDB.kt +++ b/app/src/main/java/com/geeksville/mesh/model/NodeDB.kt @@ -36,12 +36,10 @@ class NodeDB @Inject constructor( // A map from nodeNum to NodeInfo private val _nodeDBbyNum = MutableStateFlow>(mapOf()) val nodeDBbyNum: StateFlow> get() = _nodeDBbyNum - val nodesByNum get() = nodeDBbyNum.value - // A map from userId to NodeInfo - private val _nodeDBbyID = MutableStateFlow>(mapOf()) - val nodeDBbyID: StateFlow> get() = _nodeDBbyID - val nodes get() = nodeDBbyID + fun getUser(userId: String?) = userId?.let { id -> + nodeDBbyNum.value.values.find { it.user?.id == id }?.user + } init { nodeInfoDao.getMyNodeInfo().onEach { _myNodeInfo.value = it } @@ -53,11 +51,6 @@ class NodeDB @Inject constructor( _ourNodeInfo.value = ourNodeInfo _myId.value = ourNodeInfo?.user?.id }.launchIn(processLifecycle.coroutineScope) - - nodeInfoDao.nodeDBbyID().onEach { - _nodeDBbyID.value = it - } - .launchIn(processLifecycle.coroutineScope) } fun getNodes( diff --git a/app/src/main/java/com/geeksville/mesh/model/UIState.kt b/app/src/main/java/com/geeksville/mesh/model/UIState.kt index 1fb12863..4c404bd0 100644 --- a/app/src/main/java/com/geeksville/mesh/model/UIState.kt +++ b/app/src/main/java/com/geeksville/mesh/model/UIState.kt @@ -150,10 +150,11 @@ internal fun getShortDateTime(time: Long): String? { } } +@Suppress("LongParameterList") @HiltViewModel class UIViewModel @Inject constructor( private val app: Application, - val nodeDB: NodeDB, + private val nodeDB: NodeDB, private val radioConfigRepository: RadioConfigRepository, private val radioInterfaceService: RadioInterfaceService, private val meshLogRepository: MeshLogRepository, @@ -238,6 +239,14 @@ class UIViewModel @Inject constructor( // hardware info about our local device (can be null) val myNodeInfo: StateFlow get() = nodeDB.myNodeInfo val ourNodeInfo: StateFlow get() = nodeDB.ourNodeInfo + val nodesByNum get() = nodeDB.nodeDBbyNum.value // FIXME only used in MapFragment + + fun getUser(userId: String?) = nodeDB.getUser(userId) ?: MeshUser( + userId ?: DataPacket.ID_LOCAL, + app.getString(R.string.unknown_username), + app.getString(R.string.unknown_node_short_name), + MeshProtos.HardwareModel.UNSET, + ) private val _snackbarText = MutableLiveData(null) val snackbarText: LiveData get() = _snackbarText @@ -289,13 +298,13 @@ class UIViewModel @Inject constructor( val toBroadcast = data.to == DataPacket.ID_BROADCAST // grab usernames from NodeInfo - val node = nodeDB.nodes.value[if (fromLocal) data.to else data.from] + val user = getUser(if (fromLocal) data.to else data.from) - val shortName = node?.user?.shortName ?: app.getString(R.string.unknown_node_short_name) + val shortName = user.shortName val longName = if (toBroadcast) { channelSet.getChannel(data.channel)?.name ?: app.getString(R.string.channel_name) } else { - node?.user?.longName ?: app.getString(R.string.unknown_username) + user.longName } Contact( @@ -318,16 +327,10 @@ class UIViewModel @Inject constructor( @OptIn(ExperimentalCoroutinesApi::class) fun getMessagesFrom(contactKey: String) = packetRepository.getMessagesFrom(contactKey).mapLatest { list -> list.map { - val defaultUser = MeshUser( - it.data.from ?: DataPacket.ID_LOCAL, - app.getString(R.string.unknown_username), - app.getString(R.string.unknown_node_short_name), - MeshProtos.HardwareModel.UNSET, - ) Message( uuid = it.uuid, receivedTime = it.received_time, - user = nodeDB.nodes.value[it.data.from]?.user ?: defaultUser, + user = getUser(it.data.from), text = it.data.text.orEmpty(), time = it.data.time, read = it.read, diff --git a/app/src/main/java/com/geeksville/mesh/repository/datastore/RadioConfigRepository.kt b/app/src/main/java/com/geeksville/mesh/repository/datastore/RadioConfigRepository.kt index 5ce9842c..52ee5c6a 100644 --- a/app/src/main/java/com/geeksville/mesh/repository/datastore/RadioConfigRepository.kt +++ b/app/src/main/java/com/geeksville/mesh/repository/datastore/RadioConfigRepository.kt @@ -53,10 +53,7 @@ class RadioConfigRepository @Inject constructor( fun myNodeInfoFlow(): Flow = nodeDB.myNodeInfoFlow() suspend fun getMyNodeInfo(): MyNodeInfo? = myNodeInfoFlow().firstOrNull() val myNodeInfo: StateFlow get() = nodeDB.myNodeInfo - val ourNodeInfo: StateFlow get() = nodeDB.ourNodeInfo - val nodeDBbyNum: StateFlow> get() = nodeDB.nodeDBbyNum - val nodeDBbyID: StateFlow> get() = nodeDB.nodeDBbyID /** * Flow representing the [NodeInfo] database. diff --git a/app/src/main/java/com/geeksville/mesh/ui/map/MapFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/map/MapFragment.kt index 5585db7a..c4596453 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/map/MapFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/map/MapFragment.kt @@ -383,7 +383,7 @@ fun MapView( fun getUsername(id: String?) = if (id == DataPacket.ID_LOCAL) { context.getString(R.string.you) } else { - model.nodeDB.nodes.value[id]?.user?.longName ?: context.getString(R.string.unknown_username) + model.getUser(id).longName } fun MapView.onWaypointChanged(waypoints: Collection): List { @@ -467,7 +467,7 @@ fun MapView( } fun MapView.zoomToNodes() { - val nodeMarkers = onNodesChanged(model.nodeDB.nodesByNum.values) + val nodeMarkers = onNodesChanged(model.nodesByNum.values) if (nodeMarkers.isNotEmpty()) { val box = BoundingBox.fromGeoPoints(nodeMarkers.map { it.position }) val center = GeoPoint(box.centerLatitude, box.centerLongitude)