mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: convert ourNodeInfo to Flow
This commit is contained in:
parent
4c0d804531
commit
9e8900ec4f
5 changed files with 15 additions and 22 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package com.geeksville.mesh.model
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MediatorLiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.geeksville.mesh.MeshProtos
|
||||
import com.geeksville.mesh.MeshUser
|
||||
|
|
@ -61,16 +60,4 @@ class NodeDB(private val ui: UIViewModel) {
|
|||
fun setNodes(nodes: Map<String, NodeInfo>) {
|
||||
_nodes.value = nodes
|
||||
}
|
||||
|
||||
/// Could be null if we haven't received our node DB yet
|
||||
val ourNodeInfo = MediatorLiveData<NodeInfo?>()
|
||||
|
||||
init {
|
||||
ourNodeInfo.addSource(_nodes) { updatedValue ->
|
||||
ourNodeInfo.value = updatedValue[_myId.value]
|
||||
}
|
||||
ourNodeInfo.addSource(_myId) { updatedValue ->
|
||||
ourNodeInfo.value = _nodes.value?.get(updatedValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ import android.net.Uri
|
|||
import android.os.RemoteException
|
||||
import android.view.Menu
|
||||
import androidx.core.content.edit
|
||||
import androidx.lifecycle.asFlow
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
|
@ -33,6 +34,7 @@ import com.geeksville.mesh.util.positionToMeter
|
|||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
|
@ -89,6 +91,10 @@ class UIViewModel @Inject constructor(
|
|||
private val preferences: SharedPreferences
|
||||
) : ViewModel(), Logging {
|
||||
|
||||
var actionBarMenu: Menu? = null
|
||||
var meshService: IMeshService? = null
|
||||
val nodeDB = NodeDB(this)
|
||||
|
||||
private val _meshLog = MutableStateFlow<List<MeshLog>>(emptyList())
|
||||
val meshLog: StateFlow<List<MeshLog>> = _meshLog
|
||||
|
||||
|
|
@ -109,6 +115,9 @@ class UIViewModel @Inject constructor(
|
|||
private val _quickChatActions = MutableStateFlow<List<QuickChatAction>>(emptyList())
|
||||
val quickChatActions: StateFlow<List<QuickChatAction>> = _quickChatActions
|
||||
|
||||
private val _ourNodeInfo = MutableStateFlow<NodeInfo?>(null)
|
||||
val ourNodeInfo: StateFlow<NodeInfo?> = _ourNodeInfo
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
meshLogRepository.getAllLogs().collect { logs ->
|
||||
|
|
@ -134,6 +143,9 @@ class UIViewModel @Inject constructor(
|
|||
channelSetRepository.channelSetFlow.onEach { channelSet ->
|
||||
_channels.value = ChannelSet(channelSet)
|
||||
}.launchIn(viewModelScope)
|
||||
combine(nodeDB.nodes.asFlow(), nodeDB.myId.asFlow()) { nodes, id -> nodes[id] }.onEach {
|
||||
_ourNodeInfo.value = it
|
||||
}.launchIn(viewModelScope)
|
||||
debug("ViewModel created")
|
||||
}
|
||||
|
||||
|
|
@ -226,12 +238,6 @@ class UIViewModel @Inject constructor(
|
|||
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
var actionBarMenu: Menu? = null
|
||||
|
||||
var meshService: IMeshService? = null
|
||||
|
||||
val nodeDB = NodeDB(this)
|
||||
|
||||
/// Connection state to our radio device
|
||||
private val _connectionState = MutableLiveData(MeshService.ConnectionState.DISCONNECTED)
|
||||
val connectionState: LiveData<MeshService.ConnectionState> get() = _connectionState
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel) {
|
|||
val connected = connectionState == MeshService.ConnectionState.CONNECTED
|
||||
|
||||
val localConfig by viewModel.localConfig.collectAsState()
|
||||
val ourNodeInfo by viewModel.nodeDB.ourNodeInfo.observeAsState()
|
||||
val ourNodeInfo by viewModel.ourNodeInfo.collectAsState()
|
||||
var userInput by remember(ourNodeInfo?.user) { mutableStateOf(ourNodeInfo?.user) }
|
||||
var positionInfo by remember(ourNodeInfo?.position) { mutableStateOf(ourNodeInfo?.position) }
|
||||
|
||||
|
|
|
|||
|
|
@ -506,7 +506,7 @@ class MapFragment : ScreenFragment("Map Fragment"), Logging, View.OnClickListene
|
|||
marker = MarkerWithLabel(map, label)
|
||||
marker.title = "${it.longName} ${node.batteryStr}"
|
||||
marker.snippet = model.gpsString(p)
|
||||
model.nodeDB.nodes.value?.get(model.nodeDB.myId.value)?.let { our ->
|
||||
model.ourNodeInfo.value?.let { our ->
|
||||
our.distanceStr(node)?.let { dist ->
|
||||
marker.subDescription = getString(R.string.map_subDescription).format(
|
||||
our.bearing(node),
|
||||
|
|
|
|||
|
|
@ -221,7 +221,7 @@ class UsersFragment : ScreenFragment("Users"), Logging {
|
|||
holder.coordsView.visibility = View.INVISIBLE
|
||||
}
|
||||
|
||||
val ourNodeInfo = model.nodeDB.nodes.value?.get(model.nodeDB.myId.value)
|
||||
val ourNodeInfo = model.ourNodeInfo.value
|
||||
val distance = ourNodeInfo?.distanceStr(n)
|
||||
if (distance != null) {
|
||||
holder.distanceView.text = distance
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue