kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
refactor: unified `getUser` repository call for convention
rodzic
e05c35afc2
commit
5cf9741b13
|
@ -2,6 +2,8 @@ package com.geeksville.mesh.model
|
|||
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.coroutineScope
|
||||
import com.geeksville.mesh.DataPacket
|
||||
import com.geeksville.mesh.MeshProtos
|
||||
import com.geeksville.mesh.database.dao.NodeInfoDao
|
||||
import com.geeksville.mesh.database.entity.MyNodeEntity
|
||||
import com.geeksville.mesh.database.entity.NodeEntity
|
||||
|
@ -35,9 +37,15 @@ class NodeDB @Inject constructor(
|
|||
private val _nodeDBbyNum = MutableStateFlow<Map<Int, NodeEntity>>(mapOf())
|
||||
val nodeDBbyNum: StateFlow<Map<Int, NodeEntity>> get() = _nodeDBbyNum
|
||||
|
||||
fun getUser(userId: String?) = userId?.let { id ->
|
||||
nodeDBbyNum.value.values.find { it.user.id == id }?.user
|
||||
}
|
||||
fun getUser(nodeNum: Int): MeshProtos.User = getUser(DataPacket.nodeNumToDefaultId(nodeNum))
|
||||
|
||||
fun getUser(userId: String): MeshProtos.User =
|
||||
nodeDBbyNum.value.values.find { it.user.id == userId }?.user
|
||||
?: MeshProtos.User.newBuilder()
|
||||
.setLongName("Meshtastic ${userId.takeLast(n = 4)}")
|
||||
.setShortName(userId.takeLast(n = 4))
|
||||
.setHwModel(MeshProtos.HardwareModel.UNSET)
|
||||
.build()
|
||||
|
||||
init {
|
||||
nodeInfoDao.getMyNodeInfo().onEach { _myNodeInfo.value = it }
|
||||
|
|
|
@ -251,12 +251,7 @@ class UIViewModel @Inject constructor(
|
|||
fun updateMapCenterAndZoom(center: GeoPoint, zoom: Double) =
|
||||
_mapState.update { it.copy(center = center, zoom = zoom) }
|
||||
|
||||
fun getUser(userId: String?) = nodeDB.getUser(userId) ?: user {
|
||||
id = userId.orEmpty()
|
||||
longName = app.getString(R.string.unknown_username)
|
||||
shortName = app.getString(R.string.unknown_node_short_name)
|
||||
hwModel = MeshProtos.HardwareModel.UNSET
|
||||
}
|
||||
fun getUser(userId: String?) = nodeDB.getUser(userId ?: DataPacket.ID_BROADCAST)
|
||||
|
||||
private val _snackbarText = MutableLiveData<Any?>(null)
|
||||
val snackbarText: LiveData<Any?> get() = _snackbarText
|
||||
|
|
|
@ -56,6 +56,8 @@ class RadioConfigRepository @Inject constructor(
|
|||
*/
|
||||
val nodeDBbyNum: StateFlow<Map<Int, NodeEntity>> get() = nodeDB.nodeDBbyNum
|
||||
|
||||
fun getUser(nodeNum: Int) = nodeDB.getUser(nodeNum)
|
||||
|
||||
suspend fun upsert(node: NodeEntity) = nodeDB.upsert(node)
|
||||
suspend fun installNodeDB(mi: MyNodeEntity, nodes: List<NodeEntity>) {
|
||||
nodeDB.installNodeDB(mi, nodes)
|
||||
|
|
|
@ -440,12 +440,8 @@ class MeshService : Service(), Logging {
|
|||
}
|
||||
}
|
||||
|
||||
private fun getUserName(num: Int): String {
|
||||
val user = nodeDBbyNodeNum[num]?.user
|
||||
val longName = user?.longName ?: getString(R.string.unknown_username)
|
||||
val shortName = user?.shortName ?: DataPacket.nodeNumToDefaultId(num)
|
||||
return "$longName ($shortName)"
|
||||
}
|
||||
private fun getUserName(num: Int): String =
|
||||
with(radioConfigRepository.getUser(num)) { "$longName ($shortName)" }
|
||||
|
||||
private val numNodes get() = nodeDBbyNodeNum.size
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue