Meshtastic-Android/app/src/main/java/com/geeksville/mesh/model/NodeDB.kt

69 lines
2.1 KiB
Kotlin
Raw Normal View History

2020-02-17 13:34:52 -08:00
package com.geeksville.mesh.model
2022-04-22 17:22:06 -03:00
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.asLiveData
2021-03-14 11:42:04 +08:00
import com.geeksville.mesh.MeshProtos
2020-02-17 13:34:52 -08:00
import com.geeksville.mesh.MeshUser
import com.geeksville.mesh.NodeInfo
import com.geeksville.mesh.Position
import kotlinx.coroutines.flow.MutableStateFlow
/// NodeDB lives inside the UIViewModel, but it needs a backpointer to reach the service
class NodeDB(private val ui: UIViewModel) {
2020-02-17 13:34:52 -08:00
private val testPositions = arrayOf(
2022-03-26 17:44:59 -03:00
Position(32.776665, -96.796989, 35, 123), // dallas
Position(32.960758, -96.733521, 35, 456), // richardson
Position(32.912901, -96.781776, 35, 789) // north dallas
2020-02-17 13:34:52 -08:00
)
val testNodeNoPosition = NodeInfo(
8,
MeshUser(
"+16508765308".format(8),
"Kevin MesterNoLoc",
2021-03-14 11:42:04 +08:00
"KLO",
MeshProtos.HardwareModel.ANDROID_SIM,
false
2020-02-17 13:34:52 -08:00
),
2020-02-19 10:53:36 -08:00
null
2020-02-17 13:34:52 -08:00
)
2022-04-22 17:22:06 -03:00
private val testNodes = testPositions.mapIndexed { index, it ->
2020-02-17 13:34:52 -08:00
NodeInfo(
9 + index,
MeshUser(
"+165087653%02d".format(9 + index),
"Kevin Mester$index",
2021-03-14 11:42:04 +08:00
"KM$index",
MeshProtos.HardwareModel.ANDROID_SIM,
false
2020-02-17 13:34:52 -08:00
),
2020-02-19 10:53:36 -08:00
it
2020-02-17 13:34:52 -08:00
)
}.associateBy { it.user?.id!! }
2020-02-17 13:34:52 -08:00
private val seedWithTestNodes = false
// The unique userId of our node
2022-04-22 17:22:06 -03:00
private val _myId = MutableLiveData<String?>(if (seedWithTestNodes) "+16508765309" else null)
val myId: LiveData<String?> get() = _myId
fun setMyId(myId: String?) {
_myId.value = myId
}
2020-02-17 13:34:52 -08:00
// A map from userId to NodeInfo
private val _nodes = MutableStateFlow(if (seedWithTestNodes) testNodes else mapOf())
val nodes: LiveData<Map<String, NodeInfo>> = _nodes.asLiveData()
2023-04-16 06:16:41 -03:00
val nodesByNum get() = nodes.value?.values?.associateBy { it.num }
2022-04-22 17:22:06 -03:00
fun setNodes(nodes: Map<String, NodeInfo>) {
_nodes.value = nodes
}
fun setNodes(list: List<NodeInfo>) {
setNodes(list.associateBy { it.user?.id!! })
}
}