mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: add fixed position admin messages
This commit is contained in:
parent
2c916f88ca
commit
6e3e173096
4 changed files with 33 additions and 15 deletions
|
|
@ -70,8 +70,8 @@ class RadioConfigViewModel @Inject constructor(
|
|||
// A map from nodeNum to NodeInfo
|
||||
val nodes: StateFlow<Map<Int, NodeInfo>> get() = radioConfigRepository.nodeDBbyNum
|
||||
|
||||
private val _myNodeInfo = MutableStateFlow<MyNodeInfo?>(null)
|
||||
val myNodeInfo get() = _myNodeInfo
|
||||
val myNodeInfo: StateFlow<MyNodeInfo?> get() = radioConfigRepository.myNodeInfo
|
||||
val ourNodeInfo: StateFlow<NodeInfo?> get() = radioConfigRepository.ourNodeInfo
|
||||
|
||||
private val requestIds = MutableStateFlow<HashMap<Int, Boolean>>(hashMapOf())
|
||||
private val _radioConfigState = MutableStateFlow(RadioConfigState())
|
||||
|
|
@ -81,10 +81,6 @@ class RadioConfigViewModel @Inject constructor(
|
|||
val currentDeviceProfile get() = _currentDeviceProfile.value
|
||||
|
||||
init {
|
||||
radioConfigRepository.myNodeInfoFlow().onEach {
|
||||
_myNodeInfo.value = it
|
||||
}.launchIn(viewModelScope)
|
||||
|
||||
radioConfigRepository.deviceProfileFlow.onEach {
|
||||
_currentDeviceProfile.value = it
|
||||
}.launchIn(viewModelScope)
|
||||
|
|
@ -100,7 +96,6 @@ class RadioConfigViewModel @Inject constructor(
|
|||
|
||||
val myNodeNum get() = myNodeInfo.value?.myNodeNum
|
||||
val maxChannels get() = myNodeInfo.value?.maxChannels ?: 8
|
||||
private val ourNodeInfo: NodeInfo? get() = nodes.value[myNodeNum]
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
|
|
@ -263,14 +258,16 @@ class RadioConfigViewModel @Inject constructor(
|
|||
"Request NodeDB reset error"
|
||||
)
|
||||
|
||||
fun requestPosition(destNum: Int, position: Position = Position(0.0, 0.0, 0)) {
|
||||
fun setFixedPosition(position: Position) {
|
||||
try {
|
||||
meshService?.requestPosition(destNum, position)
|
||||
meshService?.requestPosition(myNodeNum ?: return, position)
|
||||
} catch (ex: RemoteException) {
|
||||
errormsg("Request position error: ${ex.message}")
|
||||
}
|
||||
}
|
||||
|
||||
fun removeFixedPosition() = setFixedPosition(Position(0.0, 0.0, 0))
|
||||
|
||||
// Set the radio config (also updates our saved copy in preferences)
|
||||
fun setConfig(config: ConfigProtos.Config) {
|
||||
setRemoteConfig(myNodeNum ?: return, config)
|
||||
|
|
@ -323,7 +320,7 @@ class RadioConfigViewModel @Inject constructor(
|
|||
fun installProfile(protobuf: DeviceProfile) = with(protobuf) {
|
||||
_deviceProfile.value = null
|
||||
// meshService?.beginEditSettings()
|
||||
if (hasLongName() || hasShortName()) ourNodeInfo?.user?.let {
|
||||
if (hasLongName() || hasShortName()) ourNodeInfo.value?.user?.let {
|
||||
val user = it.copy(
|
||||
longName = if (hasLongName()) longName else it.longName,
|
||||
shortName = if (hasShortName()) shortName else it.shortName
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ class RadioConfigRepository @Inject constructor(
|
|||
fun myNodeInfoFlow(): Flow<MyNodeInfo?> = nodeDB.myNodeInfoFlow()
|
||||
suspend fun getMyNodeInfo(): MyNodeInfo? = myNodeInfoFlow().firstOrNull()
|
||||
val myNodeInfo: StateFlow<MyNodeInfo?> get() = nodeDB.myNodeInfo
|
||||
val ourNodeInfo: StateFlow<NodeInfo?> get() = nodeDB.ourNodeInfo
|
||||
|
||||
val nodeDBbyNum: StateFlow<Map<Int, NodeInfo>> get() = nodeDB.nodeDBbyNum
|
||||
val nodeDBbyID: StateFlow<Map<String, NodeInfo>> get() = nodeDB.nodeDBbyID
|
||||
|
|
|
|||
|
|
@ -1864,13 +1864,24 @@ class MeshService : Service(), Logging {
|
|||
}
|
||||
|
||||
override fun requestPosition(destNum: Int, position: Position) = toRemoteExceptions {
|
||||
if (position == Position(0.0, 0.0, 0)) {
|
||||
if (destNum != myNodeNum) {
|
||||
// request position
|
||||
sendPosition(destNum = destNum, wantResponse = true)
|
||||
} else {
|
||||
// send fixed position (local only/no remote method, so we force destNum to null)
|
||||
val (lat, lon, alt) = position
|
||||
sendPosition(destNum = null, lat = lat, lon = lon, alt = alt)
|
||||
sendToRadio(newMeshPacketTo(destNum).buildAdminPacket {
|
||||
if (position != Position(0.0, 0.0, 0)) {
|
||||
setFixedPosition = position {
|
||||
longitudeI = Position.degI(lon)
|
||||
latitudeI = Position.degI(lat)
|
||||
altitude = alt
|
||||
}
|
||||
} else {
|
||||
removeFixedPosition = true
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -194,6 +194,8 @@ sealed class ResponseState<out T> {
|
|||
data class Loading(var total: Int = 1, var completed: Int = 0) : ResponseState<Nothing>()
|
||||
data class Success<T>(val result: T) : ResponseState<T>()
|
||||
data class Error(val error: String) : ResponseState<Nothing>()
|
||||
|
||||
fun isWaiting() = this !is Empty
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
|
@ -237,7 +239,7 @@ fun RadioConfigNavHost(
|
|||
var location by remember(node) { mutableStateOf(node?.position) } // FIXME
|
||||
|
||||
val deviceProfile by viewModel.deviceProfile.collectAsStateWithLifecycle()
|
||||
val isWaiting = radioConfigState.responseState !is ResponseState.Empty
|
||||
val isWaiting = radioConfigState.responseState.isWaiting()
|
||||
var showEditDeviceProfileDialog by remember { mutableStateOf(false) }
|
||||
|
||||
val importConfigLauncher = rememberLauncherForActivityResult(
|
||||
|
|
@ -401,9 +403,16 @@ fun RadioConfigNavHost(
|
|||
positionConfig = radioConfigState.radioConfig.position,
|
||||
enabled = connected,
|
||||
onSaveClicked = { locationInput, positionInput ->
|
||||
if (locationInput != location && positionInput.fixedPosition) {
|
||||
locationInput?.let { viewModel.requestPosition(destNum, it) }
|
||||
location = locationInput
|
||||
if (positionInput.fixedPosition) {
|
||||
if (locationInput != null && locationInput != location) {
|
||||
viewModel.setFixedPosition(locationInput)
|
||||
location = locationInput
|
||||
}
|
||||
} else {
|
||||
if (radioConfigState.radioConfig.position.fixedPosition) {
|
||||
// fixed position changed from enabled to disabled
|
||||
viewModel.removeFixedPosition()
|
||||
}
|
||||
}
|
||||
val config = config { position = positionInput }
|
||||
viewModel.setRemoteConfig(destNum, config)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue