refactor(config): move destination node logic to ViewModel

pull/1330/head
andrekir 2024-10-16 12:53:22 -03:00 zatwierdzone przez Andre K
rodzic 2fab9d83f8
commit 42f9ef24fd
2 zmienionych plików z 51 dodań i 50 usunięć

Wyświetl plik

@ -150,10 +150,10 @@ class RadioConfigViewModel @Inject constructor(
} }
fun setOwner(user: MeshProtos.User) { fun setOwner(user: MeshProtos.User) {
setRemoteOwner(myNodeNum ?: return, user) setRemoteOwner(destNode.value?.num ?: return, user)
} }
fun setRemoteOwner(destNum: Int, user: MeshProtos.User) = request( private fun setRemoteOwner(destNum: Int, user: MeshProtos.User) = request(
destNum, destNum,
{ service, packetId, _ -> { service, packetId, _ ->
_radioConfigState.update { it.copy(userConfig = user) } _radioConfigState.update { it.copy(userConfig = user) }
@ -169,10 +169,10 @@ class RadioConfigViewModel @Inject constructor(
) )
fun updateChannels( fun updateChannels(
destNum: Int,
new: List<ChannelProtos.ChannelSettings>, new: List<ChannelProtos.ChannelSettings>,
old: List<ChannelProtos.ChannelSettings>, old: List<ChannelProtos.ChannelSettings>,
) { ) {
val destNum = destNode.value?.num ?: return
getChannelList(new, old).forEach { setRemoteChannel(destNum, it) } getChannelList(new, old).forEach { setRemoteChannel(destNum, it) }
if (destNum == myNodeNum) viewModelScope.launch { if (destNum == myNodeNum) viewModelScope.launch {
@ -184,7 +184,7 @@ class RadioConfigViewModel @Inject constructor(
private fun setChannels(channelUrl: String) = viewModelScope.launch { private fun setChannels(channelUrl: String) = viewModelScope.launch {
val new = Uri.parse(channelUrl).toChannelSet() val new = Uri.parse(channelUrl).toChannelSet()
val old = radioConfigRepository.channelSetFlow.firstOrNull() ?: return@launch val old = radioConfigRepository.channelSetFlow.firstOrNull() ?: return@launch
updateChannels(myNodeNum ?: return@launch, new.settingsList, old.settingsList) updateChannels(new.settingsList, old.settingsList)
} }
private fun setRemoteChannel(destNum: Int, channel: ChannelProtos.Channel) = request( private fun setRemoteChannel(destNum: Int, channel: ChannelProtos.Channel) = request(
@ -201,7 +201,11 @@ class RadioConfigViewModel @Inject constructor(
"Request getChannel error" "Request getChannel error"
) )
fun setRemoteConfig(destNum: Int, config: ConfigProtos.Config) = request( fun setConfig(config: ConfigProtos.Config) {
setRemoteConfig(destNode.value?.num ?: return, config)
}
private fun setRemoteConfig(destNum: Int, config: ConfigProtos.Config) = request(
destNum, destNum,
{ service, packetId, dest -> { service, packetId, dest ->
_radioConfigState.update { it.copy(radioConfig = config) } _radioConfigState.update { it.copy(radioConfig = config) }
@ -216,7 +220,11 @@ class RadioConfigViewModel @Inject constructor(
"Request getConfig error", "Request getConfig error",
) )
fun setModuleConfig(destNum: Int, config: ModuleConfigProtos.ModuleConfig) = request( fun setModuleConfig(config: ModuleConfigProtos.ModuleConfig) {
setModuleConfig(destNode.value?.num ?: return, config)
}
private fun setModuleConfig(destNum: Int, config: ModuleConfigProtos.ModuleConfig) = request(
destNum, destNum,
{ service, packetId, dest -> { service, packetId, dest ->
_radioConfigState.update { it.copy(moduleConfig = config) } _radioConfigState.update { it.copy(moduleConfig = config) }
@ -231,7 +239,8 @@ class RadioConfigViewModel @Inject constructor(
"Request getModuleConfig error", "Request getModuleConfig error",
) )
fun setRingtone(destNum: Int, ringtone: String) { fun setRingtone(ringtone: String) {
val destNum = destNode.value?.num ?: return
_radioConfigState.update { it.copy(ringtone = ringtone) } _radioConfigState.update { it.copy(ringtone = ringtone) }
meshService?.setRingtone(destNum, ringtone) meshService?.setRingtone(destNum, ringtone)
} }
@ -242,7 +251,8 @@ class RadioConfigViewModel @Inject constructor(
"Request getRingtone error" "Request getRingtone error"
) )
fun setCannedMessages(destNum: Int, messages: String) { fun setCannedMessages(messages: String) {
val destNum = destNode.value?.num ?: return
_radioConfigState.update { it.copy(cannedMessageMessages = messages) } _radioConfigState.update { it.copy(cannedMessageMessages = messages) }
meshService?.setCannedMessages(destNum, messages) meshService?.setCannedMessages(destNum, messages)
} }
@ -305,7 +315,8 @@ class RadioConfigViewModel @Inject constructor(
} }
} }
fun setFixedPosition(destNum: Int, position: Position) { fun setFixedPosition(position: Position) {
val destNum = destNode.value?.num ?: return
try { try {
meshService?.setFixedPosition(destNum, position) meshService?.setFixedPosition(destNum, position)
} catch (ex: RemoteException) { } catch (ex: RemoteException) {
@ -313,16 +324,7 @@ class RadioConfigViewModel @Inject constructor(
} }
} }
fun removeFixedPosition(destNum: Int) = setFixedPosition(destNum, Position(0.0, 0.0, 0)) 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)
}
fun setModuleConfig(config: ModuleConfigProtos.ModuleConfig) {
setModuleConfig(myNodeNum ?: return, config)
}
private val _deviceProfile = MutableStateFlow<DeviceProfile?>(null) private val _deviceProfile = MutableStateFlow<DeviceProfile?>(null)
val deviceProfile: StateFlow<DeviceProfile?> get() = _deviceProfile val deviceProfile: StateFlow<DeviceProfile?> get() = _deviceProfile
@ -391,7 +393,7 @@ class RadioConfigViewModel @Inject constructor(
} }
} }
if (hasFixedPosition()) { if (hasFixedPosition()) {
setFixedPosition(myNodeNum!!, Position(fixedPosition)) setFixedPosition(Position(fixedPosition))
} }
if (hasModuleConfig()) { if (hasModuleConfig()) {
val descriptor = ModuleConfigProtos.ModuleConfig.getDescriptor() val descriptor = ModuleConfigProtos.ModuleConfig.getDescriptor()

Wyświetl plik

@ -250,8 +250,7 @@ fun RadioConfigNavHost(
val connectionState by viewModel.connectionState.collectAsStateWithLifecycle() val connectionState by viewModel.connectionState.collectAsStateWithLifecycle()
val connected = connectionState == ConnectionState.CONNECTED && node != null val connected = connectionState == ConnectionState.CONNECTED && node != null
val destNum = node?.num ?: 0 val isLocal = node?.num == viewModel.myNodeNum
val isLocal = destNum == viewModel.myNodeNum
val radioConfigState by viewModel.radioConfigState.collectAsStateWithLifecycle() val radioConfigState by viewModel.radioConfigState.collectAsStateWithLifecycle()
@ -288,7 +287,7 @@ fun RadioConfigNavHost(
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE) addCategory(Intent.CATEGORY_OPENABLE)
type = "application/*" type = "application/*"
putExtra(Intent.EXTRA_TITLE, "${destNum.toUInt()}.cfg") putExtra(Intent.EXTRA_TITLE, "${node!!.num.toUInt()}.cfg")
} }
exportConfigLauncher.launch(intent) exportConfigLauncher.launch(intent)
} }
@ -348,7 +347,7 @@ fun RadioConfigNavHost(
userConfig = radioConfigState.userConfig, userConfig = radioConfigState.userConfig,
enabled = connected, enabled = connected,
onSaveClicked = { userInput -> onSaveClicked = { userInput ->
viewModel.setRemoteOwner(destNum, userInput) viewModel.setOwner(userInput)
} }
) )
} }
@ -359,7 +358,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
maxChannels = viewModel.maxChannels, maxChannels = viewModel.maxChannels,
onPositiveClicked = { channelListInput -> onPositiveClicked = { channelListInput ->
viewModel.updateChannels(destNum, channelListInput, radioConfigState.channelList) viewModel.updateChannels(channelListInput, radioConfigState.channelList)
}, },
) )
} }
@ -369,7 +368,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { deviceInput -> onSaveClicked = { deviceInput ->
val config = config { device = deviceInput } val config = config { device = deviceInput }
viewModel.setRemoteConfig(destNum, config) viewModel.setConfig(config)
} }
) )
} }
@ -387,16 +386,16 @@ fun RadioConfigNavHost(
onSaveClicked = { locationInput, positionInput -> onSaveClicked = { locationInput, positionInput ->
if (positionInput.fixedPosition) { if (positionInput.fixedPosition) {
if (locationInput != currentPosition) { if (locationInput != currentPosition) {
viewModel.setFixedPosition(destNum, locationInput) viewModel.setFixedPosition(locationInput)
} }
} else { } else {
if (radioConfigState.radioConfig.position.fixedPosition) { if (radioConfigState.radioConfig.position.fixedPosition) {
// fixed position changed from enabled to disabled // fixed position changed from enabled to disabled
viewModel.removeFixedPosition(destNum) viewModel.removeFixedPosition()
} }
} }
val config = config { position = positionInput } val config = config { position = positionInput }
viewModel.setRemoteConfig(destNum, config) viewModel.setConfig(config)
} }
) )
} }
@ -406,7 +405,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { powerInput -> onSaveClicked = { powerInput ->
val config = config { power = powerInput } val config = config { power = powerInput }
viewModel.setRemoteConfig(destNum, config) viewModel.setConfig(config)
} }
) )
} }
@ -416,7 +415,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { networkInput -> onSaveClicked = { networkInput ->
val config = config { network = networkInput } val config = config { network = networkInput }
viewModel.setRemoteConfig(destNum, config) viewModel.setConfig(config)
} }
) )
} }
@ -426,7 +425,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { displayInput -> onSaveClicked = { displayInput ->
val config = config { display = displayInput } val config = config { display = displayInput }
viewModel.setRemoteConfig(destNum, config) viewModel.setConfig(config)
} }
) )
} }
@ -437,7 +436,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { loraInput -> onSaveClicked = { loraInput ->
val config = config { lora = loraInput } val config = config { lora = loraInput }
viewModel.setRemoteConfig(destNum, config) viewModel.setConfig(config)
}, },
hasPaFan = viewModel.hasPaFan, hasPaFan = viewModel.hasPaFan,
) )
@ -448,7 +447,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { bluetoothInput -> onSaveClicked = { bluetoothInput ->
val config = config { bluetooth = bluetoothInput } val config = config { bluetooth = bluetoothInput }
viewModel.setRemoteConfig(destNum, config) viewModel.setConfig(config)
} }
) )
} }
@ -458,7 +457,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onConfirm = { securityInput -> onConfirm = { securityInput ->
val config = config { security = securityInput } val config = config { security = securityInput }
viewModel.setRemoteConfig(destNum, config) viewModel.setConfig(config)
} }
) )
} }
@ -468,7 +467,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { mqttInput -> onSaveClicked = { mqttInput ->
val config = moduleConfig { mqtt = mqttInput } val config = moduleConfig { mqtt = mqttInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -478,7 +477,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { serialInput -> onSaveClicked = { serialInput ->
val config = moduleConfig { serial = serialInput } val config = moduleConfig { serial = serialInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -489,11 +488,11 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { ringtoneInput, extNotificationInput -> onSaveClicked = { ringtoneInput, extNotificationInput ->
if (ringtoneInput != radioConfigState.ringtone) { if (ringtoneInput != radioConfigState.ringtone) {
viewModel.setRingtone(destNum, ringtoneInput) viewModel.setRingtone(ringtoneInput)
} }
if (extNotificationInput != radioConfigState.moduleConfig.externalNotification) { if (extNotificationInput != radioConfigState.moduleConfig.externalNotification) {
val config = moduleConfig { externalNotification = extNotificationInput } val config = moduleConfig { externalNotification = extNotificationInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
} }
) )
@ -504,7 +503,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { storeForwardInput -> onSaveClicked = { storeForwardInput ->
val config = moduleConfig { storeForward = storeForwardInput } val config = moduleConfig { storeForward = storeForwardInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -514,7 +513,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { rangeTestInput -> onSaveClicked = { rangeTestInput ->
val config = moduleConfig { rangeTest = rangeTestInput } val config = moduleConfig { rangeTest = rangeTestInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -524,7 +523,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { telemetryInput -> onSaveClicked = { telemetryInput ->
val config = moduleConfig { telemetry = telemetryInput } val config = moduleConfig { telemetry = telemetryInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -535,11 +534,11 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { messagesInput, cannedMessageInput -> onSaveClicked = { messagesInput, cannedMessageInput ->
if (messagesInput != radioConfigState.cannedMessageMessages) { if (messagesInput != radioConfigState.cannedMessageMessages) {
viewModel.setCannedMessages(destNum, messagesInput) viewModel.setCannedMessages(messagesInput)
} }
if (cannedMessageInput != radioConfigState.moduleConfig.cannedMessage) { if (cannedMessageInput != radioConfigState.moduleConfig.cannedMessage) {
val config = moduleConfig { cannedMessage = cannedMessageInput } val config = moduleConfig { cannedMessage = cannedMessageInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
} }
) )
@ -550,7 +549,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { audioInput -> onSaveClicked = { audioInput ->
val config = moduleConfig { audio = audioInput } val config = moduleConfig { audio = audioInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -560,7 +559,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { remoteHardwareInput -> onSaveClicked = { remoteHardwareInput ->
val config = moduleConfig { remoteHardware = remoteHardwareInput } val config = moduleConfig { remoteHardware = remoteHardwareInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -570,7 +569,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { neighborInfoInput -> onSaveClicked = { neighborInfoInput ->
val config = moduleConfig { neighborInfo = neighborInfoInput } val config = moduleConfig { neighborInfo = neighborInfoInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -580,7 +579,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { ambientLightingInput -> onSaveClicked = { ambientLightingInput ->
val config = moduleConfig { ambientLighting = ambientLightingInput } val config = moduleConfig { ambientLighting = ambientLightingInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -590,7 +589,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { detectionSensorInput -> onSaveClicked = { detectionSensorInput ->
val config = moduleConfig { detectionSensor = detectionSensorInput } val config = moduleConfig { detectionSensor = detectionSensorInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }
@ -600,7 +599,7 @@ fun RadioConfigNavHost(
enabled = connected, enabled = connected,
onSaveClicked = { paxcounterConfigInput -> onSaveClicked = { paxcounterConfigInput ->
val config = moduleConfig { paxcounter = paxcounterConfigInput } val config = moduleConfig { paxcounter = paxcounterConfigInput }
viewModel.setModuleConfig(destNum, config) viewModel.setModuleConfig(config)
} }
) )
} }