1package com.peachpasswords.ui.pairing23import androidx.compose.foundation.background4import androidx.compose.foundation.clickable5import androidx.compose.foundation.layout.Arrangement6import androidx.compose.foundation.layout.Box7import androidx.compose.foundation.layout.Column8import androidx.compose.foundation.layout.Row9import androidx.compose.foundation.layout.Spacer10import androidx.compose.foundation.layout.fillMaxSize11import androidx.compose.foundation.layout.fillMaxWidth12import androidx.compose.foundation.layout.height13import androidx.compose.foundation.layout.padding14import androidx.compose.foundation.layout.size15import androidx.compose.foundation.layout.systemBarsPadding16import androidx.compose.foundation.shape.CircleShape17import androidx.compose.foundation.shape.RoundedCornerShape18import androidx.compose.material3.Text19import androidx.compose.runtime.Composable20import androidx.compose.runtime.getValue21import androidx.compose.ui.Alignment22import androidx.compose.ui.Modifier23import androidx.compose.ui.draw.clip24import androidx.compose.ui.graphics.Color25import androidx.compose.ui.text.font.FontStyle26import androidx.compose.ui.text.style.TextAlign27import androidx.compose.ui.unit.dp28import androidx.compose.ui.unit.sp29import androidx.hilt.navigation.compose.hiltViewModel30import androidx.lifecycle.compose.collectAsStateWithLifecycle31import com.peachpasswords.sync.PairingPhase32import com.peachpasswords.ui.components.BirdAnimation33import com.peachpasswords.ui.components.PairingAnimState34import com.peachpasswords.ui.theme.LocalPeachAccent35import com.peachpasswords.ui.theme.PeachColors36import com.peachpasswords.ui.theme.PeachType37import com.peachpasswords.ui.theme.ShellBackground3839@Composable40fun ChirpPairScreen(41 onBack: () -> Unit,42 vm: ChirpPairViewModel = hiltViewModel(),43) {44 val progress by vm.progress.collectAsStateWithLifecycle()45 val sas by vm.sasCode.collectAsStateWithLifecycle()46 val accent = LocalPeachAccent.current47 val phase = progress.phase4849 ShellBackground {50 Column(modifier = Modifier.fillMaxSize().systemBarsPadding().padding(horizontal = 24.dp)) {51 52 Row(53 modifier = Modifier.fillMaxWidth().height(56.dp),54 verticalAlignment = Alignment.CenterVertically,55 ) {56 Text(57 text = "‹",58 color = PeachColors.T1,59 fontSize = 26.sp,60 modifier = Modifier.clip(CircleShape).clickable(onClick = onBack).padding(horizontal = 8.dp),61 )62 Spacer(Modifier.size(8.dp))63 Text("Sync", color = PeachColors.T0, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 19.sp)64 }6566 StatusChip(phase, accent)6768 Box(69 modifier = Modifier.fillMaxWidth().weight(1f),70 contentAlignment = Alignment.Center,71 ) {72 BirdAnimation(73 state = animState(phase),74 modifier = Modifier.fillMaxWidth().height(220.dp),75 )76 }7778 Copy(phase, progress.peerDeviceName, progress.errorMessage)7980 Spacer(Modifier.height(20.dp))81 PrimaryAction(phase, vm)82 Spacer(Modifier.height(28.dp))83 }8485 if (sas != null) {86 SasOverlay(87 code = sas!!,88 accent = accent,89 onConfirm = { if (vm.isDebug && progress.phase == PairingPhase.HANDSHAKE) vm.demoComplete() else vm.confirmSas(true) },90 onCancel = { vm.confirmSas(false); vm.reset() },91 )92 }93 }94}9596private fun animState(phase: PairingPhase): PairingAnimState = when (phase) {97 PairingPhase.IDLE -> PairingAnimState.WAITING98 PairingPhase.LISTENING, PairingPhase.CHIRPING, PairingPhase.HANDSHAKE -> PairingAnimState.LISTENING99 PairingPhase.PAIRED -> PairingAnimState.CONNECTED100 else -> PairingAnimState.WAITING101}102103@Composable104private fun StatusChip(phase: PairingPhase, accent: Color) {105 val (label, dot) = when (phase) {106 PairingPhase.IDLE -> "ready" to PeachColors.T2107 PairingPhase.LISTENING, PairingPhase.CHIRPING -> "listening" to accent108 PairingPhase.HANDSHAKE -> "verifying" to accent109 PairingPhase.PAIRED -> "paired" to Color(0xFF7FB069)110 PairingPhase.MIC_DENIED -> "mic blocked" to PeachColors.Breach111 else -> "interrupted" to PeachColors.Breach112 }113 Row(verticalAlignment = Alignment.CenterVertically) {114 Box(Modifier.size(6.dp).clip(CircleShape).background(dot))115 Spacer(Modifier.size(6.dp))116 Text(label, color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 9.sp)117 }118}119120@Composable121private fun Copy(phase: PairingPhase, peer: String?, error: String?) {122 val (headline, sub) = when (phase) {123 PairingPhase.IDLE -> "Pair a device" to "Peach pairs over a short, inaudible chirp — no codes typed, no servers."124 PairingPhase.LISTENING, PairingPhase.CHIRPING -> "Listening…" to "Hold this phone near the other device and start pairing there too."125 PairingPhase.HANDSHAKE -> "Almost there" to "Check that the code below matches on ${peer ?: "the other device"}."126 PairingPhase.PAIRED -> "Paired" to "${peer ?: "Your devices"} will now keep your vault in step over the local network."127 PairingPhase.TIMEOUT -> "No chirp heard" to "Nothing answered. Move the devices closer and try again."128 PairingPhase.MIC_DENIED -> "Microphone needed" to "Pairing listens for an inaudible chirp. Grant mic access to continue."129 PairingPhase.VERSION_MISMATCH -> "Version mismatch" to "Update Peach on both devices, then try pairing again."130 else -> "Pairing interrupted" to (error ?: "Something went wrong. Try again.")131 }132 Column(modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {133 Text(headline, color = PeachColors.T0, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 26.sp, textAlign = TextAlign.Center)134 Spacer(Modifier.height(8.dp))135 Text(sub, color = PeachColors.T2, fontFamily = PeachType.Body, fontSize = 13.sp, textAlign = TextAlign.Center)136 }137}138139@Composable140private fun PrimaryAction(phase: PairingPhase, vm: ChirpPairViewModel) {141 val accent = LocalPeachAccent.current142 when (phase) {143 PairingPhase.IDLE -> PillButton("Start pairing", accent, filled = true) { vm.startPairing() }144 PairingPhase.LISTENING, PairingPhase.CHIRPING, PairingPhase.HANDSHAKE ->145 PillButton("Cancel", PeachColors.T2, filled = false) { vm.reset() }146 PairingPhase.PAIRED -> PillButton("Done", accent, filled = true) { vm.reset() }147 else -> PillButton("Try again", accent, filled = true) { vm.startPairing() }148 }149 if (vm.isDebug && phase == PairingPhase.IDLE) {150 Spacer(Modifier.height(10.dp))151 Text(152 "demo flow",153 color = PeachColors.T3,154 fontFamily = PeachType.Mono,155 fontSize = 9.sp,156 modifier = Modifier.fillMaxWidth().clickable { vm.runDemo() }.padding(8.dp),157 textAlign = TextAlign.Center,158 )159 }160}161162@Composable163private fun PillButton(label: String, color: Color, filled: Boolean, onClick: () -> Unit) {164 val accent = LocalPeachAccent.current165 Box(166 modifier = Modifier167 .fillMaxWidth()168 .clip(RoundedCornerShape(14.dp))169 .background(if (filled) accent.copy(alpha = 0.14f) else Color.Transparent)170 .clickable(onClick = onClick)171 .padding(vertical = 15.dp),172 contentAlignment = Alignment.Center,173 ) {174 Text(label, color = if (filled) accent else color, fontFamily = PeachType.Mono, fontSize = 13.sp)175 }176}177178@Composable179private fun SasOverlay(code: String, accent: Color, onConfirm: () -> Unit, onCancel: () -> Unit) {180 Box(181 modifier = Modifier.fillMaxSize().background(Color(0xCC0C0A06)),182 contentAlignment = Alignment.Center,183 ) {184 Column(185 modifier = Modifier.fillMaxWidth().padding(horizontal = 36.dp),186 horizontalAlignment = Alignment.CenterHorizontally,187 ) {188 Text("Verify the code", color = PeachColors.T0, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 22.sp)189 Spacer(Modifier.height(6.dp))190 Text("It must match on both devices.", color = PeachColors.T2, fontFamily = PeachType.Body, fontSize = 13.sp, textAlign = TextAlign.Center)191 Spacer(Modifier.height(28.dp))192 Text(193 text = code,194 color = accent,195 fontFamily = PeachType.Mono,196 fontSize = 38.sp,197 letterSpacing = 6.sp,198 )199 Spacer(Modifier.height(34.dp))200 PillButton("They match — pair", accent, filled = true, onClick = onConfirm)201 Spacer(Modifier.height(10.dp))202 PillButton("Cancel", PeachColors.T2, filled = false, onClick = onCancel)203 }204 }205}