Settings: saved as they change, no repeated headings

Every settings page now saves on its own: the line's and the modem's
pages and the dial-up ISP's lose their Save button (a change is stored
half a second after the last edit, and at once when the screen closes),
and the serial bridges' port, password, speed and format fields lose
their Set button (used 1.5 s after typing stops, on Done, or when the
page closes; a value that isn't valid is still ignored). Saves run in a
scope that outlives the screen, so leaving a page right after a change
keeps it.

The pages no longer repeat their title as a heading under it
(Appearance, Terminal, Linux, Fax, SSH keys, Dial-up internet, Serial
bridges), and a page's settings start 8 dp below its title bar.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRUcPSSYn1E8cwS11wj
This commit is contained in:
Claude committed 2026-11-04 04:51:17 +00:00
1 parent c71b5e997e
commit c717dad7f1
9 files changed
+106 -71

No files matched your search

@@ -61,7 +61,6 @@ fun AppearanceSection() {
}
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("Appearance", style = MaterialTheme.typography.titleMedium)
Text("Theme", style = MaterialTheme.typography.labelMedium)
SingleChoiceSegmentedButtonRow(Modifier.fillMaxWidth()) {
DarkMode.entries.forEachIndexed { i, m ->
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
package org.dmodem.app.android.ui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
/**
* Where settings are saved from: a scope that outlives the screen, so a change made just before
* the page closes is still stored. (Main.immediate: saves start in the order they're made.)
*/
object SettingsSaving {
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
}
/**
* Settings saved as they change: [value] stored with [save] a moment ([delayMs]) after its last
* change, so typing isn't saved a letter at a time, and at once when the screen closes.
* The value it starts with is taken as already stored. Returns what's stored now.
*/
@Composable
fun <T> rememberAutoSave(value: T, delayMs: Long = 500, save: suspend (T) -> Unit): T {
var stored by remember { mutableStateOf(value) }
val latest by rememberUpdatedState(value)
val saveIt by rememberUpdatedState(save)
LaunchedEffect(value) {
if (value == stored) return@LaunchedEffect
delay(delayMs)
stored = value
SettingsSaving.scope.launch { saveIt(value) }
}
DisposableEffect(Unit) {
onDispose {
val v = latest
if (v != stored) {
stored = v
SettingsSaving.scope.launch { saveIt(v) }
}
}
}
return stored
}
@@ -25,7 +25,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -51,16 +51,16 @@ import java.net.NetworkInterface
fun BridgeSection() {
val context = LocalContext.current
val store = remember { BridgeSettingsStore(context.applicationContext) }
val scope = rememberCoroutineScope()
val current by store.settings.collectAsStateWithLifecycle(initialValue = null)
val s = current ?: return
/** [f] applied to the stored settings, the internet host's service brought in line with them */
fun change(f: (BridgeSettings) -> BridgeSettings) {
scope.launch {
val app = context.applicationContext
SettingsSaving.scope.launch {
store.update(f)
// (internet mode: its own service, on or off again as the settings now are)
val now = store.current()
if (now.internet && now.anyOn) runCatching { org.dmodem.app.android.bridge.HostService.update(context) }
if (now.internet && now.anyOn) runCatching { org.dmodem.app.android.bridge.HostService.update(app) }
}
}
val run by ModemState.run.collectAsStateWithLifecycle()
@@ -80,7 +80,6 @@ fun BridgeSection() {
}
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("Serial port for other devices", style = MaterialTheme.typography.titleMedium)
Text(
if (s.internet) {
"Computers dial the phone as if it were their ISP's modem (ATDT anything, or a direct cable " +
@@ -313,20 +312,32 @@ private fun Choice(label: String, selected: Boolean, onClick: () -> Unit) {
}
}
/** a field whose value is used when Set is pressed (a port half typed isn't listened on) */
/** a field whose value is used a moment after typing stops, or on Done, or when the page closes
* (so a port half typed isn't listened on); [onSet] ignores a value that isn't valid */
@Composable
private fun Applied(label: String, value: String, number: Boolean = false, password: Boolean = false, onSet: (String) -> Unit) {
var text by remember(value) { mutableStateOf(value) }
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
OutlinedTextField(
value = text,
onValueChange = { text = if (number) it.filter(Char::isDigit).take(5) else it },
label = { Text(label) },
singleLine = true,
visualTransformation = if (password) PasswordVisualTransformation() else androidx.compose.ui.text.input.VisualTransformation.None,
keyboardOptions = KeyboardOptions(keyboardType = if (number) KeyboardType.Number else if (password) KeyboardType.Password else KeyboardType.Text),
modifier = Modifier.weight(1f),
)
Button(onClick = { onSet(text) }, enabled = text != value) { Text("Set") }
val latest by androidx.compose.runtime.rememberUpdatedState(text to value)
val set by androidx.compose.runtime.rememberUpdatedState(onSet)
LaunchedEffect(text) {
if (text == value) return@LaunchedEffect
kotlinx.coroutines.delay(1500)
set(text)
}
androidx.compose.runtime.DisposableEffect(Unit) {
onDispose { latest.let { (t, v) -> if (t != v) set(t) } }
}
OutlinedTextField(
value = text,
onValueChange = { text = if (number) it.filter(Char::isDigit).take(5) else it },
label = { Text(label) },
singleLine = true,
visualTransformation = if (password) PasswordVisualTransformation() else androidx.compose.ui.text.input.VisualTransformation.None,
keyboardOptions = KeyboardOptions(
keyboardType = if (number) KeyboardType.Number else if (password) KeyboardType.Password else KeyboardType.Text,
imeAction = androidx.compose.ui.text.input.ImeAction.Done,
),
keyboardActions = androidx.compose.foundation.text.KeyboardActions(onDone = { if (text != value) onSet(text) }),
modifier = Modifier.fillMaxWidth(),
)
}
@@ -33,7 +33,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -190,17 +189,15 @@ private fun open(context: Context, name: String) {
fun FaxSettingsSection() {
val context = LocalContext.current
val store = remember { FaxSettingsStore(context.applicationContext) }
val scope = rememberCoroutineScope()
val current by store.settings.collectAsStateWithLifecycle(initialValue = null)
val s = current ?: return
var ident by remember(s.ident) { mutableStateOf(s.ident) }
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("Fax", style = MaterialTheme.typography.titleMedium)
OutlinedTextField(
value = ident,
onValueChange = { v ->
ident = v.filter { it.isDigit() || it == '+' || it == ' ' }.take(20)
scope.launch { store.update { it.copy(ident = ident) } }
SettingsSaving.scope.launch { store.update { it.copy(ident = ident) } }
},
label = { Text("This fax's number (sent to the other machine)") }, singleLine = true,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone), modifier = Modifier.fillMaxWidth(),
@@ -210,23 +207,23 @@ fun FaxSettingsSection() {
if (ModemState.adaptiveAnswer) "Answer every call that rings (a fax machine's as a fax)" else "Answer calls that ring as faxes",
Modifier.weight(1f),
)
Switch(checked = s.answer, onCheckedChange = { v -> scope.launch { store.update { it.copy(answer = v) } } })
Switch(checked = s.answer, onCheckedChange = { v -> SettingsSaving.scope.launch { store.update { it.copy(answer = v) } } })
}
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
Text("Fine resolution (196 lines per inch)", Modifier.weight(1f))
Switch(checked = s.fine, onCheckedChange = { v -> scope.launch { store.update { it.copy(fine = v) } } })
Switch(checked = s.fine, onCheckedChange = { v -> SettingsSaving.scope.launch { store.update { it.copy(fine = v) } } })
}
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
Column(Modifier.weight(1f)) {
Text("Error correction (ECM)")
Text("What a noisy line spoils is sent again, when the other machine can", style = MaterialTheme.typography.bodySmall)
}
Switch(checked = s.ecm, onCheckedChange = { v -> scope.launch { store.update { it.copy(ecm = v) } } })
Switch(checked = s.ecm, onCheckedChange = { v -> SettingsSaving.scope.launch { store.update { it.copy(ecm = v) } } })
}
Text("Fastest rate", style = MaterialTheme.typography.bodyMedium)
for ((bps, label) in listOf(14400 to "14400 (V.17)", 9600 to "9600 (V.29)", 4800 to "4800 (V.27ter: poor lines)")) {
Row(
Modifier.fillMaxWidth().clickable { scope.launch { store.update { it.copy(maxBps = bps) } } },
Modifier.fillMaxWidth().clickable { SettingsSaving.scope.launch { store.update { it.copy(maxBps = bps) } } },
verticalAlignment = Alignment.CenterVertically,
) {
RadioButton(selected = s.maxBps == bps, onClick = null)
@@ -156,12 +156,13 @@ private fun size(n: Long) = when {
fun IspSettingsSection() {
val context = LocalContext.current
val store = remember { IspSettingsStore(context.applicationContext) }
val scope = rememberCoroutineScope()
val current by store.settings.collectAsStateWithLifecycle(initialValue = null)
val loaded = current ?: return
var s by remember(loaded) { mutableStateOf(loaded) }
// (read once: what's typed is saved as it changes, not read back over the typing)
var loaded by remember { mutableStateOf<IspSettings?>(null) }
LaunchedEffect(Unit) { loaded = store.current() }
val initial = loaded ?: return
var s by remember(initial) { mutableStateOf(initial) }
rememberAutoSave(s) { store.save(it) }
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("Dial-up internet (ISP)", style = MaterialTheme.typography.titleMedium)
OutlinedTextField(
value = s.number, onValueChange = { s = s.copy(number = it) }, label = { Text("The ISP's number") }, singleLine = true,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone), modifier = Modifier.fillMaxWidth(),
@@ -201,9 +202,5 @@ fun IspSettingsSection() {
"network. Calls in need the line to take them (on SIP: \"Take calls\", registered).",
style = MaterialTheme.typography.bodySmall,
)
Button(onClick = {
scope.launch { store.save(s) }
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show()
}, enabled = s != loaded) { Text("Save") }
}
}
@@ -74,7 +74,6 @@ fun LinuxSection(onOpened: () -> Unit = {}) {
val busy = work is Linux.Work.Busy
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("Linux", style = MaterialTheme.typography.titleMedium)
if (Linux.machine() == null) {
Text("There is no Linux built for this phone's processor.", style = MaterialTheme.typography.bodySmall)
return@Column
@@ -28,7 +28,6 @@ import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.TextButton
import androidx.compose.ui.text.font.FontWeight
import org.dmodem.app.tones.CountryTones
@@ -41,7 +40,6 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -53,7 +51,6 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import org.dmodem.app.android.modem.ModemState
import org.dmodem.app.android.settings.ModemSettings
import org.dmodem.app.android.settings.SettingsStore
import kotlinx.coroutines.launch
/** Settings' pages: the menu's rows, each a page of its own. */
enum class SettingsPage(val title: String) {
@@ -98,15 +95,13 @@ object SettingsNav {
/**
* Settings: a menu of its pages (the line, the modem, the bridges, fax,
* dial-up, appearance, the terminal, SSH keys, Linux, and the AT command
* reference), each with what it is set to; back from a page to the menu. The line's and the modem's
* settings are saved with their Save button, and apply when the modem next
* starts; their edits are kept across the two pages until then.
* reference), each with what it is set to; back from a page to the menu. Every setting is
* saved as it's changed; the line's and the modem's apply when the modem next starts.
*/
@Composable
fun SettingsScreen(modifier: Modifier = Modifier, onOpenTerminal: () -> Unit = {}) {
val context = LocalContext.current
val store = remember { SettingsStore(context.applicationContext) }
val scope = rememberCoroutineScope()
val run by ModemState.run.collectAsStateWithLifecycle()
val page by SettingsNav.page.collectAsStateWithLifecycle()
@@ -115,32 +110,18 @@ fun SettingsScreen(modifier: Modifier = Modifier, onOpenTerminal: () -> Unit = {
val initial = loaded ?: return
var s by remember(initial) { mutableStateOf(initial) }
var savedAs by remember(initial) { mutableStateOf(initial) }
var login by remember { mutableStateOf("") }
var saved by remember { mutableStateOf(false) }
// (each change saved as it's made; it applies when the modem next starts)
val stored = rememberAutoSave(s) { store.save(it) }
// back: a page to the menu (the menu's own back is the app's: Home)
androidx.activity.compose.BackHandler(enabled = page != null) { SettingsNav.open(null) }
/** the line's and the modem's Save button, and whether they're saved (and need a restart to apply) */
/** the line's and the modem's note: their changes are saved, and wait for the modem's restart */
@Composable
fun SaveRow() {
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) {
Button(onClick = {
scope.launch {
store.save(s)
savedAs = s
saved = true
}
}, enabled = s != savedAs) { Text("Save") }
if (s != savedAs) {
Text("Not saved yet", style = MaterialTheme.typography.bodyMedium)
} else if (saved) {
Text(
if (run == ModemState.Run.STOPPED) "Saved" else "Saved: restart the modem to apply",
style = MaterialTheme.typography.bodyMedium,
)
}
fun AppliesNote() {
if (stored != initial && run != ModemState.Run.STOPPED) {
Text("Saved: restart the modem to apply", style = MaterialTheme.typography.bodyMedium)
}
}
@@ -164,7 +145,7 @@ fun SettingsScreen(modifier: Modifier = Modifier, onOpenTerminal: () -> Unit = {
return@Column
}
Column(
Modifier.fillMaxSize().padding(horizontal = 16.dp).padding(bottom = 16.dp).verticalScroll(rememberScrollState()),
Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 16.dp).padding(top = 8.dp, bottom = 16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
when (p) {
@@ -227,7 +208,7 @@ fun SettingsScreen(modifier: Modifier = Modifier, onOpenTerminal: () -> Unit = {
Toggle("Fax over T.38", s.t38) { s = s.copy(t38 = it) }
NumberField("SIP log level (0-4)", s.sipLogLevel) { s = s.copy(sipLogLevel = it.coerceIn(0, 4)) }
}
SaveRow()
AppliesNote()
}
SettingsPage.MODEM -> {
Toggle("Speaker: hear the dialling and the handshake", s.speaker) { s = s.copy(speaker = it) }
@@ -270,7 +251,7 @@ fun SettingsScreen(modifier: Modifier = Modifier, onOpenTerminal: () -> Unit = {
color = if (org.dmodem.app.android.modem.LogFormat.valid(s.logTimestamp)) androidx.compose.ui.graphics.Color.Unspecified
else MaterialTheme.colorScheme.error,
)
SaveRow()
AppliesNote()
}
SettingsPage.BRIDGES -> BridgeSection()
SettingsPage.FAX -> FaxSettingsSection()
@@ -293,7 +274,7 @@ fun SettingsScreen(modifier: Modifier = Modifier, onOpenTerminal: () -> Unit = {
label = "settings page",
modifier = modifier,
) { p ->
if (p == null) SettingsMenu(Modifier, s, s != savedAs) else Page(p)
if (p == null) SettingsMenu(Modifier, s) else Page(p)
}
}
@@ -311,9 +292,9 @@ private fun settingsTransition(back: Boolean): ContentTransform {
/** Settings' menu: a row for each page, what it's set to beneath */
@Composable
private fun SettingsMenu(modifier: Modifier, s: ModemSettings, unsaved: Boolean) {
private fun SettingsMenu(modifier: Modifier, s: ModemSettings) {
val rows = listOf(
SettingsPage.LINE to lineSummary(s) + if (unsaved) " (not saved)" else "",
SettingsPage.LINE to lineSummary(s),
SettingsPage.MODEM to "${CountryTones.find(s.country)?.label ?: s.country}; speaker " +
(if (s.speaker) "on" else "off") + "; " + (if (s.adaptiveAnswer) "fax or data as calls turn out" else "calls answered as data"),
SettingsPage.BRIDGES to "The modem for another computer: TCP, Bluetooth, USB serial",
@@ -56,7 +56,6 @@ fun SshKeysSection() {
var hosts by remember { mutableStateOf(false) }
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("SSH keys", style = MaterialTheme.typography.titleMedium)
if (keys.isEmpty()) {
Text("None yet. Make one here and put its public key in the server's ~/.ssh/authorized_keys.", style = MaterialTheme.typography.bodySmall)
}
@@ -64,7 +64,6 @@ fun TerminalSettingsSection() {
}
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("Terminal", style = MaterialTheme.typography.titleMedium)
Choice("Colours", TermTheme.byId(s.themeId).name, TermTheme.ALL.map { it.name to it.id }) { set { t -> t.copy(themeId = it) } }
Choice("Font", s.font.label, TerminalFont.entries.map { it.label to it }) { set { t -> t.copy(font = it) } }
FontPreview(s.font, TermTheme.byId(s.themeId))