package com.cottondb.android.ui.settings import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.material3.Button import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import com.cottondb.android.design.CottonFonts import com.cottondb.android.design.CottonMetrics import com.cottondb.android.design.CottonTextStyles import com.cottondb.android.generated.CottonPalette import com.cottondb.android.settings.SettingsRead import 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. */ @Composable fun 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) } } } } } @Composable private fun LoadingSettings() = Text( "Loading settings…", modifier = Modifier.padding(top = 28.dp), style = CottonTextStyles.body, color = CottonPalette.inkSecondary, ) @Composable private 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") } } @Composable private 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) } } @Composable private 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") } } } @Composable private 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() } } @Composable private 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) }