Cotton code
210 lines · 7.8 KB · KotlinRaw
package com.cottondb.android.ui.settingsimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.layout.safeDrawingPaddingimport androidx.compose.foundation.rememberScrollStateimport androidx.compose.foundation.verticalScrollimport androidx.compose.material3.Buttonimport androidx.compose.material3.HorizontalDividerimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.semantics.Roleimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.text.style.TextOverflowimport androidx.compose.ui.unit.dpimport com.cottondb.android.design.CottonFontsimport com.cottondb.android.design.CottonMetricsimport com.cottondb.android.design.CottonTextStylesimport com.cottondb.android.generated.CottonPaletteimport com.cottondb.android.settings.SettingsReadimport com.cottondb.android.settings.SettingsState/** * The first Android Settings sheet. It receives navigation/platform actions from the * app root: join, local notification settings, device panel, leave/reset, and later * typed routes. Rows are deliberately absent until their action has a live owner. */@Composablefun SettingsScreen(    state: SettingsState,    onBack: () -> Unit,    onRefresh: () -> Unit,    onJoin: () -> Unit,    onOpenDevices: () -> Unit,    onOpenNotificationSettings: () -> Unit,    onSyncNow: () -> Unit,    onOpenBin: (() -> Unit)? = null,    binLabel: String? = null,    onOpenPrivacy: (() -> Unit)? = null,    onOpenTerms: (() -> Unit)? = null,    onOpenKeyboard: (() -> Unit)? = null,) {    val read = when (state) {        is SettingsState.Data -> state.read        is SettingsState.Error -> state.last        SettingsState.Loading -> null    }    Column(        modifier = Modifier            .fillMaxSize()            .background(CottonPalette.groundMuted)            .safeDrawingPadding()            .verticalScroll(rememberScrollState())            .padding(horizontal = CottonMetrics.settingsMargin, vertical = 20.dp),    ) {        Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {            Text(                "‹",                modifier = Modifier                    .clickable(role = Role.Button, onClick = onBack)                    .semantics { contentDescription = "Back" }                    .padding(end = 16.dp),                style = CottonTextStyles.pageTitle,                color = CottonPalette.ink,            )            Text(                "Settings",                modifier = Modifier.weight(1f),                style = CottonTextStyles.pageTitle.copy(fontFamily = CottonFonts.display),                color = CottonPalette.ink,            )        }        when {            read == null && state is SettingsState.Loading -> LoadingSettings()            read == null && state is SettingsState.Error -> SettingsFailure(state.message, onRefresh)            read != null -> SettingsBody(                read = read,                error = (state as? SettingsState.Error)?.message,                syncing = (state as? SettingsState.Data)?.syncing == true,                onRefresh = onRefresh,                onJoin = onJoin,                onOpenDevices = onOpenDevices,                onOpenNotificationSettings = onOpenNotificationSettings,                onSyncNow = onSyncNow,                onOpenBin = onOpenBin,                binLabel = binLabel,            )        }        onOpenKeyboard?.let { SettingsRow("Keyboard", "", it) }        if (onOpenPrivacy != null || onOpenTerms != null) {            SettingsSection("About") {                onOpenPrivacy?.let { SettingsRow("Privacy", "", it) }                onOpenTerms?.let { SettingsRow("Terms", "", it) }            }        }    }}@Composableprivate fun LoadingSettings() = Text(    "Loading settings…",    modifier = Modifier.padding(top = 28.dp),    style = CottonTextStyles.body,    color = CottonPalette.inkSecondary,)@Composableprivate fun SettingsFailure(message: String, onRetry: () -> Unit) = Column(    modifier = Modifier.padding(top = 28.dp),    verticalArrangement = Arrangement.spacedBy(12.dp),) {    Text(message, style = CottonTextStyles.body, color = CottonPalette.statusWarning)    Button(onClick = onRetry) { Text("Try again") }}@Composableprivate fun SettingsBody(    read: SettingsRead,    error: String?,    syncing: Boolean,    onRefresh: () -> Unit,    onJoin: () -> Unit,    onOpenDevices: () -> Unit,    onOpenNotificationSettings: () -> Unit,    onSyncNow: () -> Unit,    onOpenBin: (() -> Unit)?,    binLabel: String?,) {    error?.let { SettingsFailure(it, onRefresh) }    SettingsSection("This phone") {        SettingsRow("Notifications", "", onOpenNotificationSettings)        if (read.boot.paired && onOpenBin != null) {            SettingsRow("In the bin", binLabel.orEmpty(), onOpenBin)        }    }    SettingsSection(read.boot.serverName?.takeIf(String::isNotBlank) ?: "Your database") {        if (!read.boot.paired) {            SettingsRow("Not joined", "Join a database", onJoin)            return@SettingsSection        }        read.account?.ownerName?.let { SettingsRow("Your name", it, onClick = null) }        SettingsRow("Your devices", "", onOpenDevices)        SyncStatus(read, syncing, onSyncNow)    }}@Composableprivate fun SyncStatus(read: SettingsRead, syncing: Boolean, onSyncNow: () -> Unit) {    val status = read.sync    SettingsRow("Sync", read.syncLabel, onClick = null)    if (status?.state == SettingsRead.NO_SERVER_YET) {        Text(            "Everything stays on this phone",            modifier = Modifier.padding(start = 132.dp, top = 1.dp, bottom = 5.dp),            style = CottonTextStyles.caption.copy(fontFamily = CottonFonts.hand),            color = CottonPalette.inkSecondary,        )    }    if (read.hasServer) {        Button(            onClick = onSyncNow,            enabled = !syncing,            modifier = Modifier.padding(top = 6.dp).semantics { contentDescription = "Sync now" },        ) { Text(if (syncing) "Syncing…" else "Sync now") }    }}@Composableprivate fun SettingsSection(title: String, content: @Composable () -> Unit) = Column(    modifier = Modifier.padding(top = 24.dp),) {    Text(        title,        style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display),        color = CottonPalette.ink,    )    Column(modifier = Modifier.padding(top = 7.dp)) { content() }}@Composableprivate fun SettingsRow(label: String, value: String, onClick: (() -> Unit)?) {    val modifier = Modifier        .fillMaxWidth()        .then(if (onClick == null) Modifier else Modifier.clickable(role = Role.Button, onClick = onClick))        .semantics { contentDescription = if (value.isBlank()) label else "$label, $value" }        .padding(vertical = 10.dp)    Row(modifier, verticalAlignment = Alignment.CenterVertically) {        Text(label, modifier = Modifier.width(132.dp), style = CottonTextStyles.body, color = CottonPalette.inkSecondary)        Text(            value,            modifier = Modifier.weight(1f),            style = CottonTextStyles.body,            color = CottonPalette.ink,            maxLines = 1,            overflow = TextOverflow.Ellipsis,        )    }    HorizontalDivider(thickness = CottonMetrics.hairline, color = CottonPalette.hairline)}