Yokan / rebuild
Course 1 8624561

CookiesSettingsScreen.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/browse/CookiesSettingsScreen.kt

implementation111 lines
1package eu.friedcutlet.yokan.ui.screens.settings.browse23import 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.filled.Security10import androidx.compose.material3.Icon11import androidx.compose.material3.MaterialTheme12import androidx.compose.material3.Text13import androidx.compose.material3.TextButton14import androidx.compose.runtime.Composable15import androidx.compose.runtime.getValue16import androidx.compose.runtime.mutableStateOf17import androidx.compose.runtime.remember18import androidx.compose.runtime.setValue19import androidx.compose.ui.Alignment20import androidx.compose.ui.Modifier21import androidx.compose.ui.text.font.FontWeight22import androidx.compose.ui.unit.dp23import eu.friedcutlet.yokan.database.DbSettings24import eu.friedcutlet.yokan.ui.components.ConfirmDialog25import eu.friedcutlet.yokan.ui.screens.settings.ButtonRow26import eu.friedcutlet.yokan.ui.screens.settings.InfoRow27import eu.friedcutlet.yokan.ui.screens.settings.SettingsScaffold28import eu.friedcutlet.yokan.ui.screens.settings.SwitchTile29import org.jetbrains.compose.resources.stringResource30import yokan.composeapp.generated.resources.Res31import yokan.composeapp.generated.resources.ui_clear3233@Composable34internal fun CookiesSettingsScreen(35    title: String,36    settings: DbSettings,37    onUpdate: ((DbSettings) -> DbSettings) -> Unit,38    onBack: () -> Unit,39) {40    val cookies = settings.cookiesList.orEmpty()41    // Deliberately not saveable: revealing secrets lasts only while this screen is open.42    var revealValues by remember { mutableStateOf(false) }43    var pendingDestructive by remember { mutableStateOf<DestructiveAction?>(null) }44    pendingDestructive?.let { action ->45        ConfirmDialog(46            title = action.title,47            message = action.message,48            confirmLabel = stringResource(Res.string.ui_clear),49            danger = true,50            onConfirm = {51                action.onConfirm()52                pendingDestructive = null53            },54            onDismiss = { pendingDestructive = null },55        )56    }57    SettingsScaffold(title, onBack) {58        item(key = "cookies") {59            SwitchTile(60                title = "Show cookie values",61                subtitle = "Hidden by default. Revealing applies only while this screen is open.",62                value = revealValues,63                onValueChange = { revealValues = it },64            )65        }66        if (cookies.isEmpty()) {67            item(key = "empty") { InfoRow("No cookies stored yet. Cookies are captured automatically when sources set them via Set-Cookie headers.") }68        } else {69            cookies.distinctBy { it.host }.forEach { cookie ->70                item(key = "host:${cookie.host?.let { "known:$it" } ?: "unknown"}") {71                    Row(72                        modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp),73                        verticalAlignment = Alignment.CenterVertically,74                        horizontalArrangement = Arrangement.spacedBy(8.dp),75                    ) {76                        Icon(Icons.Filled.Security, contentDescription = null, tint = MaterialTheme.colorScheme.primary)77                        Column(Modifier.weight(1f)) {78                            Text(cookie.host ?: "Unknown host", fontWeight = FontWeight.SemiBold)79                            Text(80                                text = cookieValueText(cookie.cookie, revealValues),81                                color = MaterialTheme.colorScheme.onSurfaceVariant,82                                style = MaterialTheme.typography.bodySmall,83                            )84                        }85                        TextButton(onClick = {86                            pendingDestructive = DestructiveAction(87                                title = "Clear cookies for ${cookie.host ?: "Unknown host"}?",88                                message = "This may sign you out of this source. This cannot be undone.",89                            ) {90                                onUpdate { current ->91                                    current.copy(cookiesList = current.cookiesList.orEmpty().filterNot { it.host == cookie.host })92                                }93                            }94                        }) { Text(stringResource(Res.string.ui_clear)) }95                    }96                }97            }98            item(key = "clear-all") {99                ButtonRow("Clear all cookies", danger = true) {100                    pendingDestructive = DestructiveAction(101                        title = "Clear all cookies?",102                        message = "This removes cookies for every host and may sign you out of sources. This cannot be undone.",103                    ) { onUpdate { it.copy(cookiesList = emptyList()) } }104                }105            }106        }107    }108}109110internal fun cookieValueText(value: String?, revealed: Boolean): String =111    if (revealed) value.orEmpty() else "•••••••• (hidden)"112

Search chapters, reference and source filenames.