feat: add optional summary text to SwitchPreference

This commit is contained in:
andrekir 2024-11-11 17:07:57 -03:00
parent 1d992d5caf
commit 683b354dd4
4 changed files with 68 additions and 30 deletions

View file

@ -10,7 +10,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material.ContentAlpha
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
@ -103,7 +102,6 @@ fun RegularPreference(
}
}
if (summary != null) {
Divider(modifier = Modifier.padding(vertical = 8.dp))
Text(
text = summary,
style = MaterialTheme.typography.body2,

View file

@ -1,24 +1,62 @@
package com.geeksville.mesh.ui.components
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material.ContentAlpha
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ListItem
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Switch
import androidx.compose.material.SwitchDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.geeksville.mesh.R
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun SwitchPreference(
title: String,
summary: String,
checked: Boolean,
enabled: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
) {
val color = if (enabled) {
Color.Unspecified
} else {
MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled)
}
ListItem(
modifier = modifier,
trailing = {
Switch(
enabled = enabled,
checked = checked,
onCheckedChange = onCheckedChange,
)
},
secondaryText = {
Text(
text = summary,
color = color,
)
},
) {
Text(
text = title,
color = color,
)
}
}
@Composable
fun SwitchPreference(
@ -34,6 +72,7 @@ fun SwitchPreference(
.fillMaxWidth()
.size(48.dp)
.padding(padding),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
@ -46,15 +85,9 @@ fun SwitchPreference(
},
)
Switch(
modifier = modifier
.fillMaxWidth()
.wrapContentWidth(Alignment.End),
enabled = enabled,
checked = checked,
onCheckedChange = onCheckedChange,
colors = SwitchDefaults.colors(
uncheckedThumbColor = colorResource(R.color.colourGrey)
)
)
}
}

View file

@ -101,8 +101,8 @@ fun DeviceConfigItemList(
onItemSelected = { deviceInput = deviceInput.copy { role = it } },
summary = stringResource(id = deviceInput.role.stringRes),
)
Divider()
}
item { Divider() }
item {
EditTextPreference(title = "Redefine PIN_BUTTON",
@ -132,8 +132,8 @@ fun DeviceConfigItemList(
onItemSelected = { deviceInput = deviceInput.copy { rebroadcastMode = it } },
summary = stringResource(id = deviceInput.rebroadcastMode.stringRes),
)
Divider()
}
item { Divider() }
item {
EditTextPreference(title = "NodeInfo broadcast interval (seconds)",
@ -146,24 +146,26 @@ fun DeviceConfigItemList(
}
item {
SwitchPreference(title = "Double tap as button press",
SwitchPreference(
title = "Double tap as button press",
summary = stringResource(id = R.string.config_device_doubleTapAsButtonPress_summary),
checked = deviceInput.doubleTapAsButtonPress,
enabled = enabled,
onCheckedChange = {
deviceInput = deviceInput.copy { doubleTapAsButtonPress = it }
})
onCheckedChange = { deviceInput = deviceInput.copy { doubleTapAsButtonPress = it } }
)
Divider()
}
item { Divider() }
item {
SwitchPreference(title = "Disable triple-click",
SwitchPreference(
title = "Disable triple-click",
summary = stringResource(id = R.string.config_device_disableTripleClick_summary),
checked = deviceInput.disableTripleClick,
enabled = enabled,
onCheckedChange = {
deviceInput = deviceInput.copy { disableTripleClick = it }
})
onCheckedChange = { deviceInput = deviceInput.copy { disableTripleClick = it } }
)
Divider()
}
item { Divider() }
item {
EditTextPreference(title = "POSIX Timezone",
@ -182,18 +184,19 @@ fun DeviceConfigItemList(
}
item {
SwitchPreference(title = "Disable LED heartbeat",
SwitchPreference(
title = "Disable LED heartbeat",
summary = stringResource(id = R.string.config_device_ledHeartbeatDisabled_summary),
checked = deviceInput.ledHeartbeatDisabled,
enabled = enabled,
onCheckedChange = {
deviceInput = deviceInput.copy { ledHeartbeatDisabled = it }
})
onCheckedChange = { deviceInput = deviceInput.copy { ledHeartbeatDisabled = it } }
)
Divider()
}
item { Divider() }
item {
PreferenceFooter(
enabled = deviceInput != deviceConfig,
enabled = enabled && deviceInput != deviceConfig,
onCancelClicked = {
focusManager.clearFocus()
deviceInput = deviceConfig

View file

@ -64,6 +64,10 @@
<string name="rebroadcast_mode_none">Only permitted for SENSOR, TRACKER and TAK_TRACKER roles, this will inhibit all rebroadcasts, not unlike CLIENT_MUTE role.</string>
<string name="rebroadcast_mode_core_portnums_only">Ignores packets from non-standard portnums such as: TAK, RangeTest, PaxCounter, etc. Only rebroadcasts packets with standard portnums: NodeInfo, Text, Position, Telemetry, and Routing.</string>
<string name="config_device_doubleTapAsButtonPress_summary">Treat double tap on supported accelerometers as a user button press.</string>
<string name="config_device_disableTripleClick_summary">Disables the triple-press of user button to enable or disable GPS.</string>
<string name="config_device_ledHeartbeatDisabled_summary">Controls the blinking LED on the device. For most devices this will control one of the up to 4 LEDs, the charger and GPS LEDs are not controllable.</string>
<string name="elevation_suffix" translatable="false">MSL</string>
<string name="channel_air_util" translatable="false">ChUtil %.1f%% AirUtilTX %.1f%%</string>