kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
refactor(config): move navigation route logic to ViewModel
rodzic
700d8d3460
commit
2fab9d83f8
|
@ -24,6 +24,7 @@ import com.geeksville.mesh.moduleConfig
|
|||
import com.geeksville.mesh.repository.datastore.RadioConfigRepository
|
||||
import com.geeksville.mesh.ui.AdminRoute
|
||||
import com.geeksville.mesh.ui.ConfigRoute
|
||||
import com.geeksville.mesh.ui.ModuleRoute
|
||||
import com.geeksville.mesh.ui.ResponseState
|
||||
import com.google.protobuf.MessageLite
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
|
@ -161,7 +162,7 @@ class RadioConfigViewModel @Inject constructor(
|
|||
"Request setOwner error",
|
||||
)
|
||||
|
||||
fun getOwner(destNum: Int) = request(
|
||||
private fun getOwner(destNum: Int) = request(
|
||||
destNum,
|
||||
{ service, packetId, dest -> service.getRemoteOwner(packetId, dest) },
|
||||
"Request getOwner error"
|
||||
|
@ -194,7 +195,7 @@ class RadioConfigViewModel @Inject constructor(
|
|||
"Request setRemoteChannel error"
|
||||
)
|
||||
|
||||
fun getChannel(destNum: Int, index: Int) = request(
|
||||
private fun getChannel(destNum: Int, index: Int) = request(
|
||||
destNum,
|
||||
{ service, packetId, dest -> service.getRemoteChannel(packetId, dest, index) },
|
||||
"Request getChannel error"
|
||||
|
@ -209,7 +210,7 @@ class RadioConfigViewModel @Inject constructor(
|
|||
"Request setConfig error",
|
||||
)
|
||||
|
||||
fun getConfig(destNum: Int, configType: Int) = request(
|
||||
private fun getConfig(destNum: Int, configType: Int) = request(
|
||||
destNum,
|
||||
{ service, packetId, dest -> service.getRemoteConfig(packetId, dest, configType) },
|
||||
"Request getConfig error",
|
||||
|
@ -224,7 +225,7 @@ class RadioConfigViewModel @Inject constructor(
|
|||
"Request setConfig error",
|
||||
)
|
||||
|
||||
fun getModuleConfig(destNum: Int, configType: Int) = request(
|
||||
private fun getModuleConfig(destNum: Int, configType: Int) = request(
|
||||
destNum,
|
||||
{ service, packetId, dest -> service.getModuleConfig(packetId, dest, configType) },
|
||||
"Request getModuleConfig error",
|
||||
|
@ -235,7 +236,7 @@ class RadioConfigViewModel @Inject constructor(
|
|||
meshService?.setRingtone(destNum, ringtone)
|
||||
}
|
||||
|
||||
fun getRingtone(destNum: Int) = request(
|
||||
private fun getRingtone(destNum: Int) = request(
|
||||
destNum,
|
||||
{ service, packetId, dest -> service.getRingtone(packetId, dest) },
|
||||
"Request getRingtone error"
|
||||
|
@ -246,7 +247,7 @@ class RadioConfigViewModel @Inject constructor(
|
|||
meshService?.setCannedMessages(destNum, messages)
|
||||
}
|
||||
|
||||
fun getCannedMessages(destNum: Int) = request(
|
||||
private fun getCannedMessages(destNum: Int) = request(
|
||||
destNum,
|
||||
{ service, packetId, dest -> service.getCannedMessages(packetId, dest) },
|
||||
"Request getCannedMessages error"
|
||||
|
@ -295,7 +296,7 @@ class RadioConfigViewModel @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun getSessionPasskey(destNum: Int) {
|
||||
private fun getSessionPasskey(destNum: Int) {
|
||||
if (radioConfigState.value.hasMetadata()) {
|
||||
sendAdminRequest(destNum)
|
||||
} else {
|
||||
|
@ -409,13 +410,43 @@ class RadioConfigViewModel @Inject constructor(
|
|||
_radioConfigState.update { it.copy(responseState = ResponseState.Empty) }
|
||||
}
|
||||
|
||||
fun setResponseStateLoading(route: String) {
|
||||
fun setResponseStateLoading(route: Enum<*>) {
|
||||
val destNum = destNode.value?.num ?: return
|
||||
|
||||
_radioConfigState.value = RadioConfigState(
|
||||
route = route,
|
||||
route = route.name,
|
||||
responseState = ResponseState.Loading(),
|
||||
)
|
||||
// channel editor is synchronous, so we don't use requestIds as total
|
||||
if (route == ConfigRoute.CHANNELS.name) setResponseStateTotal(maxChannels + 1)
|
||||
|
||||
when (route) {
|
||||
ConfigRoute.USER -> getOwner(destNum)
|
||||
|
||||
ConfigRoute.CHANNELS -> {
|
||||
getChannel(destNum, 0)
|
||||
getConfig(destNum, ConfigRoute.LORA.configType)
|
||||
// channel editor is synchronous, so we don't use requestIds as total
|
||||
setResponseStateTotal(maxChannels + 1)
|
||||
}
|
||||
|
||||
is AdminRoute -> getSessionPasskey(destNum)
|
||||
|
||||
is ConfigRoute -> {
|
||||
if (route == ConfigRoute.LORA) {
|
||||
getChannel(destNum, 0)
|
||||
}
|
||||
getConfig(destNum, route.configType)
|
||||
}
|
||||
|
||||
is ModuleRoute -> {
|
||||
if (route == ModuleRoute.CANNED_MESSAGE) {
|
||||
getCannedMessages(destNum)
|
||||
}
|
||||
if (route == ModuleRoute.EXTERNAL_NOTIFICATION) {
|
||||
getRingtone(destNum)
|
||||
}
|
||||
getModuleConfig(destNum, route.configType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setResponseStateTotal(total: Int) {
|
||||
|
|
|
@ -12,6 +12,7 @@ import androidx.annotation.StringRes
|
|||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
|
@ -203,13 +204,6 @@ enum class ModuleRoute(val title: String, val configType: Int = 0) {
|
|||
PAXCOUNTER("Paxcounter", 12),
|
||||
}
|
||||
|
||||
private fun getName(route: Any): String = when (route) {
|
||||
is AdminRoute -> route.name
|
||||
is ConfigRoute -> route.name
|
||||
is ModuleRoute -> route.name
|
||||
else -> ""
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic sealed class defines each possible state of a response.
|
||||
*/
|
||||
|
@ -327,58 +321,25 @@ fun RadioConfigNavHost(
|
|||
modifier = modifier,
|
||||
) {
|
||||
composable("home") {
|
||||
RadioSettingsScreen(
|
||||
RadioConfigItemList(
|
||||
enabled = connected && !isWaiting,
|
||||
isLocal = isLocal,
|
||||
onRouteClick = { route ->
|
||||
viewModel.setResponseStateLoading(getName(route))
|
||||
when (route) {
|
||||
ConfigRoute.USER -> {
|
||||
viewModel.getOwner(destNum)
|
||||
}
|
||||
|
||||
ConfigRoute.CHANNELS -> {
|
||||
viewModel.getChannel(destNum, 0)
|
||||
viewModel.getConfig(destNum, ConfigRoute.LORA.configType)
|
||||
}
|
||||
|
||||
"IMPORT" -> {
|
||||
viewModel.clearPacketResponse()
|
||||
viewModel.setDeviceProfile(null)
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
||||
addCategory(Intent.CATEGORY_OPENABLE)
|
||||
type = "application/*"
|
||||
}
|
||||
importConfigLauncher.launch(intent)
|
||||
}
|
||||
|
||||
"EXPORT" -> {
|
||||
viewModel.clearPacketResponse()
|
||||
viewModel.setDeviceProfile(null)
|
||||
showEditDeviceProfileDialog = true
|
||||
}
|
||||
|
||||
is AdminRoute -> {
|
||||
viewModel.getSessionPasskey(destNum)
|
||||
}
|
||||
|
||||
is ConfigRoute -> {
|
||||
if (route == ConfigRoute.LORA) {
|
||||
viewModel.getChannel(destNum, 0)
|
||||
}
|
||||
viewModel.getConfig(destNum, route.configType)
|
||||
}
|
||||
|
||||
is ModuleRoute -> {
|
||||
if (route == ModuleRoute.CANNED_MESSAGE) {
|
||||
viewModel.getCannedMessages(destNum)
|
||||
}
|
||||
if (route == ModuleRoute.EXTERNAL_NOTIFICATION) {
|
||||
viewModel.getRingtone(destNum)
|
||||
}
|
||||
viewModel.getModuleConfig(destNum, route.configType)
|
||||
}
|
||||
viewModel.setResponseStateLoading(route)
|
||||
},
|
||||
onImport = {
|
||||
viewModel.clearPacketResponse()
|
||||
viewModel.setDeviceProfile(null)
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
||||
addCategory(Intent.CATEGORY_OPENABLE)
|
||||
type = "application/*"
|
||||
}
|
||||
importConfigLauncher.launch(intent)
|
||||
},
|
||||
onExport = {
|
||||
viewModel.clearPacketResponse()
|
||||
viewModel.setDeviceProfile(null)
|
||||
showEditDeviceProfileDialog = true
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -746,13 +707,17 @@ private fun NavButton(@StringRes title: Int, enabled: Boolean, onClick: () -> Un
|
|||
}
|
||||
|
||||
@Composable
|
||||
private fun RadioSettingsScreen(
|
||||
private fun RadioConfigItemList(
|
||||
enabled: Boolean = true,
|
||||
isLocal: Boolean = true,
|
||||
onRouteClick: (Any) -> Unit = {},
|
||||
modifier: Modifier = Modifier,
|
||||
onRouteClick: (Enum<*>) -> Unit = {},
|
||||
onImport: () -> Unit = {},
|
||||
onExport: () -> Unit = {},
|
||||
) {
|
||||
LazyColumn(
|
||||
modifier = Modifier.padding(horizontal = 16.dp)
|
||||
modifier = modifier,
|
||||
contentPadding = PaddingValues(horizontal = 16.dp),
|
||||
) {
|
||||
item { PreferenceCategory(stringResource(R.string.device_settings)) }
|
||||
items(ConfigRoute.entries) { NavCard(it.title, enabled = enabled) { onRouteClick(it) } }
|
||||
|
@ -762,8 +727,8 @@ private fun RadioSettingsScreen(
|
|||
|
||||
if (isLocal) {
|
||||
item { PreferenceCategory("Import / Export") }
|
||||
item { NavCard("Import configuration", enabled = enabled) { onRouteClick("IMPORT") } }
|
||||
item { NavCard("Export configuration", enabled = enabled) { onRouteClick("EXPORT") } }
|
||||
item { NavCard("Import configuration", enabled = enabled) { onImport() } }
|
||||
item { NavCard("Export configuration", enabled = enabled) { onExport() } }
|
||||
}
|
||||
|
||||
items(AdminRoute.entries) { NavButton(it.title, enabled) { onRouteClick(it) } }
|
||||
|
@ -773,5 +738,5 @@ private fun RadioSettingsScreen(
|
|||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
private fun RadioSettingsScreenPreview() {
|
||||
RadioSettingsScreen()
|
||||
RadioConfigItemList()
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue