Yokan / rebuild
Course 1 8624561

SecuritySettingsScreen.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/security/SecuritySettingsScreen.kt

implementation146 lines
1package eu.friedcutlet.yokan.ui.screens.settings.security23import androidx.compose.foundation.layout.padding4import androidx.compose.material.icons.Icons5import androidx.compose.material.icons.filled.Info6import androidx.compose.material3.Icon7import androidx.compose.material3.ListItem8import androidx.compose.material3.MaterialTheme9import androidx.compose.material3.Text10import androidx.compose.runtime.Composable11import androidx.compose.runtime.remember12import androidx.compose.ui.Modifier13import androidx.compose.ui.unit.dp14import eu.friedcutlet.yokan.app.YokanRuntime15import eu.friedcutlet.yokan.database.AppLockTimeoutChoices16import eu.friedcutlet.yokan.database.DbCategory17import eu.friedcutlet.yokan.database.DbItemType18import eu.friedcutlet.yokan.database.DbPrivacyPreferences19import eu.friedcutlet.yokan.database.DbSecureScreen20import eu.friedcutlet.yokan.database.DbSettings21import eu.friedcutlet.yokan.database.DbSource22import eu.friedcutlet.yokan.ui.screens.settings.ChoiceTile23import eu.friedcutlet.yokan.ui.screens.settings.InfoRow24import eu.friedcutlet.yokan.ui.screens.settings.SettingsScaffold25import eu.friedcutlet.yokan.ui.screens.settings.SettingsSection26import eu.friedcutlet.yokan.ui.screens.settings.SwitchTile27import eu.friedcutlet.yokan.ui.screens.settings.downloads.PolicyCategoryTile28import org.jetbrains.compose.resources.stringResource29import yokan.composeapp.generated.resources.Res30import yokan.composeapp.generated.resources.app_lock31import yokan.composeapp.generated.resources.app_lock_description32import yokan.composeapp.generated.resources.biometric_or_device_credential_not_available33import yokan.composeapp.generated.resources.require_biometric_or_device_credential34import eu.friedcutlet.yokan.ui.theme.YokanHorizontalDivider as HorizontalDivider3536@Composable37internal fun SecuritySettingsRoute(38    title: String,39    settings: DbSettings,40    onUpdate: ((DbSettings) -> DbSettings) -> Unit,41    onBack: () -> Unit,42) {43    val sources = remember { YokanRuntime.database.sources().filter { it.isAdded } }44    val appLock = YokanRuntime.appLock45    SecuritySettingsScreen(title, settings, sources, onUpdate, onBack, appLock.isSupported, appLock::requestUnlock)46}4748@Composable49internal fun SecuritySettingsScreen(50    title: String,51    settings: DbSettings,52    sources: List<DbSource>,53    onUpdate: ((DbSettings) -> DbSettings) -> Unit,54    onBack: () -> Unit,55    canAuth: Boolean,56    onRequestUnlock: (onResult: (Boolean) -> Unit) -> Unit,57) {58    val privacy = settings.features.privacy59    fun update(transform: (DbPrivacyPreferences) -> DbPrivacyPreferences) {60        onUpdate { it.copy(features = it.features.copy(privacy = transform(it.features.privacy))) }61    }62    val subtitle = if (canAuth) {63        stringResource(Res.string.require_biometric_or_device_credential)64    } else {65        stringResource(Res.string.biometric_or_device_credential_not_available)66    }67    SettingsScaffold(title, onBack) {68        item { SettingsSection("App lock") }69        item(key = "lock") {70            SwitchTile(71                stringResource(Res.string.app_lock),72                subtitle,73                settings.appLockEnabled,74                { desired ->75                    if (!canAuth) return@SwitchTile76                    onRequestUnlock { authenticated ->77                        if (authenticated) {78                            onUpdate { it.copy(appLockEnabled = desired) }79                        }80                    }81                },82                enabled = canAuth,83            )84        }85        item(key = "lock-timeout") {86            ChoiceTile("Lock when idle", AppLockTimeoutChoices, privacy.appLockTimeoutMinutes, ::lockTimeoutLabel, enabled = canAuth && settings.appLockEnabled) { minutes ->87                onRequestUnlock { ok -> if (ok) update { it.copy(appLockTimeoutMinutes = minutes) } }88            }89        }90        item { SettingsSection("Privacy") }91        item(key = "secure-screen") {92            ChoiceTile("Secure screen", DbSecureScreen.entries, privacy.secureScreen, ::secureScreenLabel) { mode -> update { it.copy(secureScreen = mode) } }93        }94        item(key = "secure-screen-info") {95            InfoRow("Android blocks screenshots and recents previews. iOS covers the app switcher preview and screen recording; screenshots cannot be blocked. Desktop and web cannot hide windows from capture.")96        }97        item(key = "hide-notification-content") {98            SwitchTile("Hide notification content", "Notifications show no titles", privacy.hideNotificationContent, { value -> update { it.copy(hideNotificationContent = value) } })99        }100        item(key = "incognito-sources") {101            // The category picker renders any id/name list; sources reuse it.102            PolicyCategoryTile(103                title = "Incognito sources",104                categories = sources.map { DbCategory(id = it.id, name = "${it.name} (${it.lang})", forItemType = DbItemType.Manga) },105                selected = privacy.incognitoSourceIds,106                allWhenEmpty = false,107                label = { it.name },108            ) { ids -> update { it.copy(incognitoSourceIds = ids) } }109        }110        item(key = "incognito-sources-info") {111            InfoRow("Titles from incognito sources leave no history or reading progress and stay private on Discord.")112        }113        item { HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp)) }114        item {115            ListItem(116                headlineContent = {117                    Text(118                        stringResource(Res.string.app_lock_description),119                        style = MaterialTheme.typography.bodyMedium,120                        color = MaterialTheme.colorScheme.onSurfaceVariant,121                    )122                },123                leadingContent = {124                    Icon(125                        Icons.Filled.Info,126                        contentDescription = null,127                        tint = MaterialTheme.colorScheme.primary,128                    )129                },130            )131        }132    }133}134135private fun lockTimeoutLabel(minutes: Int): String = when (minutes) {136    0 -> "Always"137    -1 -> "Never"138    1 -> "After 1 minute"139    else -> "After $minutes minutes"140}141142private fun secureScreenLabel(mode: DbSecureScreen): String = when (mode) {143    DbSecureScreen.Never -> "Never"144    DbSecureScreen.Incognito -> "In incognito mode"145    DbSecureScreen.Always -> "Always"146}147

Search chapters, reference and source filenames.