1package com.peachpasswords.ui.codex23import androidx.activity.compose.rememberLauncherForActivityResult4import androidx.activity.result.contract.ActivityResultContracts5import androidx.compose.foundation.background6import androidx.compose.foundation.clickable7import androidx.compose.foundation.layout.Arrangement8import androidx.compose.foundation.layout.Box9import androidx.compose.foundation.layout.Column10import androidx.compose.foundation.layout.Row11import androidx.compose.foundation.layout.Spacer12import androidx.compose.foundation.layout.fillMaxSize13import androidx.compose.foundation.layout.fillMaxWidth14import androidx.compose.foundation.layout.height15import androidx.compose.foundation.layout.padding16import androidx.compose.foundation.layout.size17import androidx.compose.foundation.shape.CircleShape18import androidx.compose.foundation.shape.RoundedCornerShape19import androidx.compose.foundation.text.BasicTextField20import androidx.compose.material3.LinearProgressIndicator21import androidx.compose.material3.Text22import androidx.compose.runtime.Composable23import androidx.compose.runtime.getValue24import androidx.compose.runtime.mutableStateOf25import androidx.compose.runtime.remember26import androidx.compose.runtime.setValue27import androidx.compose.ui.Alignment28import androidx.compose.ui.Modifier29import androidx.compose.ui.draw.clip30import androidx.compose.ui.graphics.Color31import androidx.compose.ui.graphics.SolidColor32import androidx.compose.ui.text.font.FontStyle33import androidx.compose.ui.unit.dp34import androidx.compose.ui.unit.sp35import androidx.hilt.navigation.compose.hiltViewModel36import androidx.lifecycle.compose.collectAsStateWithLifecycle37import com.peachpasswords.codex.CodexStep38import com.peachpasswords.ui.components.SecureFlagEffect39import com.peachpasswords.ui.theme.LocalPeachAccent40import com.peachpasswords.ui.theme.peachColors41import com.peachpasswords.ui.theme.PeachType42import com.peachpasswords.ui.theme.ShellBackground4344@Composable45fun CodexGenerateScreen(46 onBack: () -> Unit,47 onSetupRecovery: () -> Unit = {},48 vm: CodexViewModel = hiltViewModel(),49) {50 val state by vm.generate.collectAsStateWithLifecycle()51 val accent = LocalPeachAccent.current52 val saveLauncher = rememberLauncherForActivityResult(53 ActivityResultContracts.CreateDocument("application/pdf"),54 ) { uri -> uri?.let { vm.writePdf(it) } }5556 SecureFlagEffect() 5758 ShellBackground {59 Column(Modifier.fillMaxSize().padding(horizontal = 24.dp)) {60 CodexHeader("Codex backup", onBack)61 Spacer(Modifier.height(8.dp))62 Text(63 "A printable PDF of Aztec codes that encodes your encrypted vault. Keep it with your recovery words — together they restore everything, even offline.",64 color = peachColors.T2, fontFamily = PeachType.Body, fontSize = 13.sp,65 )66 Spacer(Modifier.height(28.dp))6768 when (val s = state) {69 is GenerateState.Idle -> {70 if (!vm.recoveryWordsSet) {71 Text("Set up recovery words first", color = peachColors.T1, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 19.sp)72 Spacer(Modifier.height(6.dp))73 Text(74 "A Codex backup only restores alongside your recovery words. Set them up before generating one.",75 color = peachColors.T2, fontFamily = PeachType.Body, fontSize = 13.sp,76 )77 Spacer(Modifier.height(20.dp))78 CodexButton("Set up recovery words", accent, filled = true, onClick = onSetupRecovery)79 } else {80 var w3 by remember { mutableStateOf("") }81 var w7 by remember { mutableStateOf("") }82 var w11 by remember { mutableStateOf("") }83 val ok = vm.gateMatches(w3, w7, w11)84 Text("Confirm your recovery words", color = peachColors.T1, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 19.sp)85 Spacer(Modifier.height(6.dp))86 Text("Enter the words at positions 3, 7, and 11.", color = peachColors.T2, fontFamily = PeachType.Body, fontSize = 13.sp)87 Spacer(Modifier.height(16.dp))88 GateField("Word #3", w3, accent) { w3 = it }89 GateField("Word #7", w7, accent) { w7 = it }90 GateField("Word #11", w11, accent) { w11 = it }91 Spacer(Modifier.height(20.dp))92 CodexButton(93 "Generate Codex PDF",94 if (ok) accent else peachColors.T2,95 filled = ok,96 ) { if (ok) vm.startGenerate(System.currentTimeMillis()) }97 }98 }99100 is GenerateState.Running -> Column {101 Text(stepLabel(s.step), color = peachColors.T1, fontFamily = PeachType.Mono, fontSize = 11.sp)102 Spacer(Modifier.height(10.dp))103 val frac = if (s.total > 0) s.current.toFloat() / s.total else 0f104 LinearProgressIndicator(105 progress = { if (s.step == CodexStep.AZTEC) frac else 0.5f },106 modifier = Modifier.fillMaxWidth().height(4.dp),107 color = accent, trackColor = peachColors.S2,108 )109 if (s.step == CodexStep.AZTEC) {110 Spacer(Modifier.height(6.dp))111 Text("${s.current} / ${s.total} codes", color = peachColors.T3, fontFamily = PeachType.Mono, fontSize = 9.sp)112 }113 }114115 is GenerateState.Done -> Column {116 SummaryRow("verify code", s.manifest.verifyCode, accent)117 SummaryRow("entries", s.manifest.entryCount.toString(), accent)118 SummaryRow("codes", "${s.manifest.totalChunks} (${s.manifest.parityChunks} recovery)", accent)119 SummaryRow("pages", s.manifest.pageCount.toString(), accent)120 Spacer(Modifier.height(24.dp))121 CodexButton("Save PDF…", accent, filled = true) { saveLauncher.launch("peach-codex.pdf") }122 Spacer(Modifier.height(10.dp))123 CodexButton("Generate again", peachColors.T2, filled = false) { vm.startGenerate(System.currentTimeMillis()) }124 }125126 is GenerateState.Error -> Column {127 Text("Couldn't generate", color = peachColors.Breach, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 19.sp)128 Spacer(Modifier.height(6.dp))129 Text(s.message, color = peachColors.T2, fontFamily = PeachType.Body, fontSize = 13.sp)130 Spacer(Modifier.height(20.dp))131 CodexButton("Try again", accent, filled = true) { vm.startGenerate(System.currentTimeMillis()) }132 }133 }134 }135 }136}137138private fun stepLabel(step: CodexStep): String = when (step) {139 CodexStep.COMPRESS -> "compressing vault…"140 CodexStep.ERASURE -> "adding recovery codes…"141 CodexStep.AZTEC -> "drawing Aztec codes…"142 CodexStep.VERIFY -> "verifying it rebuilds…"143 CodexStep.PDF -> "laying out the page…"144}145146@Composable147internal fun CodexHeader(title: String, onBack: () -> Unit) {148 Row(149 modifier = Modifier.fillMaxWidth().padding(top = 12.dp).height(48.dp),150 verticalAlignment = Alignment.CenterVertically,151 ) {152 Text("‹", color = peachColors.T1, fontSize = 26.sp,153 modifier = Modifier.clip(CircleShape).clickable(onClick = onBack).padding(horizontal = 8.dp))154 Spacer(Modifier.size(8.dp))155 Text(title, color = peachColors.T0, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 19.sp)156 }157}158159@Composable160private fun SummaryRow(label: String, value: String, accent: Color) {161 Row(Modifier.fillMaxWidth().padding(vertical = 7.dp), horizontalArrangement = Arrangement.SpaceBetween) {162 Text(label, color = peachColors.T2, fontFamily = PeachType.Mono, fontSize = 10.sp)163 Text(value, color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp)164 }165}166167@Composable168private fun GateField(label: String, value: String, accent: Color, onChange: (String) -> Unit) {169 Column(Modifier.fillMaxWidth().padding(vertical = 6.dp)) {170 Text(label, color = peachColors.T2, fontFamily = PeachType.Mono, fontSize = 10.sp)171 BasicTextField(172 value = value,173 onValueChange = { onChange(it) },174 singleLine = true,175 textStyle = PeachType.bodyMono.copy(color = peachColors.T0),176 cursorBrush = SolidColor(accent),177 modifier = Modifier.fillMaxWidth().padding(top = 4.dp),178 )179 Box(Modifier.fillMaxWidth().padding(top = 4.dp).height(1.dp).background(peachColors.T3))180 }181}182183@Composable184internal fun CodexButton(label: String, color: Color, filled: Boolean, onClick: () -> Unit) {185 val accent = LocalPeachAccent.current186 Box(187 modifier = Modifier.fillMaxWidth().clip(RoundedCornerShape(14.dp))188 .background(if (filled) accent.copy(alpha = 0.14f) else Color.Transparent)189 .clickable(onClick = onClick).padding(vertical = 15.dp),190 contentAlignment = Alignment.Center,191 ) {192 Text(label, color = if (filled) accent else color, fontFamily = PeachType.Mono, fontSize = 13.sp)193 }194}