mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
Line settings: USB, Bluetooth, Wi-Fi / Cell and More buttons filter the line menu
Each kind's button shows only its lines in the menu (USB's one line is picked at once); More shows every line. The kind of the line saved is lit at first. The page's duplicate Line heading is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
3 files changed
+140
-24
No files matched your search
@@ -11,6 +11,14 @@ import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.semantics.selected
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.ExposedDropdownMenuAnchorType
|
||||
@@ -51,7 +59,6 @@ fun LineChoice(s: ModemSettings, onChange: (ModemSettings) -> Unit) {
|
||||
val askBtModem = rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
|
||||
if (BluetoothBridge.hasPermission(context)) onChange(s.copy(line = ModemSettings.LINE_BT_MODEM))
|
||||
}
|
||||
Text("Line", style = MaterialTheme.typography.titleMedium)
|
||||
val lines = listOf(
|
||||
ModemSettings.LINE_SIP to "A SIP account (calls over the internet)",
|
||||
ModemSettings.LINE_WIFI to "Another phone with D-Modem, on Wi-Fi",
|
||||
@@ -61,40 +68,52 @@ fun LineChoice(s: ModemSettings, onChange: (ModemSettings) -> Unit) {
|
||||
ModemSettings.LINE_BT_MODEM to "A phone line, through a Bluetooth modem (Dial-Up Networking)",
|
||||
ModemSettings.LINE_NONE to "No line (the modem on its own)",
|
||||
)
|
||||
// the line chosen: the permission asked for first where it needs one
|
||||
fun choose(line: String) {
|
||||
when (line) {
|
||||
ModemSettings.LINE_SIP, ModemSettings.LINE_CELL, ModemSettings.LINE_NONE, ModemSettings.LINE_USB ->
|
||||
onChange(s.copy(line = line))
|
||||
ModemSettings.LINE_WIFI -> onChange(
|
||||
s.copy(line = line, peerAddress = if (s.line == line) s.peerAddress else "", peerName = ""),
|
||||
)
|
||||
ModemSettings.LINE_BT_MODEM -> if (!BluetoothBridge.hasPermission(context)) {
|
||||
askBtModem.launch(BluetoothBridge.permissions())
|
||||
} else {
|
||||
onChange(s.copy(line = line))
|
||||
}
|
||||
ModemSettings.LINE_BLUETOOTH -> if (!BluetoothBridge.hasPermission(context)) {
|
||||
askBt.launch(BluetoothBridge.permissions())
|
||||
} else {
|
||||
onChange(s.copy(line = line, peerAddress = if (s.line == line) s.peerAddress else "", peerName = ""))
|
||||
}
|
||||
}
|
||||
}
|
||||
var kind by rememberSaveable { mutableStateOf(LineKind.of(s.line)) }
|
||||
LineKindButtons(kind) { k ->
|
||||
kind = k
|
||||
// (a kind of one line is that line; another, its first unless the line is one of it)
|
||||
val shown = lines.filter { k.has(it.first) }
|
||||
if (shown.none { it.first == s.line }) choose(shown.first().first)
|
||||
}
|
||||
val shown = lines.filter { kind.has(it.first) }
|
||||
var open by remember { mutableStateOf(false) }
|
||||
ExposedDropdownMenuBox(expanded = open, onExpandedChange = { open = it }) {
|
||||
ExposedDropdownMenuBox(expanded = open, onExpandedChange = { open = it && shown.size > 1 }) {
|
||||
OutlinedTextField(
|
||||
value = lines.firstOrNull { it.first == s.line }?.second ?: lines[0].second,
|
||||
onValueChange = {},
|
||||
readOnly = true,
|
||||
singleLine = true,
|
||||
label = { Text("The modem's line") },
|
||||
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = open) },
|
||||
trailingIcon = { if (shown.size > 1) ExposedDropdownMenuDefaults.TrailingIcon(expanded = open) },
|
||||
modifier = Modifier.fillMaxWidth().menuAnchor(ExposedDropdownMenuAnchorType.PrimaryNotEditable),
|
||||
)
|
||||
ExposedDropdownMenu(expanded = open, onDismissRequest = { open = false }) {
|
||||
for ((line, label) in lines) {
|
||||
for ((line, label) in shown) {
|
||||
DropdownMenuItem(
|
||||
text = { Text(label) },
|
||||
onClick = {
|
||||
open = false
|
||||
when (line) {
|
||||
ModemSettings.LINE_SIP, ModemSettings.LINE_CELL, ModemSettings.LINE_NONE, ModemSettings.LINE_USB ->
|
||||
onChange(s.copy(line = line))
|
||||
ModemSettings.LINE_WIFI -> onChange(
|
||||
s.copy(line = line, peerAddress = if (s.line == line) s.peerAddress else "", peerName = ""),
|
||||
)
|
||||
ModemSettings.LINE_BT_MODEM -> if (!BluetoothBridge.hasPermission(context)) {
|
||||
askBtModem.launch(BluetoothBridge.permissions())
|
||||
} else {
|
||||
onChange(s.copy(line = line))
|
||||
}
|
||||
ModemSettings.LINE_BLUETOOTH -> if (!BluetoothBridge.hasPermission(context)) {
|
||||
askBt.launch(BluetoothBridge.permissions())
|
||||
} else {
|
||||
onChange(s.copy(line = line, peerAddress = if (s.line == line) s.peerAddress else "", peerName = ""))
|
||||
}
|
||||
}
|
||||
choose(line)
|
||||
},
|
||||
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding,
|
||||
)
|
||||
@@ -103,6 +122,58 @@ fun LineChoice(s: ModemSettings, onChange: (ModemSettings) -> Unit) {
|
||||
}
|
||||
}
|
||||
|
||||
/** the line menu's filters: the kinds of line, each its icon's button; MORE every line */
|
||||
enum class LineKind(val label: String, private val lines: Set<String>) {
|
||||
USB("USB", setOf(ModemSettings.LINE_USB)),
|
||||
BLUETOOTH("Bluetooth", setOf(ModemSettings.LINE_BLUETOOTH, ModemSettings.LINE_BT_MODEM)),
|
||||
NETWORK("Wi-Fi / Cell", setOf(ModemSettings.LINE_SIP, ModemSettings.LINE_WIFI, ModemSettings.LINE_CELL)),
|
||||
MORE("More", emptySet());
|
||||
|
||||
/** whether line is one of this kind's (every line is MORE's) */
|
||||
fun has(line: String): Boolean = this == MORE || line in lines
|
||||
|
||||
/** this kind's icon */
|
||||
val icon: ImageVector
|
||||
get() = when (this) {
|
||||
USB -> SettingsIcons.Bridges
|
||||
BLUETOOTH -> SettingsIcons.Bluetooth
|
||||
NETWORK -> SettingsIcons.Wifi
|
||||
MORE -> SettingsIcons.More
|
||||
}
|
||||
|
||||
companion object {
|
||||
/** the kind a line is shown under at first (none's: MORE) */
|
||||
fun of(line: String): LineKind = entries.firstOrNull { it != MORE && line in it.lines } ?: MORE
|
||||
}
|
||||
}
|
||||
|
||||
/** the row of the line kinds' buttons, kind's lit; onPick on a press */
|
||||
@Composable
|
||||
fun LineKindButtons(kind: LineKind, onPick: (LineKind) -> Unit) {
|
||||
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
for (k in LineKind.entries) {
|
||||
val on = k == kind
|
||||
val colors = MaterialTheme.colorScheme
|
||||
Surface(
|
||||
onClick = { onPick(k) },
|
||||
modifier = Modifier.weight(1f).semantics { selected = on },
|
||||
shape = RoundedCornerShape(16.dp),
|
||||
color = if (on) colors.primaryContainer else colors.surfaceContainer,
|
||||
contentColor = if (on) colors.onPrimaryContainer else colors.onSurfaceVariant,
|
||||
) {
|
||||
Column(
|
||||
Modifier.padding(vertical = 10.dp, horizontal = 4.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(4.dp),
|
||||
) {
|
||||
Icon(k.icon, contentDescription = null, modifier = Modifier.size(24.dp))
|
||||
Text(k.label, style = MaterialTheme.typography.labelMedium, maxLines = 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A phone line through a USB modem: which ones there are, what's plugged in */
|
||||
@Composable
|
||||
fun UsbLineFields() {
|
||||
|
||||
@@ -91,5 +91,28 @@ object SettingsIcons {
|
||||
"M20 18c1.1 0 1.99-.9 1.99-2L22 6c0-1.1-.9-2-2-2H4c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2H0v2h24v-2h-4zM4 6h16v10H4V6z",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Material Icons' bluetooth */
|
||||
val Bluetooth: ImageVector by lazy {
|
||||
icon(
|
||||
"bluetooth",
|
||||
"M17.71 7.71L12 2h-1v7.59L6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 11 14.41V22h1l5.71-5.71-4.3-4.29 4.3-4.29zM13 5.83l1.88 1.88L13 9.59V5.83zm1.88 10.46L13 18.17v-3.76l1.88 1.88z",
|
||||
)
|
||||
}
|
||||
|
||||
/** Material Icons' wifi */
|
||||
val Wifi: ImageVector by lazy {
|
||||
icon(
|
||||
"wifi",
|
||||
"M1 9l2 2c4.97-4.97 13.03-4.97 18 0l2-2C16.93 2.93 7.08 2.93 1 9zm8 8l3 3 3-3c-1.65-1.66-4.34-1.66-6 0zm-4-4l2 2c2.76-2.76 7.24-2.76 10 0l2-2C15.14 9.14 8.87 9.14 5 13z",
|
||||
)
|
||||
}
|
||||
|
||||
/** Material Icons' more_horiz */
|
||||
val More: ImageVector by lazy {
|
||||
icon(
|
||||
"more_horiz",
|
||||
"M6 10c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm12 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm-6 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z",
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,8 @@ class PeerShotsTest {
|
||||
save(ModemSettings(line = ModemSettings.LINE_SIP))
|
||||
SettingsNav.open(SettingsPage.LINE)
|
||||
compose.onNodeWithText("Settings").performClick()
|
||||
// (under More: the Wi-Fi / Cell lines' menu hasn't it)
|
||||
compose.onNodeWithText("More").performClick()
|
||||
compose.onNodeWithText("A SIP account (calls over the internet)").performScrollTo().performClick()
|
||||
compose.onNodeWithText("No line (the modem on its own)").performClick()
|
||||
compose.onNodeWithText("a dial gets NO DIALTONE", substring = true).assertExists()
|
||||
@@ -84,14 +86,34 @@ class PeerShotsTest {
|
||||
shot("peer-4-no-line")
|
||||
}
|
||||
|
||||
/** the line kinds' buttons: Wi-Fi / Cell's menu only its lines, More's every one; drawn */
|
||||
@Test
|
||||
fun theLineKinds() {
|
||||
save(ModemSettings(line = ModemSettings.LINE_SIP))
|
||||
SettingsNav.open(SettingsPage.LINE)
|
||||
compose.onNodeWithText("Settings").performClick()
|
||||
compose.onNodeWithText("A SIP account (calls over the internet)").performScrollTo().performClick()
|
||||
compose.onNodeWithText("Another phone with D-Modem, on Wi-Fi").assertExists()
|
||||
compose.onNodeWithText("No line (the modem on its own)").assertDoesNotExist()
|
||||
compose.onNodeWithText("A phone line, through a USB modem").assertDoesNotExist()
|
||||
shot("peer-6-line-kinds")
|
||||
compose.onNodeWithText("Another phone with D-Modem, on Wi-Fi").performClick()
|
||||
compose.onNodeWithText("More").performClick()
|
||||
compose.onNodeWithText("Another phone with D-Modem, on Wi-Fi").performScrollTo().performClick()
|
||||
compose.onNodeWithText("No line (the modem on its own)").assertExists()
|
||||
compose.onNodeWithText("A phone line, through a USB modem").assertExists()
|
||||
shot("peer-7-line-kinds-more")
|
||||
}
|
||||
|
||||
/** a USB modem picked: the ones known, none plugged in, no SIP account asked for; drawn */
|
||||
@Test
|
||||
fun aUsbModem() {
|
||||
save(ModemSettings(line = ModemSettings.LINE_SIP))
|
||||
SettingsNav.open(SettingsPage.LINE)
|
||||
compose.onNodeWithText("Settings").performClick()
|
||||
compose.onNodeWithText("A SIP account (calls over the internet)").performScrollTo().performClick()
|
||||
compose.onNodeWithText("A phone line, through a USB modem").performClick()
|
||||
// (the USB button: its one line, no menu)
|
||||
compose.onNodeWithText("USB").performClick()
|
||||
compose.onNodeWithText("A phone line, through a USB modem").assertExists()
|
||||
compose.onNodeWithText("pico-serial Line stick", substring = true).assertExists()
|
||||
// (none on Robolectric's bus)
|
||||
compose.onNodeWithText("Plugged in: none").assertExists()
|
||||
|
||||
Reference in new issue
Block a user