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) {
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,
{ service, packetId, _ ->
_radioConfigState.update { it.copy(userConfig = user) }
@ -169,10 +169,10 @@ class RadioConfigViewModel @Inject constructor(
)
fun updateChannels(
destNum: Int,
new: List<ChannelProtos.ChannelSettings>,
old: List<ChannelProtos.ChannelSettings>,
) {
val destNum = destNode.value?.num ?: return
getChannelList(new, old).forEach { setRemoteChannel(destNum, it) }
if (destNum == myNodeNum) viewModelScope.launch {
@ -184,7 +184,7 @@ class RadioConfigViewModel @Inject constructor(
private fun setChannels(channelUrl: String) = viewModelScope.launch {
val new = Uri.parse(channelUrl).toChannelSet()
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(
@ -201,7 +201,11 @@ class RadioConfigViewModel @Inject constructor(
"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,
{ service, packetId, dest ->
_radioConfigState.update { it.copy(radioConfig = config) }
@ -216,7 +220,11 @@ class RadioConfigViewModel @Inject constructor(
"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,
{ service, packetId, dest ->
_radioConfigState.update { it.copy(moduleConfig = config) }
@ -231,7 +239,8 @@ class RadioConfigViewModel @Inject constructor(
"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) }
meshService?.setRingtone(destNum, ringtone)
}
@ -242,7 +251,8 @@ class RadioConfigViewModel @Inject constructor(
"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) }
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 {
meshService?.setFixedPosition(destNum, position)
} catch (ex: RemoteException) {
@ -313,16 +324,7 @@ class RadioConfigViewModel @Inject constructor(
}
}
fun removeFixedPosition(destNum: Int) = setFixedPosition(destNum, 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)
}
fun removeFixedPosition() = setFixedPosition(Position(0.0, 0.0, 0))
private val _deviceProfile = MutableStateFlow<DeviceProfile?>(null)
val deviceProfile: StateFlow<DeviceProfile?> get() = _deviceProfile
@ -391,7 +393,7 @@ class RadioConfigViewModel @Inject constructor(
}
}
if (hasFixedPosition()) {
setFixedPosition(myNodeNum!!, Position(fixedPosition))
setFixedPosition(Position(fixedPosition))
}
if (hasModuleConfig()) {
val descriptor = ModuleConfigProtos.ModuleConfig.getDescriptor()

Wyświetl plik

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