Added remote data output sections to SettingsScreen.kt

This commit is contained in:
Arty Bishop committed 2025-12-27 15:02:15 +00:00
1 parent a68e466040
commit a835ac4e07
8 files changed
+246 -129

No files matched your search

@@ -150,10 +150,10 @@ class RadarViewModel(
private fun sendPassDataBT(orbitalPos: OrbitalPos) {
viewModelScope.launch {
if (settingsRepo.getBluetoothState()) {
val btDevice = settingsRepo.getBluetoothAddress()
if (settingsRepo.rcSettings.value.bluetoothState) {
val btDevice = settingsRepo.rcSettings.value.bluetoothAddress
if (bluetoothReporter.isConnected()) {
val format = settingsRepo.getBluetoothFormat()
val format = settingsRepo.rcSettings.value.bluetoothFormat
val azimuth = orbitalPos.azimuth.toDegrees().round(0).toInt()
val elevation = orbitalPos.elevation.toDegrees().round(0).toInt()
bluetoothReporter.reportRotation(format, azimuth, elevation)
@@ -19,6 +19,7 @@ import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
@@ -42,6 +43,7 @@ import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import com.rtbishop.look4sat.R
import com.rtbishop.look4sat.domain.model.OtherSettings
import com.rtbishop.look4sat.domain.model.RCSettings
import com.rtbishop.look4sat.domain.predict.GeoPos
import com.rtbishop.look4sat.presentation.MainTheme
import com.rtbishop.look4sat.presentation.Screen
@@ -76,8 +78,10 @@ private fun SettingsScreen(uiState: SettingsState) {
else -> Manifest.permission.BLUETOOTH_CONNECT
}
val bluetoothRequest = rememberLauncherForActivityResult(bluetoothContract) { isGranted ->
uiState.sendRCAction(RCAction.SetBluetoothState(isGranted))
if (!isGranted) uiState.sendSystemAction(SystemAction.ShowToast(bluetoothError))
if (!isGranted) {
uiState.sendRCAction(RCAction.SetBluetoothState(false))
uiState.sendSystemAction(SystemAction.ShowToast(bluetoothError))
}
}
val locationContract = ActivityResultContracts.RequestMultiplePermissions()
val locationError = stringResource(R.string.location_gps_error)
@@ -123,6 +127,18 @@ private fun SettingsScreen(uiState: SettingsState) {
val updateFromFile = { contentRequest.launch("*/*") }
val clearAllData: () -> Unit = { uiState.sendAction(SettingsAction.ClearAllData) }
// RC settings
val rcSettings = uiState.rcSettings
val setRotatorState = { value: Boolean -> uiState.sendRCAction(RCAction.SetRotatorState(value)) }
val setRotatorAddress = { value: String -> uiState.sendRCAction(RCAction.SetRotatorAddress(value)) }
val setRotatorPort = { value: String -> uiState.sendRCAction(RCAction.SetRotatorPort(value)) }
val setBluetoothState = { value: Boolean ->
bluetoothRequest.launch(bluetoothPerm)
uiState.sendRCAction(RCAction.SetBluetoothState(value))
}
val setBluetoothAddress = { value: String -> uiState.sendRCAction(RCAction.SetBluetoothAddress(value)) }
val setBluetoothFormat = { value: String -> uiState.sendRCAction(RCAction.SetBluetoothFormat(value)) }
// Other settings
val otherSettings = uiState.otherSettings
val toggleUtc = { value: Boolean -> uiState.sendAction(SettingsAction.ToggleUtc(value)) }
@@ -177,6 +193,8 @@ private fun SettingsScreen(uiState: SettingsState) {
)
}
item { DataCard(dataSettings, updateFromWeb, updateFromFile, clearAllData) }
item { NetworkOutputCard(rcSettings, setRotatorState, setRotatorAddress, setRotatorPort) }
item { BluetoothOutputCard(rcSettings, setBluetoothState, setBluetoothAddress, setBluetoothFormat) }
item {
OtherCard(
otherSettings,
@@ -329,51 +347,132 @@ private fun DataCard(
}
}
//private fun setupRemoteCard() {
// binding.run {
// settingsRemote.remoteSwitch.apply {
// isChecked = viewModel.getRotatorEnabled()
// settingsRemote.remoteIp.isEnabled = isChecked
// settingsRemote.remoteIpEdit.setText(viewModel.getRotatorServer())
// settingsRemote.remotePort.isEnabled = isChecked
// settingsRemote.remotePortEdit.setText(viewModel.getRotatorPort())
// setOnCheckedChangeListener { _, isChecked ->
// viewModel.setRotatorEnabled(isChecked)
// settingsRemote.remoteIp.isEnabled = isChecked
// settingsRemote.remotePort.isEnabled = isChecked
// }
// }
// settingsRemote.remoteIpEdit.doOnTextChanged { text, _, _, _ ->
// if (text.toString().isValidIPv4()) viewModel.setRotatorServer(text.toString())
// }
// settingsRemote.remotePortEdit.doOnTextChanged { text, _, _, _ ->
// if (text.toString().isValidPort()) viewModel.setRotatorPort(text.toString())
// }
// }
//}
//
//private fun setupBTCard() {
// binding.run {
// settingsBtremote.BTremoteSwitch.apply {
// isChecked = viewModel.getBTEnabled()
// settingsBtremote.BTremoteAddress.isEnabled = isChecked
// settingsBtremote.BTAddressEdit.setText(viewModel.getBTDeviceAddr())
// settingsBtremote.BTremoteFormat.isEnabled = isChecked
// settingsBtremote.BTFormatEdit.setText(viewModel.getBTFormat())
// setOnCheckedChangeListener { _, isChecked ->
// toggleBTstate(isChecked)
// bluetoothRequest.launch(bluetooth)
// }
// }
// settingsBtremote.BTAddressEdit.doOnTextChanged { text, _, _, _ ->
// viewModel.setBTDeviceAddr(text.toString())
// }
// settingsBtremote.BTFormatEdit.doOnTextChanged { text, _, _, _ ->
// viewModel.setBTFormat(text.toString())
// }
// }
//}
//
@Preview(showBackground = true)
@Composable
private fun NetworkOutputCardPreview() = MainTheme {
val settings = RCSettings(
rotatorState = false,
rotatorAddress = "127.0.0.1",
rotatorPort = "4533",
bluetoothState = false,
bluetoothFormat = $$"W$AZ $EL",
bluetoothName = "Name",
bluetoothAddress = "00:0C:BF:13:80:5D"
)
NetworkOutputCard(settings)
}
@Composable
private fun NetworkOutputCard(
settings: RCSettings,
setRotatorState: (Boolean) -> Unit = {},
setRotatorAddress: (String) -> Unit = {},
setRotatorPort: (String) -> Unit = {}
) {
ElevatedCard(modifier = Modifier.fillMaxWidth()) {
Column(modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)) {
Text(
text = stringResource(id = R.string.remote_title),
color = MaterialTheme.colorScheme.primary
)
Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth()
) {
Text(text = stringResource(R.string.remote_switch))
Switch(checked = settings.rotatorState, onCheckedChange = { setRotatorState(it) })
}
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth()
) {
OutlinedTextField(
value = settings.rotatorAddress,
singleLine = true,
label = { Text(text = stringResource(R.string.remote_ip_hint)) },
onValueChange = { setRotatorAddress(it) },
modifier = Modifier.weight(1.5f),
enabled = settings.rotatorState
)
OutlinedTextField(
value = settings.rotatorPort,
onValueChange = { setRotatorPort(it) },
label = { Text(text = stringResource(R.string.remote_port_hint)) },
singleLine = true,
modifier = Modifier.weight(1f),
enabled = settings.rotatorState
)
}
Spacer(modifier = Modifier.height(4.dp))
}
}
}
@Preview(showBackground = true)
@Composable
private fun BluetoothOutputCardPreview() = MainTheme {
val settings = RCSettings(
rotatorState = false,
rotatorAddress = "127.0.0.1",
rotatorPort = "4533",
bluetoothState = false,
bluetoothFormat = $$"W$AZ $EL",
bluetoothName = "Name",
bluetoothAddress = "00:0C:BF:13:80:5D"
)
BluetoothOutputCard(settings)
}
@Composable
private fun BluetoothOutputCard(
settings: RCSettings,
setBluetoothState: (Boolean) -> Unit = {},
setBluetoothAddress: (String) -> Unit = {},
setBluetoothFormat: (String) -> Unit = {}
) {
ElevatedCard(modifier = Modifier.fillMaxWidth()) {
Column(modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)) {
Text(
text = stringResource(id = R.string.BTremote_title),
color = MaterialTheme.colorScheme.primary
)
Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth()
) {
Text(text = stringResource(R.string.BTremote_switch))
Switch(checked = settings.bluetoothState, onCheckedChange = { setBluetoothState(it) })
}
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth()
) {
OutlinedTextField(
value = settings.bluetoothAddress,
singleLine = true,
label = { Text(text = stringResource(R.string.BTremote_device_hint)) },
onValueChange = { setBluetoothAddress(it) },
modifier = Modifier.weight(1.5f),
enabled = settings.bluetoothState
)
OutlinedTextField(
value = settings.bluetoothFormat,
onValueChange = { setBluetoothFormat(it) },
label = { Text(text = stringResource(R.string.BTremote_output_hint)) },
singleLine = true,
modifier = Modifier.weight(1f),
enabled = settings.bluetoothState
)
}
Spacer(modifier = Modifier.height(4.dp))
}
}
}
@Preview(showBackground = true)
@Composable
private fun OtherCardPreview() = MainTheme {
@@ -1,6 +1,7 @@
package com.rtbishop.look4sat.presentation.settings
import com.rtbishop.look4sat.domain.model.OtherSettings
import com.rtbishop.look4sat.domain.model.RCSettings
import com.rtbishop.look4sat.domain.predict.GeoPos
import com.rtbishop.look4sat.domain.predict.OrbitalPass
@@ -12,16 +13,6 @@ data class DataSettings(
val isUpdating: Boolean, val entriesTotal: Int, val radiosTotal: Int, val timestamp: Long
)
data class RCSettings(
val rotatorState: Boolean,
val rotatorAddress: String,
val rotatorPort: String,
val bluetoothState: Boolean,
val bluetoothFormat: String,
val bluetoothName: String,
val bluetoothAddress: String,
)
data class SettingsState(
val nextPass: OrbitalPass,
val nextTime: String,
@@ -52,9 +43,6 @@ sealed class SettingsAction {
}
sealed class SystemAction {
data object OpenGitHub : SystemAction()
data object OpenDonate : SystemAction()
data object OpenFDroid : SystemAction()
data class ShowToast(val message: String) : SystemAction()
}
@@ -51,15 +51,6 @@ class SettingsViewModel(
private val defaultPosSettings = PositionSettings(false, settingsRepo.stationPosition.value, 0)
private val defaultDataSettings = DataSettings(false, 0, 0, 0L)
private val defaultRCSettings = RCSettings(
rotatorState = settingsRepo.getRotatorState(),
rotatorAddress = settingsRepo.getRotatorAddress(),
rotatorPort = settingsRepo.getRotatorPort(),
bluetoothState = settingsRepo.getBluetoothState(),
bluetoothFormat = settingsRepo.getBluetoothFormat(),
bluetoothName = settingsRepo.getBluetoothName(),
bluetoothAddress = settingsRepo.getBluetoothAddress()
)
private val _uiState = MutableStateFlow(
SettingsState(
nextTime = "00:00:00",
@@ -69,7 +60,7 @@ class SettingsViewModel(
positionSettings = defaultPosSettings,
dataSettings = defaultDataSettings,
otherSettings = settingsRepo.otherSettings.value,
rcSettings = defaultRCSettings,
rcSettings = settingsRepo.rcSettings.value,
sendAction = ::handleAction,
sendRCAction = ::handleAction,
sendSystemAction = ::handleAction
@@ -112,6 +103,11 @@ class SettingsViewModel(
_uiState.update { it.copy(dataSettings = newDataSettings) }
}
}
viewModelScope.launch {
settingsRepo.rcSettings.collect { settings ->
_uiState.update { it.copy(rcSettings = settings) }
}
}
viewModelScope.launch {
settingsRepo.otherSettings.collect { settings ->
_uiState.update { it.copy(otherSettings = settings) }
@@ -151,7 +147,6 @@ class SettingsViewModel(
private fun handleAction(action: SystemAction) {
when (action) {
is SystemAction.ShowToast -> showToast(action.message)
else -> {}
}
}