AppearanceSettingsScreen.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/appearance/AppearanceSettingsScreen.kt
1package eu.friedcutlet.yokan.ui.screens.settings.appearance23import androidx.compose.foundation.layout.Arrangement4import androidx.compose.foundation.layout.Column5import androidx.compose.foundation.layout.Row6import androidx.compose.foundation.layout.fillMaxWidth7import androidx.compose.foundation.layout.padding8import androidx.compose.material.icons.Icons9import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight10import androidx.compose.material.icons.filled.AspectRatio11import androidx.compose.material.icons.filled.ColorLens12import androidx.compose.material.icons.filled.Tune13import androidx.compose.material3.ElevatedCard14import androidx.compose.material3.Icon15import androidx.compose.material3.MaterialTheme16import androidx.compose.material3.Slider17import androidx.compose.material3.Text18import androidx.compose.runtime.Composable19import androidx.compose.runtime.getValue20import androidx.compose.runtime.mutableStateOf21import androidx.compose.runtime.remember22import androidx.compose.runtime.setValue23import androidx.compose.ui.Alignment24import androidx.compose.ui.Modifier25import androidx.compose.ui.text.font.FontWeight26import androidx.compose.ui.unit.dp27import androidx.lifecycle.Lifecycle28import androidx.lifecycle.compose.LifecycleEventEffect29import androidx.lifecycle.compose.collectAsStateWithLifecycle30import androidx.lifecycle.viewmodel.compose.viewModel31import eu.friedcutlet.yokan.app.YokanRuntime32import eu.friedcutlet.yokan.database.DbL10nLocale33import eu.friedcutlet.yokan.database.DbSettings34import eu.friedcutlet.yokan.services.appicons.AppIconDesign35import eu.friedcutlet.yokan.services.appicons.AppIconSelection36import eu.friedcutlet.yokan.services.system.isTvDevice37import eu.friedcutlet.yokan.ui.components.ChoiceDialog38import eu.friedcutlet.yokan.ui.navigation.SettingsPage39import eu.friedcutlet.yokan.ui.navigation.SettingsPage.CustomNavigation40import eu.friedcutlet.yokan.ui.screens.settings.RouteTile41import eu.friedcutlet.yokan.ui.screens.settings.SettingsScaffold42import eu.friedcutlet.yokan.ui.screens.settings.SettingsSection43import eu.friedcutlet.yokan.ui.screens.settings.SwitchTile44import eu.friedcutlet.yokan.ui.screens.settings.YokanMenuTile45import eu.friedcutlet.yokan.ui.theme.AppFontFamilyChoices46import eu.friedcutlet.yokan.ui.theme.FlexSchemes47import eu.friedcutlet.yokan.ui.theme.rememberSystemDark48import eu.friedcutlet.yokan.ui.theme.resolveDarkMode49import org.jetbrains.compose.resources.stringResource50import yokan.composeapp.generated.resources.Res51import yokan.composeapp.generated.resources.eink_device52import yokan.composeapp.generated.resources.eink_mode53import yokan.composeapp.generated.resources.eink_mode_help54import yokan.composeapp.generated.resources.eink_optimize_downloads55import yokan.composeapp.generated.resources.eink_optimize_downloads_help56import yokan.composeapp.generated.resources.eink_refresh57import yokan.composeapp.generated.resources.eink_refresh_every58import yokan.composeapp.generated.resources.eink_refresh_every_page59import yokan.composeapp.generated.resources.eink_refresh_off60import yokan.composeapp.generated.resources.eink_section61import eu.friedcutlet.yokan.database.DbEinkPreset62import eu.friedcutlet.yokan.database.EinkRefreshIntervals63import eu.friedcutlet.yokan.ui.screens.settings.ChoiceTile64import yokan.composeapp.generated.resources.settings_app_language65import yokan.composeapp.generated.resources.settings_blend_level66import yokan.composeapp.generated.resources.settings_dark_mode67import yokan.composeapp.generated.resources.settings_date_format68import yokan.composeapp.generated.resources.settings_follow_system_theme69import yokan.composeapp.generated.resources.settings_font70import yokan.composeapp.generated.resources.settings_pure_black_dark_mode71import yokan.composeapp.generated.resources.settings_relative_timestamps72import yokan.composeapp.generated.resources.settings_reorder_navigation73import yokan.composeapp.generated.resources.settings_reorder_navigation_subtitle74import yokan.composeapp.generated.resources.settings_section_appearance75import yokan.composeapp.generated.resources.settings_section_theme76import yokan.composeapp.generated.resources.settings_section_timestamp77import yokan.composeapp.generated.resources.settings_theme78import yokan.composeapp.generated.resources.ui_ui_scale7980@Composable81internal fun AppearanceSettingsRoute(82 title: String,83 settings: DbSettings,84 onUpdate: ((DbSettings) -> DbSettings) -> Unit,85 onBack: () -> Unit,86 onOpen: (SettingsPage) -> Unit,87) {88 val model = viewModel { AppIconViewModel(YokanRuntime.appIcons) }89 val iconState by model.state.collectAsStateWithLifecycle()90 LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { model.refresh() }91 AppearanceSettingsScreen(92 title, settings, onUpdate, onBack, onOpen,93 iconState = iconState,94 onIconPickerOpen = model::openPicker,95 onIconPickerDismiss = model::dismissPicker,96 onIconDesign = model::chooseDesign,97 onIconAppearance = model::chooseAppearance,98 onIconApply = model::applyDraft,99 onIconReset = { model.select(AppIconSelection.Default) },100 onIconRefresh = model::refresh,101 )102}103104@Composable105internal fun AppearanceSettingsScreen(106 title: String,107 settings: DbSettings,108 onUpdate: ((DbSettings) -> DbSettings) -> Unit,109 onBack: () -> Unit,110 onOpen: (SettingsPage) -> Unit,111 iconState: AppIconState,112 onIconPickerOpen: () -> Unit,113 onIconPickerDismiss: () -> Unit,114 onIconDesign: (AppIconDesign) -> Unit,115 onIconAppearance: (AppIconSelection) -> Unit,116 onIconApply: () -> Unit,117 onIconReset: () -> Unit,118 onIconRefresh: () -> Unit,119) {120 val blend = (settings.flexColorSchemeBlendLevel / 100.0).toFloat().coerceIn(0f, 1f)121 val systemDark = rememberSystemDark()122 val previewDarkTheme = resolveDarkMode(123 followSystem = settings.followSystemTheme,124 themeIsDark = settings.themeIsDark,125 systemDark = systemDark,126 )127 var themePickerOpen by remember { mutableStateOf(false) }128 var languagePickerOpen by remember { mutableStateOf(false) }129 var fontPickerOpen by remember { mutableStateOf(false) }130 var relativeTimestampPickerOpen by remember { mutableStateOf(false) }131 var dateFormatPickerOpen by remember { mutableStateOf(false) }132133 if (themePickerOpen) {134 ThemePreviewDialog(135 settings = settings,136 darkTheme = previewDarkTheme,137 blend = blend,138 onApply = { idx -> onUpdate { it.copy(flexSchemeColorIndex = idx) } },139 onDismiss = { themePickerOpen = false },140 )141 }142 if (languagePickerOpen) {143 // Mangayomi's 17 supported locales (app_localizations.dart).144 val choices = listOf(145 null,146 DbL10nLocale("ar"),147 DbL10nLocale("as"),148 DbL10nLocale("de"),149 DbL10nLocale("en"),150 DbL10nLocale("es"),151 DbL10nLocale("es", "419"),152 DbL10nLocale("fr"),153 DbL10nLocale("hi"),154 DbL10nLocale("id"),155 DbL10nLocale("it"),156 DbL10nLocale("ja"),157 DbL10nLocale("pt"),158 DbL10nLocale("pt", "BR"),159 DbL10nLocale("ru"),160 DbL10nLocale("th"),161 DbL10nLocale("tr"),162 DbL10nLocale("zh"),163 )164 ChoiceDialog(165 title = stringResource(Res.string.settings_app_language),166 options = choices,167 selected = choices.firstOrNull {168 it?.languageCode == settings.locale?.languageCode && it?.countryCode == settings.locale?.countryCode169 } ?: choices.firstOrNull { it?.languageCode == settings.locale?.languageCode } ?: choices[0],170 label = { it.languageLabel() },171 onSelect = { locale -> onUpdate { it.copy(locale = locale) } },172 onDismiss = { languagePickerOpen = false },173 )174 }175 if (fontPickerOpen) {176 val choices = AppFontFamilyChoices177 ChoiceDialog(178 title = stringResource(Res.string.settings_font),179 options = choices,180 selected = settings.appFontFamily ?: "Default",181 label = { it },182 onSelect = { font -> onUpdate { it.copy(appFontFamily = font.takeIf { it != "Default" }) } },183 onDismiss = { fontPickerOpen = false },184 )185 }186 if (relativeTimestampPickerOpen) {187 ChoiceDialog(188 title = stringResource(Res.string.settings_relative_timestamps),189 options = listOf(0, 1, 2),190 selected = settings.relativeTimesTamps.coerceIn(0, 2),191 label = ::relativeTimestampLabel,192 onSelect = { value -> onUpdate { it.copy(relativeTimesTamps = value) } },193 onDismiss = { relativeTimestampPickerOpen = false },194 )195 }196 if (dateFormatPickerOpen) {197 ChoiceDialog(198 title = stringResource(Res.string.settings_date_format),199 options = DateFormats,200 selected = settings.dateFormat,201 label = { it },202 onSelect = { format -> onUpdate { it.copy(dateFormat = format) } },203 onDismiss = { dateFormatPickerOpen = false },204 )205 }206207 if (iconState.pickerOpen) {208 AppIconPicker(iconState, onIconDesign, onIconAppearance, onIconApply, onIconReset, onIconRefresh, onIconPickerDismiss)209 }210 SettingsScaffold(title, onBack) {211 item { SettingsSection(stringResource(Res.string.settings_section_theme)) }212 item(key = "system-theme") {213 SwitchTile(214 stringResource(Res.string.settings_follow_system_theme),215 if (settings.followSystemTheme) "Currently following system" else null,216 settings.followSystemTheme,217 { value -> onUpdate { it.copy(followSystemTheme = value) } },218 )219 }220 if (!settings.followSystemTheme) {221 item { SwitchTile(stringResource(Res.string.settings_dark_mode), null, settings.themeIsDark, { onUpdate { it.copy(followSystemTheme = false, themeIsDark = !it.themeIsDark) } }) }222 }223 if (previewDarkTheme) {224 item { SwitchTile(stringResource(Res.string.settings_pure_black_dark_mode), null, settings.pureBlackDarkMode, { value -> onUpdate { it.copy(pureBlackDarkMode = value) } }) }225 }226 item {227 val schemeIndex = settings.flexSchemeColorIndex.coerceIn(0, FlexSchemes.lastIndex)228 val schemeName = FlexSchemes[schemeIndex].displayName229 YokanMenuTile(230 title = stringResource(Res.string.settings_theme),231 subtitle = schemeName,232 trailing = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null) },233 onClick = { themePickerOpen = true },234 )235 }236 item {237 ElevatedCard(modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 6.dp)) {238 Column(Modifier.padding(horizontal = 16.dp, vertical = 12.dp)) {239 Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {240 Icon(Icons.Filled.ColorLens, contentDescription = null, tint = MaterialTheme.colorScheme.primary)241 Text(stringResource(Res.string.settings_blend_level), fontWeight = FontWeight.Medium, modifier = Modifier.weight(1f))242 Text("${(blend * 100).toInt()}%", color = MaterialTheme.colorScheme.onSurfaceVariant, style = MaterialTheme.typography.labelMedium)243 }244 Slider(value = blend, onValueChange = { value -> onUpdate { it.copy(flexColorSchemeBlendLevel = value * 100.0) } })245 }246 }247 }248 item { MaterialKolorBlock(settings, onUpdate) }249 item { SettingsSection(stringResource(Res.string.eink_section)) }250 item(key = "eink-mode") {251 SwitchTile(252 stringResource(Res.string.eink_mode),253 stringResource(Res.string.eink_mode_help),254 settings.einkMode,255 { value -> onUpdate { it.copy(einkMode = value) } },256 )257 }258 if (settings.einkMode) {259 item(key = "eink-device") {260 ChoiceTile(stringResource(Res.string.eink_device), DbEinkPreset.entries, settings.einkPreset, { it.label }) { preset ->261 onUpdate { it.copy(einkPreset = preset) }262 }263 }264 item(key = "eink-refresh") {265 val labels = EinkRefreshIntervals.associateWith { pages ->266 when (pages) {267 0 -> stringResource(Res.string.eink_refresh_off)268 1 -> stringResource(Res.string.eink_refresh_every_page)269 else -> stringResource(Res.string.eink_refresh_every, pages)270 }271 }272 ChoiceTile(stringResource(Res.string.eink_refresh), EinkRefreshIntervals, settings.einkRefreshEveryPages, { labels.getValue(it) }) { pages ->273 onUpdate { it.copy(einkRefreshEveryPages = pages) }274 }275 }276 item(key = "eink-downloads") {277 val preset = settings.einkPreset278 SwitchTile(279 stringResource(Res.string.eink_optimize_downloads),280 stringResource(Res.string.eink_optimize_downloads_help, preset.label, preset.width, preset.height),281 settings.einkOptimizeDownloads,282 { value -> onUpdate { it.copy(einkOptimizeDownloads = value) } },283 )284 }285 }286 item { SettingsSection(stringResource(Res.string.settings_section_appearance)) }287 item(key = "app-icons") {288 AppIconTile(iconState, onIconPickerOpen)289 }290 item {291 SwitchTile(292 "Automatic UI scale",293 "Adjust scale from the platform text-size setting",294 settings.autoUiScale,295 { value -> onUpdate { it.copy(autoUiScale = value) } },296 )297 }298 item {299 ElevatedCard(modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 6.dp)) {300 Column(Modifier.padding(horizontal = 16.dp, vertical = 12.dp)) {301 val currentScale = settings.uiScale.toFloat().coerceIn(0.75f, 2.0f)302 Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {303 Icon(Icons.Filled.Tune, contentDescription = null, tint = MaterialTheme.colorScheme.primary)304 Text(stringResource(Res.string.ui_ui_scale), fontWeight = FontWeight.Medium, modifier = Modifier.weight(1f))305 Text("${(currentScale * 100).toInt()}%", color = MaterialTheme.colorScheme.onSurfaceVariant, style = MaterialTheme.typography.labelMedium)306 }307 Slider(308 value = currentScale,309 onValueChange = { value ->310 // Snap to 5% steps so the persisted number stays readable.311 val snapped = ((value * 20).toInt() / 20f).coerceIn(0.75f, 2.0f)312 onUpdate { it.copy(uiScale = snapped.toDouble()) }313 },314 enabled = !settings.autoUiScale,315 valueRange = 0.75f..2.0f,316 steps = 24,317 )318 Text(319 if (settings.autoUiScale) "Using automatic scaling" else "System default 100%",320 color = MaterialTheme.colorScheme.onSurfaceVariant,321 style = MaterialTheme.typography.bodySmall,322 )323 }324 }325 }326 if (isTvDevice()) {327 item {328 val currentTvScale = (settings.tvUiScale ?: 1.0).toFloat().coerceIn(0.85f, 1.25f)329 ElevatedCard(modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 6.dp)) {330 Column(Modifier.padding(horizontal = 16.dp, vertical = 12.dp)) {331 Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {332 Icon(Icons.Filled.AspectRatio, contentDescription = null, tint = MaterialTheme.colorScheme.primary)333 Text("TV interface scale", fontWeight = FontWeight.Medium, modifier = Modifier.weight(1f))334 Text("${(currentTvScale * 100).toInt()}%", color = MaterialTheme.colorScheme.onSurfaceVariant, style = MaterialTheme.typography.labelMedium)335 }336 Slider(337 value = currentTvScale,338 onValueChange = { value ->339 val snapped = ((value * 20).toInt() / 20f).coerceIn(0.85f, 1.25f)340 onUpdate { it.copy(tvUiScale = snapped.toDouble()) }341 },342 valueRange = 0.85f..1.25f,343 steps = 7,344 )345 Text(346 "Fine-tune sizing for TV screens and viewing distances",347 color = MaterialTheme.colorScheme.onSurfaceVariant,348 style = MaterialTheme.typography.bodySmall,349 )350 }351 }352 }353 item {354 SwitchTile(355 "Anime-only TV mode",356 "Hide manga and novel library tabs on TV layout",357 settings.animeOnlyTvMode ?: true,358 { value -> onUpdate { it.copy(animeOnlyTvMode = value) } },359 )360 }361 }362 item(key = "language") {363 YokanMenuTile(364 title = stringResource(Res.string.settings_app_language),365 subtitle = settings.locale.languageLabel(),366 trailing = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null) },367 onClick = { languagePickerOpen = true },368 )369 }370 item {371 YokanMenuTile(372 title = stringResource(Res.string.settings_font),373 subtitle = settings.appFontFamily ?: "Default",374 trailing = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null) },375 onClick = { fontPickerOpen = true },376 )377 }378 item { RouteTile(CustomNavigation, stringResource(Res.string.settings_reorder_navigation), stringResource(Res.string.settings_reorder_navigation_subtitle), onOpen = onOpen) }379 item { SettingsSection(stringResource(Res.string.settings_section_timestamp)) }380 item {381 YokanMenuTile(382 title = stringResource(Res.string.settings_relative_timestamps),383 subtitle = relativeTimestampLabel(settings.relativeTimesTamps),384 trailing = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null) },385 onClick = { relativeTimestampPickerOpen = true },386 )387 }388 item {389 YokanMenuTile(390 title = stringResource(Res.string.settings_date_format),391 subtitle = settings.dateFormat,392 trailing = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null) },393 onClick = { dateFormatPickerOpen = true },394 )395 }396 }397}398399private val DateFormats = listOf(400 "M/d/y",401 "MM/dd/yy",402 "dd/MM/yy",403 "yyyy-MM-dd",404 "dd MMM yyyy",405 "MMM dd, yyyy",406 "MM/dd/yyyy",407 "dd/MM/yyyy",408 "d/M/y",409 "y/M/d",410 "yyyy/MM/dd",411 "MMM d, y",412 "MMM d, yyyy",413 "MMMM d, yyyy",414)415416internal fun DbL10nLocale?.languageLabel(): String = when {417 this?.languageCode == null -> "Default"418 languageCode == "es" && countryCode == "419" -> "Español (Latinoamérica)"419 languageCode == "pt" && countryCode == "BR" -> "Português (Brasil)"420 else -> when (languageCode) {421 "ar" -> "العربية"422 "as" -> "অসমীয়া"423 "de" -> "Deutsch"424 "en" -> "English"425 "es" -> "Español"426 "fr" -> "Français"427 "hi" -> "हिन्दी"428 "id" -> "Bahasa Indonesia"429 "it" -> "Italiano"430 "ja" -> "日本語"431 "pt" -> "Português"432 "ru" -> "Русский"433 "th" -> "ไทย"434 "tr" -> "Türkçe"435 "zh" -> "中文"436 else -> languageCode.orEmpty()437 }438}439440private fun relativeTimestampLabel(value: Int): String = when (value) {441 0 -> "Off"442 1 -> "Short"443 else -> "Long"444}445