Android: the language setting, TalkBack moves, sample names in the current language

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-22 01:59:17 +03:00
parent de5ca02a82
commit 305fff20b6
10 changed files with 117 additions and 7 deletions

View file

@ -1,5 +1,6 @@
package io.github.vrubelroman.chesspenguin
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
@ -11,10 +12,15 @@ import androidx.compose.ui.Modifier
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import io.github.vrubelroman.chesspenguin.data.Language
import io.github.vrubelroman.chesspenguin.data.Theme
import io.github.vrubelroman.chesspenguin.theme.ChessPenguinTheme
class MainActivity : ComponentActivity() {
override fun attachBaseContext(base: Context) {
super.attachBaseContext(Language.wrap(base))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()

View file

@ -0,0 +1,44 @@
package io.github.vrubelroman.chesspenguin.data
import android.app.LocaleManager
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
/// The interface languages; "" follows the system (android-ui.md §9).
val LANGUAGES = listOf("", "en", "ru")
/// The language chosen in the app. Android 13+ keeps it itself (and shows it in the
/// system settings of the app); on Android 12 the app keeps it and applies it to its
/// activity.
object Language {
private const val KEY = "language"
fun get(context: Context): String = if (Build.VERSION.SDK_INT >= 33) {
context.getSystemService(LocaleManager::class.java).applicationLocales.get(0)?.language ?: ""
} else {
context.getSharedPreferences("settings", Context.MODE_PRIVATE).getString(KEY, "") ?: ""
}
/// Returns true when the activity has to be recreated to show it (Android 12).
fun set(context: Context, tag: String): Boolean {
if (Build.VERSION.SDK_INT >= 33) {
context.getSystemService(LocaleManager::class.java).applicationLocales =
if (tag.isEmpty()) LocaleList.getEmptyLocaleList() else LocaleList.forLanguageTags(tag)
return false
}
context.getSharedPreferences("settings", Context.MODE_PRIVATE).edit().putString(KEY, tag).apply()
return true
}
/// The activity's context in the chosen language (Android 12 only).
fun wrap(base: Context): Context {
if (Build.VERSION.SDK_INT >= 33) return base
val tag = base.getSharedPreferences("settings", Context.MODE_PRIVATE).getString(KEY, "") ?: ""
if (tag.isEmpty()) return base
val config = Configuration(base.resources.configuration)
config.setLocales(LocaleList.forLanguageTags(tag))
return base.createConfigurationContext(config)
}
}

View file

@ -42,7 +42,7 @@ data class DbEntry(
val fileName: String,
val games: Int? = null,
val format: String? = null,
/// A sample keeps the name of the current language until it is renamed.
/// A sample shows the name of the current language until it is renamed (displayName).
val renamed: Boolean = false,
)
@ -59,9 +59,7 @@ class Library(private val context: Context) {
private fun load(): List<DbEntry> {
if (!file.exists()) return samples().also { save(it) }
val list = runCatching { json.decodeFromString<List<DbEntry>>(file.readText()) }.getOrDefault(emptyList())
val names = samples().associate { it.id to it.name }
return list.map { e -> if (e.source == Source.Sample && !e.renamed) e.copy(name = names[e.id] ?: e.name) else e }
return runCatching { json.decodeFromString<List<DbEntry>>(file.readText()) }.getOrDefault(emptyList())
}
private fun save(list: List<DbEntry>) {

View file

@ -23,6 +23,8 @@ import io.github.vrubelroman.chesspenguin.ChessPenguinApp
import io.github.vrubelroman.chesspenguin.R
import io.github.vrubelroman.chesspenguin.data.Notation
import io.github.vrubelroman.chesspenguin.data.DbEntry
import io.github.vrubelroman.chesspenguin.data.Source
import androidx.compose.ui.res.stringResource
@Composable
fun app(): ChessPenguinApp = LocalContext.current.applicationContext as ChessPenguinApp
@ -30,6 +32,15 @@ fun app(): ChessPenguinApp = LocalContext.current.applicationContext as ChessPen
/// 1248 → "1 248": digit groups apart, as on the desktop.
fun groupDigits(n: Int): String = "%,d".format(n).replace(',', ' ').replace(' ', ' ')
/// The name on the list: a sample keeps the name of the current language until renamed.
@Composable
fun displayName(e: DbEntry): String = when {
e.source != Source.Sample || e.renamed -> e.name
e.id == "sample-classics.pgn" -> stringResource(R.string.sample_classics)
e.id == "sample-alekhine.pgn" -> stringResource(R.string.sample_alekhine)
else -> e.name
}
/// The label of a database's format, for its badge (§10.6).
fun badge(e: DbEntry): String {
val ext = e.fileName.substringAfterLast('.', "").lowercase()

View file

@ -144,7 +144,7 @@ fun DatabasesScreen(onOpen: (String) -> Unit, onSettings: () -> Unit) {
}
}
renaming?.let { e ->
RenameDialog(e.name, onDismiss = { renaming = null }, onSave = {
RenameDialog(displayName(e), onDismiss = { renaming = null }, onSave = {
library.rename(e.id, it)
renaming = null
})
@ -197,7 +197,7 @@ private fun DatabaseRow(e: DbEntry, onOpen: () -> Unit, onRename: () -> Unit, on
) {
FormatBadge(format)
Column(Modifier.weight(1f).padding(start = 16.dp)) {
Text(e.name, style = MaterialTheme.typography.bodyLarge, maxLines = 1, overflow = TextOverflow.Ellipsis)
Text(displayName(e), style = MaterialTheme.typography.bodyLarge, maxLines = 1, overflow = TextOverflow.Ellipsis)
Text(
subtitle,
style = MaterialTheme.typography.bodyMedium,

View file

@ -123,7 +123,7 @@ fun GamesScreen(db: String, onBack: () -> Unit, onGame: (Int) -> Unit) {
LaunchedEffect(Unit) { focus.requestFocus() }
} else {
Column {
Text(entry?.name ?: "", maxLines = 1, overflow = TextOverflow.Ellipsis)
Text(entry?.let { displayName(it) } ?: "", maxLines = 1, overflow = TextOverflow.Ellipsis)
database?.let { d ->
val n = d.len().toInt()
val found = shown?.size

View file

@ -33,8 +33,17 @@ import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withLink
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.onClick
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.selected
import androidx.compose.ui.semantics.stateDescription
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import io.github.vrubelroman.chesspenguin.R
import io.github.vrubelroman.chesspenguin.theme.ChessColors
import io.github.vrubelroman.chesspenguin.theme.LocalChessColors
import io.github.vrubelroman.chesspenguin.theme.nagColor
@ -293,11 +302,30 @@ private fun MoveCell(
modifier: Modifier,
) {
val selected = id != null && id == current
// TalkBack reads "4, White, dxe5", and whether it is the current move.
val spoken = id?.let {
val n = game.nodes[it]
stringResource(if (white) R.string.move_white else R.string.move_black, n.moveNumber.toInt(), n.san + n.nagSymbols)
}
val currentLabel = stringResource(R.string.current_move)
// The whole cell takes the tap; the fill only hugs the move.
Box(
modifier
.fillMaxHeight()
.then(if (id != null) Modifier.clickable { onMove(id) } else Modifier)
.then(
if (spoken != null) {
Modifier.clearAndSetSemantics {
contentDescription = spoken
role = Role.Button
this.selected = selected
if (selected) stateDescription = currentLabel
onClick { onMove(id); true }
}
} else {
Modifier
},
)
.padding(start = 4.dp),
contentAlignment = Alignment.CenterStart,
) {

View file

@ -1,5 +1,6 @@
package io.github.vrubelroman.chesspenguin.ui
import android.app.Activity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
@ -36,11 +37,15 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import java.util.Locale
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import io.github.vrubelroman.chesspenguin.BuildConfig
import io.github.vrubelroman.chesspenguin.R
import io.github.vrubelroman.chesspenguin.data.LANGUAGES
import io.github.vrubelroman.chesspenguin.data.Language
import io.github.vrubelroman.chesspenguin.data.NetState
import io.github.vrubelroman.chesspenguin.data.Notation
import io.github.vrubelroman.chesspenguin.data.Theme
@ -115,6 +120,16 @@ fun SettingsScreen(onBack: () -> Unit, onAbout: () -> Unit) {
set { it.copy(theme = themeNames[i].first) }
}
}
val context = LocalContext.current
val languageNames = LANGUAGES.map {
if (it.isEmpty()) stringResource(R.string.theme_system) else Locale.forLanguageTag(it).let { l -> l.getDisplayLanguage(l).replaceFirstChar(Char::uppercase) }
}
val language = remember { Language.get(context) }
Item(stringResource(R.string.language), languageNames[LANGUAGES.indexOf(language).coerceAtLeast(0)]) {
choice = Choice(R.string.language, languageNames, LANGUAGES.indexOf(language).coerceAtLeast(0)) { i ->
if (Language.set(context, LANGUAGES[i])) (context as? Activity)?.recreate()
}
}
Group(stringResource(R.string.group_engine))
when {

View file

@ -130,4 +130,8 @@
<string name="engine_state_missing">не скачан</string>
<string name="found_of">Найдено %1$s из %2$s</string>
<string name="group_game">Партия</string>
<string name="move_white">%1$d, белые, %2$s</string>
<string name="move_black">%1$d, чёрные, %2$s</string>
<string name="current_move">текущий ход</string>
<string name="language">Язык</string>
</resources>

View file

@ -129,4 +129,8 @@
<string name="engine_state_missing">not downloaded</string>
<string name="found_of">Found %1$s of %2$s</string>
<string name="group_game">Game</string>
<string name="move_white">%1$d, White, %2$s</string>
<string name="move_black">%1$d, Black, %2$s</string>
<string name="current_move">current move</string>
<string name="language">Language</string>
</resources>