1package com.peachpasswords.ui.vault23import androidx.compose.foundation.Canvas4import androidx.compose.foundation.border5import androidx.compose.foundation.clickable6import 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.padding13import androidx.compose.foundation.layout.size14import androidx.compose.foundation.layout.systemBarsPadding15import androidx.compose.foundation.layout.width16import androidx.compose.foundation.lazy.LazyColumn17import androidx.compose.foundation.lazy.items18import androidx.compose.foundation.shape.RoundedCornerShape19import androidx.compose.material.icons.Icons20import androidx.compose.material.icons.automirrored.rounded.ArrowBack21import androidx.compose.material3.Icon22import androidx.compose.material3.Text23import androidx.compose.runtime.Composable24import androidx.compose.runtime.collectAsState25import androidx.compose.runtime.getValue26import androidx.compose.runtime.mutableStateMapOf27import androidx.compose.runtime.remember28import androidx.compose.ui.Alignment29import androidx.compose.ui.Modifier30import androidx.compose.ui.draw.clip31import androidx.compose.ui.geometry.Offset32import androidx.compose.ui.geometry.Size33import androidx.compose.ui.graphics.Color34import androidx.compose.ui.graphics.StrokeCap35import androidx.compose.ui.graphics.drawscope.Stroke36import androidx.compose.ui.text.TextStyle37import androidx.compose.ui.text.font.FontStyle38import androidx.compose.ui.text.style.TextAlign39import androidx.compose.ui.unit.dp40import androidx.compose.ui.unit.sp41import androidx.hilt.navigation.compose.hiltViewModel42import androidx.lifecycle.ViewModel43import com.peachpasswords.ui.components.FaviconInitials44import com.peachpasswords.ui.components.PeachMark45import com.peachpasswords.ui.theme.LocalPeachAccent46import com.peachpasswords.ui.theme.PeachColors47import com.peachpasswords.ui.theme.PeachType48import com.peachpasswords.ui.theme.ShellBackground49import com.peachpasswords.vault.VaultEntry50import com.peachpasswords.vault.VaultHealth51import com.peachpasswords.vault.VaultRepository52import com.peachpasswords.vault.domainKey53import com.peachpasswords.vault.hasPasskey54import dagger.hilt.android.lifecycle.HiltViewModel55import kotlinx.coroutines.flow.MutableStateFlow56import kotlinx.coroutines.flow.StateFlow57import kotlinx.coroutines.flow.asStateFlow58import javax.inject.Inject596061private val Amber = Color(0xFFC8A840)626364private const val OLD_PASSWORD_MS = 365L * 24 * 60 * 60 * 10006566@HiltViewModel67class VaultHealthViewModel @Inject constructor(68 private val vault: VaultRepository,69) : ViewModel() {70 val entries: StateFlow<List<VaultEntry>> = vault.entries7172 private val _report = MutableStateFlow(VaultHealth.buildReport(vault.getEntries()))73 val report: StateFlow<VaultHealth.VaultHealthReport> = _report.asStateFlow()7475 76 fun recompute() {77 _report.value = VaultHealth.buildReport(vault.getEntries())78 }79}8081@Composable82fun VaultHealthScreen(83 onBack: () -> Unit,84 onEditEntry: (String) -> Unit,85 vm: VaultHealthViewModel = hiltViewModel(),86) {87 val accent = LocalPeachAccent.current88 val entries by vm.entries.collectAsState()89 val report by vm.report.collectAsState()9091 val byId = remember(entries) { entries.associateBy { it.id } }92 val flags = remember(report) { report.entries.associateBy { it.entryId } }9394 val now = System.currentTimeMillis()95 val breached = report.entries.filter { it.isBreach }.mapNotNull { byId[it.entryId] }96 val reused = report.entries.filter { it.isReused }.mapNotNull { byId[it.entryId] }97 val weak = report.entries.filter { it.isWeak }.mapNotNull { byId[it.entryId] }98 val old = entries.filter { e ->99 e.passwordChangedAt?.let { (now - it) > OLD_PASSWORD_MS } == true100 }101 val noPasskey = entries.filter { !it.hasPasskey() }102103 val allHealthy = breached.isEmpty() && reused.isEmpty() &&104 weak.isEmpty() && old.isEmpty() && noPasskey.isEmpty()105106 val expanded = remember { mutableStateMapOf<String, Boolean>() }107108 ShellBackground {109 Column(110 modifier = Modifier111 .fillMaxSize()112 .systemBarsPadding()113 .padding(horizontal = 20.dp, vertical = 16.dp),114 ) {115 116 Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {117 Icon(118 Icons.AutoMirrored.Rounded.ArrowBack,119 contentDescription = "back",120 tint = PeachColors.T0,121 modifier = Modifier.size(22.dp).clip(RoundedCornerShape(8.dp)).clickable(onClick = onBack),122 )123 Spacer(Modifier.width(14.dp))124 Text(125 "vault health",126 color = PeachColors.T0,127 style = TextStyle(128 fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 22.sp,129 ),130 )131 Spacer(Modifier.weight(1f))132 GhostPill("check now", accent) { vm.recompute() }133 }134135 Spacer(Modifier.size(22.dp))136137 ScoreRing(score = report.vaultScore, accent = accent)138139 Spacer(Modifier.size(24.dp))140141 if (allHealthy) {142 Column(143 modifier = Modifier.fillMaxSize(),144 horizontalAlignment = Alignment.CenterHorizontally,145 ) {146 Spacer(Modifier.weight(1f))147 PeachMark(size = 60.dp)148 Spacer(Modifier.size(18.dp))149 Text(150 "Your vault looks healthy.",151 color = PeachColors.T1,152 textAlign = TextAlign.Center,153 style = TextStyle(154 fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 20.sp,155 ),156 )157 Spacer(Modifier.weight(1f))158 }159 } else {160 LazyColumn(modifier = Modifier.weight(1f)) {161 healthSection("Breached", breached, PeachColors.Breach, expanded, onEditEntry)162 healthSection("Reused", reused, Amber, expanded, onEditEntry)163 healthSection("Weak", weak, Amber, expanded, onEditEntry)164 healthSection("Old", old, PeachColors.T2, expanded, onEditEntry)165 healthSection("No passkey", noPasskey, PeachColors.T2, expanded, onEditEntry)166 }167 }168 }169 }170}171172private fun androidx.compose.foundation.lazy.LazyListScope.healthSection(173 title: String,174 bucket: List<VaultEntry>,175 badgeColor: Color,176 expanded: androidx.compose.runtime.snapshots.SnapshotStateMap<String, Boolean>,177 onEditEntry: (String) -> Unit,178) {179 item(key = "header_$title") {180 SectionHeader(181 title = title,182 count = bucket.size,183 badgeColor = badgeColor,184 expanded = expanded[title] == true,185 onToggle = { expanded[title] = !(expanded[title] == true) },186 )187 }188 if (expanded[title] == true) {189 items(bucket, key = { "${title}_${it.id}" }) { entry ->190 HealthEntryRow(entry = entry, onChange = { onEditEntry(entry.id) })191 }192 }193}194195@Composable196private fun SectionHeader(197 title: String,198 count: Int,199 badgeColor: Color,200 expanded: Boolean,201 onToggle: () -> Unit,202) {203 Row(204 verticalAlignment = Alignment.CenterVertically,205 modifier = Modifier206 .fillMaxWidth()207 .clip(RoundedCornerShape(10.dp))208 .clickable(onClick = onToggle)209 .padding(vertical = 12.dp),210 ) {211 Text(212 title,213 color = if (expanded) PeachColors.T0 else PeachColors.T1,214 style = TextStyle(215 fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 18.sp,216 ),217 )218 Spacer(Modifier.width(10.dp))219 CountBadge(count, badgeColor)220 }221}222223@Composable224private fun HealthEntryRow(entry: VaultEntry, onChange: () -> Unit) {225 Row(226 verticalAlignment = Alignment.CenterVertically,227 modifier = Modifier.fillMaxWidth().padding(start = 6.dp, top = 8.dp, bottom = 8.dp),228 ) {229 FaviconInitials(source = entry.url ?: entry.title, size = 26.dp)230 Spacer(Modifier.width(12.dp))231 Column(Modifier.weight(1f)) {232 Text(entry.title, color = PeachColors.T0, style = PeachType.entryNameList)233 val domain = entry.domainKey()234 if (!domain.isNullOrBlank()) {235 Text(domain, color = PeachColors.T2, style = PeachType.tinyMono)236 }237 }238 GhostPill("Change password", LocalPeachAccent.current, onChange)239 }240}241242@Composable243private fun ScoreRing(score: Int, accent: Color) {244 Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {245 Box(contentAlignment = Alignment.Center) {246 Canvas(modifier = Modifier.size(100.dp)) {247 val stroke = 8.dp.toPx()248 val inset = stroke / 2f249 val arcSize = Size(size.width - stroke, size.height - stroke)250 val topLeft = Offset(inset, inset)251 drawArc(252 color = PeachColors.T3,253 startAngle = -90f,254 sweepAngle = 360f,255 useCenter = false,256 topLeft = topLeft,257 size = arcSize,258 style = Stroke(width = stroke, cap = StrokeCap.Round),259 )260 drawArc(261 color = accent,262 startAngle = -90f,263 sweepAngle = (score.coerceIn(0, 100) / 100f) * 360f,264 useCenter = false,265 topLeft = topLeft,266 size = arcSize,267 style = Stroke(width = stroke, cap = StrokeCap.Round),268 )269 }270 Column(horizontalAlignment = Alignment.CenterHorizontally) {271 Text(272 score.toString(),273 color = PeachColors.T0,274 style = TextStyle(275 fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 44.sp,276 ),277 )278 Text(279 "vault score",280 color = PeachColors.T2,281 style = PeachType.labelMono.copy(fontSize = 9.sp),282 )283 }284 }285 }286}287288@Composable289private fun CountBadge(count: Int, color: Color) {290 Box(291 modifier = Modifier292 .clip(RoundedCornerShape(999.dp))293 .border(1.dp, color.copy(alpha = 0.4f), RoundedCornerShape(999.dp))294 .padding(horizontal = 9.dp, vertical = 3.dp),295 ) {296 Text(count.toString(), color = color, style = PeachType.sortPill.copy(fontSize = 10.sp))297 }298}299300@Composable301private fun GhostPill(label: String, color: Color, onClick: () -> Unit) {302 Box(303 modifier = Modifier304 .clip(RoundedCornerShape(999.dp))305 .border(1.dp, color.copy(alpha = 0.4f), RoundedCornerShape(999.dp))306 .clickable(onClick = onClick)307 .padding(horizontal = 12.dp, vertical = 5.dp),308 ) {309 Text(label, color = color, style = PeachType.sortPill.copy(fontSize = 10.sp))310 }311}