package com.cottondb.android.ui.bootimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.ColumnScopeimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.Spacerimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.navigationBarsPaddingimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.statusBarsPaddingimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.rememberScrollStateimport androidx.compose.foundation.shape.CircleShapeimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.foundation.text.KeyboardOptionsimport androidx.compose.foundation.verticalScrollimport androidx.compose.material3.CircularProgressIndicatorimport androidx.compose.material3.HorizontalDividerimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.clipimport androidx.compose.ui.focus.FocusRequesterimport androidx.compose.ui.focus.focusRequesterimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.platform.LocalFocusManagerimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.text.TextStyleimport androidx.compose.ui.text.input.ImeActionimport androidx.compose.ui.text.input.KeyboardCapitalizationimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.cottondb.android.boot.BootRefusalimport com.cottondb.android.boot.BootStateimport com.cottondb.android.boot.JoinStateimport com.cottondb.android.design.ConstellationMarkimport com.cottondb.android.design.CottonFontsimport com.cottondb.android.design.CottonMetricsimport com.cottondb.android.design.CottonTextStylesimport com.cottondb.android.generated.CottonPaletteasGeneratedPalette/** * The app-root boot surface. It has no core dependency: the root collects the * two models and supplies every command, keeping one owner for native events. */@ComposablefunCottonBootGate( boot: BootState, join: JoinState, showJoin: Boolean, deviceName: String, appVersion: String, onShowJoin: () -> Unit, onStartLocal: (deviceName: String, appVersion: String) -> Unit, onJoin: (server: String, name: String, appVersion: String) -> Unit, onCancelJoin: () -> Unit, onRetry: () -> Unit, onSetName: (String) -> Unit, onErase: () -> Unit, onLeave: () -> Unit, readyContent: @Composable () -> Unit, modifier: Modifier = Modifier,) {when (boot) {BootState.Waiting -> SplashWait(modifier)BootState.Start -> if (showJoin) {JoinScreen(join, appVersion, onJoin, onCancelJoin, onSetName, modifier) } else {StartScreen(deviceName, appVersion, onStartLocal, onShowJoin, modifier) }isBootState.Refused -> if (showJoin) {JoinScreen(join, appVersion, onJoin, onCancelJoin, onSetName, modifier) } else {RefusalScreen(boot.refusal, onRetry, onShowJoin, onErase, onLeave, modifier) }BootState.Opening -> ProgressPaper( title = "Still opening your database", body = "This takes a moment when the app was closed suddenly — Cotton is folding in the last changes you made. It will open by itself.", modifier = modifier, )BootState.Bootstrapping -> ProgressPaper( title = "Downloading your database", body = "Your Cotton database is copying to this phone. This happens once, and it can take a few minutes on a big database — don't close the app.", modifier = modifier, )isBootState.Error -> ErrorPaper(boot.message, onRetry, modifier)isBootState.Ready -> readyContent() }}@ComposableprivatefunSplashWait(modifier: Modifier) {Box( modifier = modifier.fillMaxSize().background(GeneratedPalette.groundMuted), contentAlignment = Alignment.Center, ) {Column(horizontalAlignment = Alignment.CenterHorizontally) {ConstellationMark(Modifier.width(74.dp).height(106.dp))Spacer(Modifier.height(20.dp))CircularProgressIndicator( color = GeneratedPalette.inkTertiary, strokeWidth = CottonMetrics.hairline, modifier = Modifier.width(22.dp).height(22.dp), ) } }}@ComposableprivatefunStartScreen( deviceName: String, appVersion: String, onStartLocal: (String, String) -> Unit, onShowJoin: () -> Unit, modifier: Modifier,) = BootFrame(modifier) {Masthead()PaperSheet {Text("Your life, in a database you own. It lives on your devices and nowhere else.", style = CottonTextStyles.body, color = GeneratedPalette.inkBody, )Spacer(Modifier.height(18.dp))InkPill("Start a new database", "Start a new database") { onStartLocal(deviceName, appVersion) }Spacer(Modifier.height(14.dp))PaperLine()QuietAction("Join my database", "Join my database", onShowJoin)Text("Already have Cotton on a Mac or a phone? Join.", modifier = Modifier.padding(top = 14.dp), fontFamily = CottonFonts.hand, fontSize = 19.sp, color = GeneratedPalette.inkSecondary, ) }}@ComposableprivatefunJoinScreen( state: JoinState, appVersion: String, onJoin: (String, String, String) -> Unit, onCancel: () -> Unit, onSetName: (String) -> Unit, modifier: Modifier,) = BootFrame(modifier) {Masthead()PaperSheet {when (state) {isJoinState.Form -> {JoinForm(state, appVersion, onJoin)Spacer(Modifier.height(14.dp))PaperLine()QuietAction("Back", "Back", onCancel) }isJoinState.Pending -> PendingApproval(state, onCancel)JoinState.Opening -> OpeningJoin()isJoinState.Joined -> if (state.needsName) NameForm(state.error, onSetName) elseOpeningJoin() } }}@ComposableprivatefunJoinForm( state: JoinState.Form, appVersion: String, onJoin: (String, String, String) -> Unit,) {var server by remember { mutableStateOf(state.server) }var name by remember { mutableStateOf(state.name) }var localError by remember { mutableStateOf<String?>(null) }val nameFocus = remember { FocusRequester() }val focus = LocalFocusManager.currentLaunchedEffect(state.server, state.name, state.busy) {if (!state.busy) { server = state.server name = state.name } }fun submit() {if (server.trim().isEmpty()) { localError = "That's not a name or an address — type the short name your other device shows, or its full https:// address" } elseif (name.trim().isEmpty()) { localError = "Give this phone a name" } else { localError = null focus.clearFocus() onJoin(server.trim(), name.trim(), appVersion) } }Text("Join your Cotton", style = CottonTextStyles.screenTitle, color = GeneratedPalette.ink)Spacer(Modifier.height(10.dp))Text("Type the name your other device shows. Open Cotton there — the name is on its Settings, under Your devices. An address works too, if your database has no internet name.", style = CottonTextStyles.preview, color = GeneratedPalette.inkBody, )Spacer(Modifier.height(18.dp))RuledField( label = "The name your other device shows", value = server, onValueChange = { server = it }, hint = "such as home — or a full https:// address", keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.None, imeAction = ImeAction.Next), onImeAction = { nameFocus.requestFocus() }, )Spacer(Modifier.height(14.dp))RuledField( label = "This phone's name", value = name, onValueChange = { name = it }, focusRequester = nameFocus, keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences, imeAction = ImeAction.Done), onImeAction = ::submit, )val error = localError ?: state.errorSpacer(Modifier.height(18.dp))Row(verticalAlignment = Alignment.CenterVertically) {if (error != null) Text(error, Modifier.weight(1f), style = CottonTextStyles.preview, color = GeneratedPalette.statusError)elseif (state.busy) Text("Joining…", Modifier.weight(1f), fontFamily = CottonFonts.hand, fontSize = 20.sp, color = GeneratedPalette.ink)elseSpacer(Modifier.weight(1f))CircleAction(if (state.busy) "Joining…"else"Join", enabled = !state.busy, ::submit) }}@ComposableprivatefunPendingApproval(state: JoinState.Pending, onCancel: () -> Unit) {Column(horizontalAlignment = Alignment.CenterHorizontally) {Text("Asking ${state.server?.ifBlank { null } ?: "your other device"}", style = CottonTextStyles.screenTitle, color = GeneratedPalette.ink)Spacer(Modifier.height(18.dp))Text( state.digits, modifier = Modifier.semantics { contentDescription = "Server digits ${state.digits}" } .clip(RoundedCornerShape(12.dp)).background(GeneratedPalette.groundElevated).border(CottonMetrics.hairline, GeneratedPalette.hairline, RoundedCornerShape(12.dp)).padding(horizontal = 18.dp, vertical = 12.dp), fontFamily = CottonFonts.type, fontSize = 30.sp, color = GeneratedPalette.ink, )Spacer(Modifier.height(18.dp))Text("Approve this phone on any of your devices. It will ask if the phone shows these numbers.", style = CottonTextStyles.body, color = GeneratedPalette.inkBody, )Spacer(Modifier.height(18.dp))PaperLine()QuietAction("Cancel", "Cancel joining", onCancel) }}@ComposableprivatefunOpeningJoin() = Column(horizontalAlignment = Alignment.CenterHorizontally) {CircularProgressIndicator(color = GeneratedPalette.ink, strokeWidth = CottonMetrics.hairline)Spacer(Modifier.height(16.dp))Text("Bringing your records together…", fontFamily = CottonFonts.hand, fontSize = 20.sp, color = GeneratedPalette.ink)}@ComposableprivatefunNameForm(error: String?, onSetName: (String) -> Unit) {var name by remember { mutableStateOf("") }Text("What should we call you?", style = CottonTextStyles.screenTitle, color = GeneratedPalette.ink)Spacer(Modifier.height(10.dp))Text("Just how things read back to you. Change it any time.", style = CottonTextStyles.preview, color = GeneratedPalette.inkBody)Spacer(Modifier.height(18.dp))RuledField("", name, { name = it }, placeholder = "Enter name", keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), onImeAction = { onSetName(name) })if (error != null) {Spacer(Modifier.height(10.dp))Text(error, style = CottonTextStyles.preview, color = GeneratedPalette.statusError) }Spacer(Modifier.height(18.dp))InkPill("Continue", "Continue") { onSetName(name) }}@ComposableprivatefunRefusalScreen( refusal: BootRefusal, onRetry: () -> Unit, onJoin: () -> Unit, onErase: () -> Unit, onLeave: () -> Unit, modifier: Modifier,) = BootFrame(modifier) {PaperSheet {Text(refusalReason(refusal.code).uppercase(), style = CottonTextStyles.sectionLabel, color = GeneratedPalette.statusError)Spacer(Modifier.height(10.dp))Text(refusalTitle(refusal.code), style = CottonTextStyles.screenTitle, color = GeneratedPalette.ink)Spacer(Modifier.height(10.dp))PaperLine(GeneratedPalette.statusError)Spacer(Modifier.height(10.dp))Text(refusal.sentence ?: refusalBody(refusal.code), style = CottonTextStyles.preview, color = GeneratedPalette.inkBody)Spacer(Modifier.height(20.dp))InkPill("Try again", "Try again", onRetry)if (refusal.code == "device_revoked") {Spacer(Modifier.height(12.dp))PaperLine()QuietAction("Keep my copy", "Keep my copy", onJoin)QuietAction("Erase Cotton from this phone", "Erase Cotton from this phone", onErase, GeneratedPalette.statusError) } elseif (refusal.code == "scope_refused") {Spacer(Modifier.height(12.dp))PaperLine()QuietAction("Leave this database", "Leave this database", onLeave, GeneratedPalette.statusError) } }}@ComposableprivatefunProgressPaper(title: String, body: String, modifier: Modifier) = BootFrame(modifier) {PaperSheet {CircularProgressIndicator(color = GeneratedPalette.inkTertiary, strokeWidth = CottonMetrics.hairline)Spacer(Modifier.height(16.dp))Text(title, style = CottonTextStyles.screenTitle, color = GeneratedPalette.ink)Spacer(Modifier.height(10.dp))PaperLine()Spacer(Modifier.height(10.dp))Text(body, style = CottonTextStyles.preview, color = GeneratedPalette.inkBody) }}@ComposableprivatefunErrorPaper(message: String, onRetry: () -> Unit, modifier: Modifier) = BootFrame(modifier) {PaperSheet {Text("Something went wrong", style = CottonTextStyles.screenTitle, color = GeneratedPalette.ink)Spacer(Modifier.height(10.dp))Text(message, style = CottonTextStyles.caption, color = GeneratedPalette.inkTertiary)Spacer(Modifier.height(18.dp))InkPill("Try again", "Try again", onRetry) }}@ComposableprivatefunBootFrame(modifier: Modifier, content: @Composable ColumnScope.() -> Unit) {Column( modifier = modifier.fillMaxSize().background(GeneratedPalette.groundMuted).statusBarsPadding().navigationBarsPadding() .verticalScroll(rememberScrollState()).padding(horizontal = CottonMetrics.settingsMargin, vertical = 24.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { content() }}@ComposableprivatefunMasthead() {Spacer(Modifier.height(24.dp))ConstellationMark(Modifier.width(74.dp).height(106.dp))Spacer(Modifier.height(14.dp))Text("Cotton", fontFamily = CottonFonts.display, fontSize = 34.sp, color = GeneratedPalette.ink)Spacer(Modifier.height(24.dp))}@ComposableprivatefunPaperSheet(content: @Composable ColumnScope.() -> Unit) {Column( modifier = Modifier.fillMaxWidth().clip(RoundedCornerShape(CottonMetrics.cardRadius)).background(GeneratedPalette.ground) .border(CottonMetrics.hairline, GeneratedPalette.hairline, RoundedCornerShape(CottonMetrics.cardRadius)).padding(24.dp), content = content, )}@ComposableprivatefunInkPill(label: String, description: String, action: () -> Unit) {Text( label, modifier = Modifier.clip(RoundedCornerShape(30.dp)).background(GeneratedPalette.ink).clickable(onClick = action) .semantics { contentDescription = description }.padding(horizontal = 22.dp, vertical = 14.dp), fontFamily = CottonFonts.type, fontSize = 16.sp, color = GeneratedPalette.ground, )}@ComposableprivatefunCircleAction(label: String, enabled: Boolean, action: () -> Unit) {Text("→", modifier = Modifier.clip(CircleShape).background(if (enabled) GeneratedPalette.ink elseGeneratedPalette.inkTertiary) .clickable(enabled = enabled, onClick = action).semantics { contentDescription = label }.padding(horizontal = 17.dp, vertical = 13.dp), fontSize = 20.sp, color = GeneratedPalette.ground, )}@ComposableprivatefunQuietAction(label: String, description: String, action: () -> Unit, color: Color = GeneratedPalette.ink) {Row(modifier = Modifier.fillMaxWidth().clickable(onClick = action).semantics { contentDescription = description }.padding(vertical = 12.dp), verticalAlignment = Alignment.CenterVertically) {Text(label, Modifier.weight(1f), fontFamily = CottonFonts.type, fontSize = 15.sp, color = color)Text("→", fontSize = 16.sp, color = color) }}@ComposableprivatefunPaperLine(color: Color = GeneratedPalette.hairline) = HorizontalDivider(thickness = CottonMetrics.hairline, color = color)@ComposableprivatefunRuledField( label: String, value: String, onValueChange: (String) -> Unit, hint: String? = null, placeholder: String? = null, focusRequester: FocusRequester? = null, keyboardOptions: KeyboardOptions, onImeAction: () -> Unit,) {if (label.isNotEmpty()) Text(label, fontFamily = CottonFonts.typeItalic, fontSize = 13.sp, color = GeneratedPalette.inkSecondary)BasicTextField( value = value, onValueChange = onValueChange, modifier = Modifier.fillMaxWidth().then(if (focusRequester != null) Modifier.focusRequester(focusRequester) elseModifier).padding(vertical = 7.dp), textStyle = TextStyle(fontFamily = CottonFonts.type, fontSize = 17.sp, color = GeneratedPalette.ink), keyboardOptions = keyboardOptions, keyboardActions = androidx.compose.foundation.text.KeyboardActions(onDone = { onImeAction() }, onNext = { onImeAction() }), decorationBox = { inner -> if (value.isEmpty() && placeholder != null) Text(placeholder, fontFamily = CottonFonts.type, fontSize = 17.sp, color = GeneratedPalette.inkTertiary); inner() }, )PaperLine()if (hint != null) Text(hint, Modifier.padding(top = 4.dp), fontFamily = CottonFonts.typeItalic, fontSize = 12.sp, color = GeneratedPalette.inkSecondary)}privatefun refusalReason(code: String): String = when (code) {"device_not_approved" -> "Not approved""device_revoked" -> "Removed from your server""device_bad_signature" -> "Key not accepted""device_key_unreadable", "no_device_key" -> "Key unreadable""device_clock" -> "Clock out of step""rate_limited" -> "Paused by your server""snapshot_stalled" -> "No data for a minute""upgrade_required" -> "This build is out of date"else -> "Could not open"}privatefun refusalTitle(code: String): String = when (code) {"device_not_approved" -> "This phone isn't approved yet""device_revoked" -> "This phone was removed""device_bad_signature" -> "Your server didn't recognise this phone""device_clock" -> "Check the clock""upgrade_required" -> "Update Cotton to keep syncing"else -> "We couldn't open your Cotton"}privatefun refusalBody(code: String): String = when (code) {"device_not_approved" -> "Your server only opens for devices you've approved. Join again, then say yes on any device that's already in.""device_revoked" -> "Someone removed this phone from your server. The copy on this phone is untouched — keep it and join again when you're ready, or erase Cotton from this phone.""device_bad_signature" -> "Your server didn't accept this phone's signature — it hasn't approved this phone's key for that address. Join again from your Mac to pair it afresh.""device_clock" -> "This phone's clock or your Mac's is off by more than two minutes, so the server can't tell a fresh request from an old one. Fix the time and try again.""upgrade_required" -> "This build is older than your data. Update Cotton, then try again."else -> "Your data is safe — we just couldn't reach it. Try again in a moment."}