Settings: each change made to what is saved, not to a stale copy

The appearance and terminal settings saved a whole copy of the settings
as the screen last showed them; two changes in quick succession (a theme
and a palette tapped one after the other) raced, the second undoing the
first. Each change is now applied to the stored settings inside the
store's own edit. The appearance test waits for the save it checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
Claude committed 2026-10-16 21:03:36 +00:00
1 parent cc7fd931f0
commit 7db6dfb673
5 files changed
+89 -59

No files matched your search

@@ -49,15 +49,27 @@ class AppearanceStore(private val context: Context) {
val black = booleanPreferencesKey("black")
}
val appearance: Flow<Appearance> = context.appearanceStore.data.map { p ->
private fun from(p: Preferences): Appearance {
val d = Appearance()
Appearance(
return Appearance(
dark = p[K.dark]?.let { runCatching { DarkMode.valueOf(it) }.getOrNull() } ?: d.dark,
palette = p[K.palette]?.let { runCatching { AppPalette.valueOf(it) }.getOrNull() } ?: d.palette,
black = p[K.black] ?: d.black,
)
}
val appearance: Flow<Appearance> = context.appearanceStore.data.map { from(it) }
/** one change made to what is saved now (not to a copy that may be behind: taps in quick succession) */
suspend fun update(change: (Appearance) -> Appearance) {
context.appearanceStore.edit { p ->
val a = change(from(p))
p[K.dark] = a.dark.name
p[K.palette] = a.palette.name
p[K.black] = a.black
}
}
suspend fun save(a: Appearance) {
context.appearanceStore.edit { p ->
p[K.dark] = a.dark.name
@@ -4,6 +4,7 @@ package com.neodesktop.dmodem.settings
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.MutablePreferences
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
@@ -91,9 +92,11 @@ class TerminalSettingsStore(private val context: Context) {
private inline fun <reified E : Enum<E>> enumOf(s: String?, d: E): E =
s?.let { runCatching { enumValueOf<E>(it) }.getOrNull() } ?: d
val settings: Flow<TerminalSettings> = context.terminalStore.data.map { p ->
val settings: Flow<TerminalSettings> = context.terminalStore.data.map { from(it) }
private fun from(p: Preferences): TerminalSettings {
val d = TerminalSettings()
TerminalSettings(
return TerminalSettings(
themeId = p[K.theme] ?: d.themeId,
font = enumOf(p[K.font], d.font),
ligatures = p[K.ligatures] ?: d.ligatures,
@@ -119,29 +122,36 @@ class TerminalSettingsStore(private val context: Context) {
suspend fun current(): TerminalSettings = settings.first()
/** one change made to what is saved now (not to a copy that may be behind: changes in quick succession) */
suspend fun update(change: (TerminalSettings) -> TerminalSettings) {
context.terminalStore.edit { p -> write(p, change(from(p))) }
}
suspend fun save(s: TerminalSettings) {
context.terminalStore.edit { p ->
p[K.theme] = s.themeId
p[K.font] = s.font.name
p[K.ligatures] = s.ligatures
p[K.fontSize] = s.fontSize
p[K.fixedColumns] = s.fixedColumns
p[K.charset] = s.charset.name
p[K.backspace] = s.backspace.name
p[K.enter] = s.enter.name
p[K.bell] = s.bell.name
p[K.volumeKeys] = s.volumeKeys.name
p[K.keepScreenOn] = s.keepScreenOn
p[K.scrollback] = s.scrollback
p[K.cursor] = s.cursor.name
p[K.cursorBlink] = s.cursorBlink
p[K.boldBright] = s.boldBright
p[K.iceColors] = s.iceColors
p[K.localEcho] = s.localEcho
p[K.extraKeys] = s.extraKeys
p[K.answerback] = s.answerback
p[K.drawBoxes] = s.drawBoxes
}
context.terminalStore.edit { p -> write(p, s) }
}
private fun write(p: MutablePreferences, s: TerminalSettings) {
p[K.theme] = s.themeId
p[K.font] = s.font.name
p[K.ligatures] = s.ligatures
p[K.fontSize] = s.fontSize
p[K.fixedColumns] = s.fixedColumns
p[K.charset] = s.charset.name
p[K.backspace] = s.backspace.name
p[K.enter] = s.enter.name
p[K.bell] = s.bell.name
p[K.volumeKeys] = s.volumeKeys.name
p[K.keepScreenOn] = s.keepScreenOn
p[K.scrollback] = s.scrollback
p[K.cursor] = s.cursor.name
p[K.cursorBlink] = s.cursorBlink
p[K.boldBright] = s.boldBright
p[K.iceColors] = s.iceColors
p[K.localEcho] = s.localEcho
p[K.extraKeys] = s.extraKeys
p[K.answerback] = s.answerback
p[K.drawBoxes] = s.drawBoxes
}
suspend fun setFontSize(sp: Float) {
@@ -55,8 +55,8 @@ fun AppearanceSection() {
val scope = rememberCoroutineScope()
val current by store.appearance.collectAsStateWithLifecycle(initialValue = null)
val a = current ?: return
fun set(n: Appearance) {
scope.launch { store.save(n) }
fun set(change: (Appearance) -> Appearance) {
scope.launch { store.update(change) }
}
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
@@ -66,7 +66,7 @@ fun AppearanceSection() {
DarkMode.entries.forEachIndexed { i, m ->
SegmentedButton(
selected = a.dark == m,
onClick = { set(a.copy(dark = m)) },
onClick = { set { it.copy(dark = m) } },
shape = SegmentedButtonDefaults.itemShape(i, DarkMode.entries.size),
) { Text(m.label, maxLines = 1) }
}
@@ -76,7 +76,7 @@ fun AppearanceSection() {
FlowRow(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
for (p in palettes) {
val selected = a.palette == p || (a.palette == AppPalette.DYNAMIC && p == AppPalette.BASELINE && Build.VERSION.SDK_INT < 31)
Swatch(p, selected) { set(a.copy(palette = p)) }
Swatch(p, selected) { set { it.copy(palette = p) } }
}
}
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
@@ -84,7 +84,7 @@ fun AppearanceSection() {
Text("Black background")
Text("In dark mode (for OLED screens)", style = MaterialTheme.typography.bodySmall)
}
Switch(checked = a.black, onCheckedChange = { set(a.copy(black = it)) })
Switch(checked = a.black, onCheckedChange = { set { x -> x.copy(black = it) } })
}
}
}
@@ -58,17 +58,17 @@ fun TerminalSettingsSection() {
val scope = rememberCoroutineScope()
val current by store.settings.collectAsStateWithLifecycle(initialValue = null)
val s = current ?: return
fun set(n: TerminalSettings) {
scope.launch { store.save(n) }
fun set(change: (TerminalSettings) -> TerminalSettings) {
scope.launch { store.update(change) }
}
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(s.copy(themeId = it)) }
Choice("Font", s.font.label, TerminalFont.entries.map { it.label to it }) { set(s.copy(font = it)) }
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))
if (s.font.ligatures) {
Flag("Ligatures (-> != ==)", s.ligatures) { set(s.copy(ligatures = it)) }
Flag("Ligatures (-> != ==)", s.ligatures) { set { t -> t.copy(ligatures = it) } }
}
var showLicenses by remember { mutableStateOf(false) }
TextButton(onClick = { showLicenses = true }) { Text("Font licences") }
@@ -76,30 +76,30 @@ fun TerminalSettingsSection() {
Choice(
"Width", if (s.fixedColumns > 0) "${s.fixedColumns} columns" else "By font size",
listOf("80 columns (BBS)" to 80, "132 columns" to 132, "40 columns" to 40, "By font size" to 0),
) { set(s.copy(fixedColumns = it)) }
) { set { t -> t.copy(fixedColumns = it) } }
if (s.fixedColumns == 0) {
Text("Font size: ${"%.1f".format(s.fontSize)} sp (or pinch the screen)", style = MaterialTheme.typography.bodyMedium)
Slider(value = s.fontSize, onValueChange = { set(s.copy(fontSize = it)) }, valueRange = 6f..28f)
Slider(value = s.fontSize, onValueChange = { set { t -> t.copy(fontSize = it) } }, valueRange = 6f..28f)
}
Choice("Character set", s.charset.label, TermCharset.entries.map { it.label to it }) { set(s.copy(charset = it)) }
Choice("Backspace sends", s.backspace.label, BackspaceMode.entries.map { it.label to it }) { set(s.copy(backspace = it)) }
Choice("Enter sends", s.enter.label, EnterMode.entries.map { it.label to it }) { set(s.copy(enter = it)) }
Choice("Cursor", s.cursor.label, CursorShape.entries.map { it.label to it }) { set(s.copy(cursor = it)) }
Choice("Bell", s.bell.label, BellMode.entries.map { it.label to it }) { set(s.copy(bell = it)) }
Choice("Volume keys", s.volumeKeys.label, VolumeKeys.entries.map { it.label to it }) { set(s.copy(volumeKeys = it)) }
Flag("Blinking cursor", s.cursorBlink) { set(s.copy(cursorBlink = it)) }
Flag("Bold as bright colours (DOS, BBS)", s.boldBright) { set(s.copy(boldBright = it)) }
Flag("iCE colours (blink as bright background)", s.iceColors) { set(s.copy(iceColors = it)) }
Flag("Draw line and block characters (gapless ANSI art)", s.drawBoxes) { set(s.copy(drawBoxes = it)) }
Flag("Local echo", s.localEcho) { set(s.copy(localEcho = it)) }
Flag("Extra keys bar", s.extraKeys) { set(s.copy(extraKeys = it)) }
Flag("Keep the screen on", s.keepScreenOn) { set(s.copy(keepScreenOn = it)) }
Choice("Character set", s.charset.label, TermCharset.entries.map { it.label to it }) { set { t -> t.copy(charset = it) } }
Choice("Backspace sends", s.backspace.label, BackspaceMode.entries.map { it.label to it }) { set { t -> t.copy(backspace = it) } }
Choice("Enter sends", s.enter.label, EnterMode.entries.map { it.label to it }) { set { t -> t.copy(enter = it) } }
Choice("Cursor", s.cursor.label, CursorShape.entries.map { it.label to it }) { set { t -> t.copy(cursor = it) } }
Choice("Bell", s.bell.label, BellMode.entries.map { it.label to it }) { set { t -> t.copy(bell = it) } }
Choice("Volume keys", s.volumeKeys.label, VolumeKeys.entries.map { it.label to it }) { set { t -> t.copy(volumeKeys = it) } }
Flag("Blinking cursor", s.cursorBlink) { set { t -> t.copy(cursorBlink = it) } }
Flag("Bold as bright colours (DOS, BBS)", s.boldBright) { set { t -> t.copy(boldBright = it) } }
Flag("iCE colours (blink as bright background)", s.iceColors) { set { t -> t.copy(iceColors = it) } }
Flag("Draw line and block characters (gapless ANSI art)", s.drawBoxes) { set { t -> t.copy(drawBoxes = it) } }
Flag("Local echo", s.localEcho) { set { t -> t.copy(localEcho = it) } }
Flag("Extra keys bar", s.extraKeys) { set { t -> t.copy(extraKeys = it) } }
Flag("Keep the screen on", s.keepScreenOn) { set { t -> t.copy(keepScreenOn = it) } }
var scrollback by remember(s.scrollback) { mutableStateOf(s.scrollback.toString()) }
OutlinedTextField(
value = scrollback,
onValueChange = { v ->
scrollback = v.filter { it.isDigit() }.take(6)
scrollback.toIntOrNull()?.let { set(s.copy(scrollback = it.coerceIn(0, 100_000))) }
scrollback.toIntOrNull()?.let { set { t -> t.copy(scrollback = it.coerceIn(0, 100_000)) } }
},
label = { Text("Scrollback lines") },
singleLine = true,
@@ -109,7 +109,7 @@ fun TerminalSettingsSection() {
var answerback by remember(s.answerback) { mutableStateOf(s.answerback) }
OutlinedTextField(
value = answerback,
onValueChange = { answerback = it; set(s.copy(answerback = it)) },
onValueChange = { answerback = it; set { t -> t.copy(answerback = it) } },
label = { Text("Answerback (sent for ^E)") },
singleLine = true,
modifier = Modifier.fillMaxWidth(),
@@ -96,6 +96,17 @@ class AppearanceTest {
}
}
private fun saved(want: (Appearance) -> Boolean) {
val end = System.currentTimeMillis() + 5000
while (true) {
compose.waitForIdle()
val a = runBlocking { store.appearance.first() }
if (want(a)) return
if (System.currentTimeMillis() > end) throw AssertionError("not saved: $a")
Thread.sleep(50)
}
}
@Test
fun chosenInSettings() {
runBlocking { store.save(Appearance()) }
@@ -104,13 +115,10 @@ class AppearanceTest {
compose.onNodeWithText("Black background").performScrollTo()
compose.onNodeWithText("Dark").performClick()
compose.onNodeWithText("Teal").performClick()
compose.waitForIdle()
val a = runBlocking { store.appearance.first() }
assertEquals(DarkMode.DARK, a.dark)
assertEquals(AppPalette.TEAL, a.palette)
// (saved in the background: waited for)
saved { it.dark == DarkMode.DARK && it.palette == AppPalette.TEAL }
compose.onNodeWithText("System default").performClick()
compose.waitForIdle()
assertEquals(DarkMode.SYSTEM, runBlocking { store.appearance.first() }.dark)
saved { it.dark == DarkMode.SYSTEM }
}
}
}