package com.peachpasswords.ui.vault import android.content.ClipData import android.content.ClipboardManager import android.content.Context import android.net.Uri import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.systemBarsPadding import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.rounded.ArrowBack import androidx.compose.material3.Icon import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.VisualTransformation import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.ViewModel import com.peachpasswords.ui.theme.LocalPeachAccent import com.peachpasswords.ui.theme.PeachColors import com.peachpasswords.ui.theme.PeachType import com.peachpasswords.ui.theme.ShellBackground import com.peachpasswords.vault.VaultEntry import com.peachpasswords.vault.VaultRepository import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext import javax.inject.Inject // ───────────────────────────────────────────────────────────────────────────── // Password strength scorer (local, dependency-free) // ───────────────────────────────────────────────────────────────────────────── /** * Lightweight 0–4 strength score from length + character-class variety. No * external lib, no network — this is a UI gate, not a security guarantee. * * Thresholds (deliberately simple and explainable): * - +1 if length >= 8 * - +1 if length >= 12 * - +1 if at least 3 of {lowercase, uppercase, digit, symbol} are present * - +1 if all 4 classes are present * Empty input scores 0. Result is clamped to 0..4. The C2 gate requires >= 3 * ("good"); see [strengthLabel]. */ internal fun scorePassword(pw: String): Int { if (pw.isEmpty()) return 0 var classes = 0 if (pw.any { it in 'a'..'z' }) classes++ if (pw.any { it in 'A'..'Z' }) classes++ if (pw.any { it.isDigit() }) classes++ if (pw.any { !it.isLetterOrDigit() }) classes++ var score = 0 if (pw.length >= 8) score++ if (pw.length >= 12) score++ if (classes >= 3) score++ if (classes >= 4) score++ return score.coerceIn(0, 4) } internal fun strengthLabel(score: Int): String = when (score) { 0 -> "very weak" 1 -> "weak" 2 -> "fair" 3 -> "good" else -> "strong" } // ───────────────────────────────────────────────────────────────────────────── // ViewModel // ───────────────────────────────────────────────────────────────────────────── @HiltViewModel class ExportViewModel @Inject constructor( private val vault: VaultRepository, @ApplicationContext private val appContext: Context, ) : ViewModel() { /** Active (non-deleted) entries to export. */ fun entries(): List = vault.getEntries() fun entryCount(): Int = vault.getEntries().size /** Encrypted PeachScript export (backup-password protected). */ fun buildPeachScript(backupPassword: String): String = PeachScriptExport.buildEncrypted(entries(), backupPassword) /** Unencrypted CSV export. */ fun buildCsv(): String = CsvExport.toCsv(entries()) /** Write [content] to the SAF [uri]; returns false on any IO failure. */ fun writeToUri(uri: Uri, content: String): Boolean = runCatching { appContext.contentResolver.openOutputStream(uri)?.use { it.write(content.toByteArray(Charsets.UTF_8)) } ?: return false true }.getOrDefault(false) } // ───────────────────────────────────────────────────────────────────────────── // Screen // ───────────────────────────────────────────────────────────────────────────── object ExportStrings { const val EMPTY_VAULT = "Your vault is empty — nothing to export." const val WEAK_PASSWORD = "Backup password too weak (score < 3)" const val MISMATCH = "Passwords don't match." const val SAVE_FAILED = "Couldn't write that file." const val SAVED = "Saved." const val COPIED = "Copied to clipboard." } private enum class ExportRoute { CHOOSE, PEACHSCRIPT, CSV } @Composable fun ExportScreen( onBack: () -> Unit, vm: ExportViewModel = hiltViewModel(), ) { var route by remember { mutableStateOf(ExportRoute.CHOOSE) } ShellBackground { Column( modifier = Modifier .fillMaxSize() .systemBarsPadding() .padding(horizontal = 20.dp, vertical = 16.dp), ) { Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) { Icon( Icons.AutoMirrored.Rounded.ArrowBack, contentDescription = "back", tint = PeachColors.T0, modifier = Modifier .size(22.dp) .clip(RoundedCornerShape(8.dp)) .clickable { if (route != ExportRoute.CHOOSE) route = ExportRoute.CHOOSE else onBack() }, ) Spacer(Modifier.width(14.dp)) Text( "Export", color = PeachColors.T0, style = TextStyle(fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 22.sp), ) } Spacer(Modifier.size(18.dp)) Column(Modifier.fillMaxSize().verticalScroll(rememberScrollState())) { when (route) { ExportRoute.CHOOSE -> ChooseView( onPeachScript = { route = ExportRoute.PEACHSCRIPT }, onCsv = { route = ExportRoute.CSV }, ) ExportRoute.PEACHSCRIPT -> PeachScriptExportView(vm) ExportRoute.CSV -> CsvExportView(vm) } } } } } // ---- Choose view ---------------------------------------------------------- @Composable private fun ChooseView(onPeachScript: () -> Unit, onCsv: () -> Unit) { OptionCard( title = "PeachScript (encrypted)", subtitle = "A backup encrypted with a separate password. Restores into Peach.", onClick = onPeachScript, ) Spacer(Modifier.size(12.dp)) OptionCard( title = "CSV (unencrypted)", subtitle = "Plain text for moving into another manager. Not encrypted.", onClick = onCsv, ) } @Composable private fun OptionCard(title: String, subtitle: String, onClick: () -> Unit) { Column( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(18.dp)) .background(PeachColors.SurfaceElevated) .border(1.dp, PeachColors.T3, RoundedCornerShape(18.dp)) .clickable(onClick = onClick) .padding(20.dp), ) { Text( title, color = PeachColors.T0, style = TextStyle(fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 22.sp), ) Spacer(Modifier.size(6.dp)) Text(subtitle, color = PeachColors.T1, style = PeachType.body) } } // ---- PeachScript sub-view ------------------------------------------------- @Composable private fun PeachScriptExportView(vm: ExportViewModel) { val accent = LocalPeachAccent.current val context = LocalContext.current val isEmpty = remember { vm.entryCount() == 0 } var password by remember { mutableStateOf("") } var confirm by remember { mutableStateOf("") } // Generated artefact (the encrypted JSON string), set after [Generate backup]. var output by remember { mutableStateOf(null) } var status by remember { mutableStateOf(null) } val score = remember(password) { scorePassword(password) } val matches = password.isNotEmpty() && password == confirm val strongEnough = score >= 3 val canGenerate = !isEmpty && strongEnough && matches val saveLauncher = rememberLauncherForActivityResult( ActivityResultContracts.CreateDocument("*/*"), ) { uri -> val text = output if (uri != null && text != null) { status = if (vm.writeToUri(uri, text)) ExportStrings.SAVED else ExportStrings.SAVE_FAILED } } GlassCard { Text( "create a\nbackup.", color = PeachColors.T0, style = TextStyle( fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 24.sp, lineHeight = 26.sp, ), ) Spacer(Modifier.size(10.dp)) Text( "Your vault will be encrypted with a separate backup password.", color = PeachColors.T1, style = PeachType.body, ) if (isEmpty) { Spacer(Modifier.size(16.dp)) InlineError(ExportStrings.EMPTY_VAULT) } Spacer(Modifier.size(18.dp)) FieldLabel("Backup password") Spacer(Modifier.size(6.dp)) PeachUnderlineField(value = password, onValueChange = { password = it; output = null; status = null }, isPassword = true) Spacer(Modifier.size(16.dp)) FieldLabel("Confirm backup password") Spacer(Modifier.size(6.dp)) PeachUnderlineField(value = confirm, onValueChange = { confirm = it; output = null; status = null }, isPassword = true) Spacer(Modifier.size(16.dp)) StrengthBar(score = score, accent = accent) Spacer(Modifier.size(6.dp)) Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text( "strength: ${strengthLabel(score)}", color = if (strongEnough) accent else PeachColors.T2, style = PeachType.labelMono, ) } if (password.isNotEmpty() && !strongEnough) { Spacer(Modifier.size(8.dp)) Text(ExportStrings.WEAK_PASSWORD, color = PeachColors.Breach, style = PeachType.bodyMono) } if (confirm.isNotEmpty() && !matches) { Spacer(Modifier.size(8.dp)) Text(ExportStrings.MISMATCH, color = PeachColors.Breach, style = PeachType.bodyMono) } } Spacer(Modifier.size(18.dp)) if (output == null) { AccentButton( label = "Generate backup", enabled = canGenerate, onClick = { output = vm.buildPeachScript(password) status = null }, ) } else { val text = output!! GhostButton("Copy to clipboard", accent) { val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager cm.setPrimaryClip(ClipData.newPlainText("peach backup", text)) status = ExportStrings.COPIED } Spacer(Modifier.size(8.dp)) GhostButton("Save to file", PeachColors.T1) { saveLauncher.launch("peach-backup.peach") } status?.let { Spacer(Modifier.size(12.dp)) Text( it, color = if (it == ExportStrings.SAVE_FAILED) PeachColors.Breach else accent, style = PeachType.labelMono, ) } } } @Composable private fun StrengthBar(score: Int, accent: Color) { // Four segments; filled count == score. Color shifts to accent once "good". val fillColor = if (score >= 3) accent else PeachColors.T2 Row(modifier = Modifier.fillMaxWidth()) { repeat(4) { i -> Box( modifier = Modifier .weight(1f) .height(4.dp) .padding(end = if (i < 3) 4.dp else 0.dp) .clip(RoundedCornerShape(2.dp)) .background(if (i < score) fillColor else PeachColors.T3), ) } } } // ---- CSV sub-view --------------------------------------------------------- @Composable private fun CsvExportView(vm: ExportViewModel) { val accent = LocalPeachAccent.current val isEmpty = remember { vm.entryCount() == 0 } var acknowledged by remember { mutableStateOf(false) } var status by remember { mutableStateOf(null) } val saveLauncher = rememberLauncherForActivityResult( ActivityResultContracts.CreateDocument("text/csv"), ) { uri -> if (uri != null) { val csv = vm.buildCsv() status = if (vm.writeToUri(uri, csv)) ExportStrings.SAVED else ExportStrings.SAVE_FAILED } } WarningCard(accent) { Text( "⚠ CSV exports are not encrypted. Store this file securely and delete it after importing.", color = PeachColors.T0, style = PeachType.body, ) Spacer(Modifier.size(16.dp)) CheckboxRow( label = "I understand this file is unencrypted", checked = acknowledged, accent = accent, onToggle = { acknowledged = !acknowledged }, ) } if (isEmpty) { Spacer(Modifier.size(16.dp)) InlineError(ExportStrings.EMPTY_VAULT) } Spacer(Modifier.size(18.dp)) AccentButton( label = "Export CSV", enabled = acknowledged && !isEmpty, onClick = { saveLauncher.launch("peach-export.csv") }, ) status?.let { Spacer(Modifier.size(12.dp)) Text( it, color = if (it == ExportStrings.SAVE_FAILED) PeachColors.Breach else accent, style = PeachType.labelMono, ) } } @Composable private fun WarningCard(accent: Color, content: @Composable () -> Unit) { Column( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(18.dp)) .background(accent.copy(alpha = 0.08f)) .border(1.dp, accent.copy(alpha = 0.35f), RoundedCornerShape(18.dp)) .padding(20.dp), content = { content() }, ) } @Composable private fun CheckboxRow(label: String, checked: Boolean, accent: Color, onToggle: () -> Unit) { Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().clickable(onClick = onToggle).padding(vertical = 4.dp), ) { Box( modifier = Modifier .size(18.dp) .clip(RoundedCornerShape(5.dp)) .background(if (checked) accent else Color.Transparent) .border(1.5.dp, if (checked) accent else PeachColors.T2, RoundedCornerShape(5.dp)), contentAlignment = Alignment.Center, ) { if (checked) Text("✓", color = Color(0xFF0C0A06), style = PeachType.tinyMono.copy(fontSize = 12.sp)) } Spacer(Modifier.width(12.dp)) Text(label, color = PeachColors.T0, style = PeachType.body) } } // ---- Shared building blocks (private to this draft) ----------------------- @Composable private fun InlineError(message: String) { Box( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(12.dp)) .background(PeachColors.BreachBg) .border(1.dp, PeachColors.BreachBorder, RoundedCornerShape(12.dp)) .padding(14.dp), ) { Text(message, color = PeachColors.Breach, style = PeachType.bodyMono) } } @Composable private fun FieldLabel(text: String) { Text(text.uppercase(), color = PeachColors.T2, style = PeachType.labelMono) } @Composable private fun PeachUnderlineField( value: String, onValueChange: (String) -> Unit, isPassword: Boolean = false, ) { val accent = LocalPeachAccent.current Column(Modifier.fillMaxWidth()) { BasicTextField( value = value, onValueChange = onValueChange, singleLine = true, textStyle = PeachType.bodyMono.copy(color = PeachColors.T0), cursorBrush = SolidColor(accent), visualTransformation = if (isPassword) PasswordVisualTransformation() else VisualTransformation.None, modifier = Modifier.fillMaxWidth().padding(vertical = 6.dp), ) Box(Modifier.fillMaxWidth().height(1.dp).background(PeachColors.T3)) } } @Composable private fun GlassCard(content: @Composable () -> Unit) { Column( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(18.dp)) .background(PeachColors.SurfaceElevated) .border(1.dp, PeachColors.T3, RoundedCornerShape(18.dp)) .padding(20.dp), content = { content() }, ) } @Composable private fun GhostButton(label: String, color: Color, onClick: () -> Unit) { Box( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(12.dp)) .border(1.dp, color.copy(alpha = 0.4f), RoundedCornerShape(12.dp)) .clickable(onClick = onClick) .padding(vertical = 12.dp), contentAlignment = Alignment.Center, ) { Text(label, color = color, style = PeachType.bodyMono) } } @Composable private fun AccentButton(label: String, onClick: () -> Unit, enabled: Boolean = true) { val accent = LocalPeachAccent.current Box( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(12.dp)) .background(if (enabled) accent else accent.copy(alpha = 0.3f)) .clickable(enabled = enabled, onClick = onClick) .padding(vertical = 14.dp), contentAlignment = Alignment.Center, ) { Text(label, color = Color(0xFF0C0A06), style = PeachType.bodyMono) } }