1package com.peachpasswords.ui.settings23import androidx.activity.compose.BackHandler4import androidx.compose.animation.core.Animatable5import androidx.compose.animation.core.animateDpAsState6import androidx.compose.animation.core.animateFloat7import androidx.compose.animation.core.animateFloatAsState8import androidx.compose.animation.core.infiniteRepeatable9import androidx.compose.animation.core.keyframes10import androidx.compose.animation.core.rememberInfiniteTransition11import androidx.compose.animation.core.tween12import androidx.compose.foundation.background13import androidx.compose.foundation.clickable14import androidx.compose.foundation.gestures.detectDragGestures15import androidx.compose.foundation.interaction.MutableInteractionSource16import androidx.compose.foundation.layout.Arrangement17import androidx.compose.foundation.layout.Box18import androidx.compose.foundation.layout.BoxWithConstraints19import androidx.compose.foundation.layout.Column20import androidx.compose.foundation.layout.Row21import androidx.compose.foundation.layout.Spacer22import androidx.compose.foundation.layout.fillMaxSize23import androidx.compose.foundation.layout.fillMaxWidth24import androidx.compose.foundation.layout.height25import androidx.compose.foundation.layout.padding26import androidx.compose.foundation.layout.size27import androidx.compose.foundation.layout.statusBarsPadding28import androidx.compose.foundation.rememberScrollState29import androidx.compose.foundation.shape.RoundedCornerShape30import androidx.compose.foundation.verticalScroll31import androidx.compose.runtime.Composable32import androidx.compose.runtime.LaunchedEffect33import androidx.compose.runtime.getValue34import androidx.compose.runtime.mutableStateOf35import androidx.compose.runtime.remember36import androidx.compose.runtime.rememberCoroutineScope37import androidx.compose.runtime.setValue38import androidx.compose.runtime.collectAsState39import androidx.compose.ui.Alignment40import androidx.compose.ui.Modifier41import androidx.compose.ui.draw.clip42import androidx.compose.ui.draw.drawWithCache43import androidx.compose.ui.draw.shadow44import androidx.compose.ui.draw.drawBehind45import androidx.compose.ui.geometry.Offset46import androidx.compose.ui.graphics.BlendMode47import androidx.compose.ui.graphics.Brush48import androidx.compose.ui.graphics.Color49import androidx.compose.ui.graphics.drawscope.DrawScope50import androidx.compose.ui.graphics.graphicsLayer51import androidx.compose.ui.input.pointer.pointerInput52import androidx.compose.ui.platform.LocalDensity53import androidx.compose.ui.unit.Dp54import androidx.compose.ui.text.font.FontStyle55import androidx.compose.ui.text.font.FontWeight56import androidx.compose.ui.text.style.TextOverflow57import androidx.compose.ui.unit.dp58import androidx.compose.ui.unit.em59import androidx.compose.ui.unit.sp60import androidx.compose.ui.zIndex61import androidx.hilt.navigation.compose.hiltViewModel62import androidx.compose.material3.Text63import com.peachpasswords.ui.theme.LocalPeachAccent64import com.peachpasswords.ui.theme.LocalReduceMotion65import com.peachpasswords.ui.theme.peachColors66import com.peachpasswords.ui.theme.PeachMotion67import com.peachpasswords.ui.theme.PeachTheme68import com.peachpasswords.ui.theme.PeachStackColors69import com.peachpasswords.ui.theme.PeachType70import com.peachpasswords.ui.theme.ShellBackground71import kotlinx.coroutines.delay72import kotlinx.coroutines.launch7374757677private val EXPANDED_H = 258.dp78private val TAB_H = 46.dp79private val TAB_OVERLAP = 14.dp80private val TAB_CARD_H = TAB_H + 4.dp818283private val DRAG_THRESHOLD = 60.dp848586internal enum class DragOutcome { EXPAND, COLLAPSE, NONE }87888990919293internal fun resolveStackDrag(dragOffsetPx: Float, thresholdPx: Float, isExpanded: Boolean): DragOutcome = when {94 dragOffsetPx < -thresholdPx && !isExpanded -> DragOutcome.EXPAND95 dragOffsetPx > thresholdPx && isExpanded -> DragOutcome.COLLAPSE96 else -> DragOutcome.NONE97}9899100101102103104105internal fun collapsedTabStride(106 available: Dp,107 expanded: Dp,108 tabCard: Dp,109 tabCount: Int,110 min: Dp,111 max: Dp,112): Dp {113 if (tabCount <= 0) return min114 val raw = (available - expanded - tabCard) / tabCount115 return raw.coerceIn(min, max)116}117118@Composable119fun SettingsScreen(120 onBack: () -> Unit,121 onNavigate: (com.peachpasswords.ui.PeachNav) -> Unit = {},122 vm: SettingsViewModel = hiltViewModel(),123) {124 val entries by vm.entries.collectAsState()125 val settings by vm.settings.collectAsState()126 val isPro by vm.isPro.collectAsState()127 val showCelebration by vm.showCelebration.collectAsState()128 val reduceMotion = LocalReduceMotion.current129 val accent = LocalPeachAccent.current130 val scope = rememberCoroutineScope()131132 var active by remember { mutableStateOf(CardId.Pro) }133 var deckOpen by remember { mutableStateOf(true) }134 var fanning by remember { mutableStateOf(false) }135 var celebrating by remember { mutableStateOf<CardId?>(null) }136137 val activeIndex = STACK_CARDS.indexOfFirst { it.id == active }.coerceAtLeast(0)138139 fun handleBack() {140 if (!deckOpen) deckOpen = true else onBack()141 }142 BackHandler { handleBack() }143144 145 fun celebrate() {146 if (fanning) return147 scope.launch {148 fanning = true149 active = CardId.Pro150 CELEBRATION_FAN.forEachIndexed { i, id ->151 delay(150L)152 active = id153 }154 delay(150L)155 active = CardId.Pro156 fanning = false157 celebrating = CardId.Pro158 delay(900L)159 celebrating = null160 }161 }162163 164 165 166 167 PeachTheme(darkTheme = true, accent = accent, reduceMotion = reduceMotion) {168 ShellBackground {169 Column(Modifier.fillMaxSize().statusBarsPadding().padding(horizontal = 12.dp)) {170 171 Row(172 Modifier.fillMaxWidth().padding(top = 10.dp, bottom = 8.dp),173 verticalAlignment = Alignment.CenterVertically,174 ) {175 Box(176 Modifier.size(28.dp).clip(RoundedCornerShape(999.dp))177 .background(Color(0x04F0E8D8))178 .clickable { handleBack() },179 contentAlignment = Alignment.Center,180 ) {181 Text("‹", color = peachColors.T1, fontFamily = PeachType.Mono, fontSize = 16.sp)182 }183 Text(184 "Settings",185 modifier = Modifier.weight(1f).padding(start = 10.dp),186 color = peachColors.T0,187 fontFamily = PeachType.Display,188 fontStyle = FontStyle.Italic,189 fontSize = 22.sp,190 )191 Text(192 "${activeIndex + 1} / ${STACK_CARDS.size}",193 color = peachColors.T2,194 fontFamily = PeachType.Mono,195 fontSize = 9.sp,196 letterSpacing = 0.22.em,197 )198 }199200 BoxWithConstraints(Modifier.fillMaxSize()) {201 val focusHeight = maxHeight - 8.dp202 203 val tabStride = collapsedTabStride(204 available = maxHeight,205 expanded = EXPANDED_H,206 tabCard = TAB_CARD_H,207 tabCount = STACK_CARDS.size - 1,208 min = TAB_H - TAB_OVERLAP,209 max = 72.dp,210 )211 STACK_CARDS.forEachIndexed { index, card ->212 val isActive = card.id == active213 val distance = when {214 index == activeIndex -> -1215 index > activeIndex -> index - activeIndex - 1216 else -> STACK_CARDS.size - activeIndex + index - 1217 }218 val targetTop = when {219 isActive -> 0.dp220 !deckOpen -> focusHeight + 44.dp + (distance * 8).dp221 else -> EXPANDED_H - TAB_OVERLAP + (distance * tabStride.value).dp222 }223 val targetHeight = when {224 isActive && deckOpen -> EXPANDED_H225 isActive -> focusHeight226 else -> TAB_CARD_H227 }228 val concealed = !deckOpen && !isActive229230 val animSpec = if (fanning) {231 tween<Float>(140, easing = PeachMotion.Celebrate)232 } else {233 tween<Float>(PeachMotion.Stack, easing = PeachMotion.StackSpring)234 }235 val top by animateDpAsState(236 targetTop,237 animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)238 else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),239 label = "top",240 )241 val cardH by animateDpAsState(242 targetHeight,243 animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)244 else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),245 label = "h",246 )247 val alpha by animateFloatAsState(if (concealed) 0f else 1f, animSpec, label = "a")248 val zIndex = if (isActive) 99f else (50 - distance).toFloat()249250 251 val shakeDy = remember { Animatable(0f) }252 val shakeRot = remember { Animatable(0f) }253 LaunchedEffect(celebrating == card.id) {254 if (celebrating == card.id && !reduceMotion) {255 launch {256 shakeRot.animateTo(0f, keyframes {257 durationMillis = 880258 0f at 0; -1.3f at 211; 0.9f at 422; -0.45f at 616; 0f at 757259 })260 }261 shakeDy.animateTo(0f, keyframes {262 durationMillis = 880263 0f at 0; -10f at 211; 5f at 422; -4f at 616; 2f at 757264 })265 }266 }267268 val density = LocalDensity.current269 StackCard(270 spec = card,271 active = isActive,272 focused = isActive && !deckOpen,273 lede = vm.lede(card.id, entries.count { it.deletedAt == null }, settings),274 showSparkle = card.id == CardId.Pro && isPro,275 onIconClick = if (card.id == CardId.Pro && isPro) ({ celebrate() }) else null,276 body = {277 CardBody(278 id = card.id,279 fg = card.fg,280 vm = vm,281 settings = settings,282 entriesCount = entries.count { it.deletedAt == null },283 onNavigate = onNavigate,284 )285 },286 modifier = Modifier287 .fillMaxWidth()288 .align(Alignment.TopStart)289 .zIndex(zIndex)290 291 292 .pointerInput(card.id, deckOpen, isActive) {293 var dragY = 0f294 val thresholdPx = with(density) { DRAG_THRESHOLD.toPx() }295 detectDragGestures(296 onDragStart = { dragY = 0f },297 onDrag = { change, amount -> change.consume(); dragY += amount.y },298 onDragEnd = {299 when (resolveStackDrag(dragY, thresholdPx, isActive && !deckOpen)) {300 DragOutcome.EXPAND -> { active = card.id; deckOpen = false }301 DragOutcome.COLLAPSE -> { deckOpen = true }302 DragOutcome.NONE -> {}303 }304 },305 )306 }307 .graphicsLayer {308 translationY = top.toPx() + with(density) { shakeDy.value.dp.toPx() }309 rotationZ = shakeRot.value310 this.alpha = alpha311 }312 .height(cardH),313 onClick = {314 if (isActive) {315 if (deckOpen) deckOpen = false316 } else {317 active = card.id318 deckOpen = false319 }320 },321 enabled = !concealed,322 )323 }324 }325 }326 if (showCelebration) {327 ProCelebration(onDone = { vm.dismissCelebration(); celebrate() })328 }329 }330 }331}332333@Composable334private fun StackCard(335 spec: StackCardSpec,336 active: Boolean,337 focused: Boolean,338 lede: String,339 showSparkle: Boolean,340 onIconClick: (() -> Unit)?,341 body: @Composable () -> Unit,342 modifier: Modifier,343 onClick: () -> Unit,344 enabled: Boolean,345) {346 val interaction = remember { MutableInteractionSource() }347 Box(348 modifier349 .shadow(if (active) 18.dp else 8.dp, RoundedCornerShape(PeachStackColors.CardRadius), clip = false)350 .clip(RoundedCornerShape(PeachStackColors.CardRadius))351 .drawBehind { drawRect(brush = cardGradient155(spec.bgStart, spec.bgEnd)) }352 .clickable(353 interactionSource = interaction,354 indication = null,355 enabled = enabled && !active || (active && !focused),356 ) { onClick() }357 .padding(358 horizontal = if (active) 18.dp else 16.dp,359 vertical = if (active) 16.dp else 12.dp,360 ),361 ) {362 if (showSparkle) SparkleDust(Modifier.fillMaxSize())363 if (active) {364 Column(Modifier.fillMaxSize()) {365 Row(366 Modifier.fillMaxWidth(),367 horizontalArrangement = Arrangement.SpaceBetween,368 verticalAlignment = Alignment.CenterVertically,369 ) {370 Text(371 spec.num,372 color = spec.fg.copy(alpha = 0.7f),373 fontFamily = PeachType.Mono,374 fontSize = 8.5.sp,375 letterSpacing = 0.26.em,376 )377 Box(378 Modifier.clip(RoundedCornerShape(999.dp))379 .then(if (onIconClick != null) Modifier.clickable { onIconClick() } else Modifier)380 .padding(2.dp),381 ) {382 CardIcon(spec.icon, spec.fg.copy(alpha = 0.85f))383 }384 }385 Text(386 spec.title,387 modifier = Modifier.padding(top = 6.dp),388 color = spec.fg,389 fontFamily = PeachType.Display,390 fontStyle = FontStyle.Italic,391 fontSize = 26.sp,392 )393 Text(394 lede,395 modifier = Modifier.padding(top = 4.dp),396 color = spec.fg.copy(alpha = 0.72f),397 fontFamily = PeachType.Display,398 fontStyle = FontStyle.Italic,399 fontSize = 12.5.sp,400 )401 402 403 Column(404 Modifier.fillMaxWidth().weight(1f)405 .padding(top = 14.dp)406 .verticalScroll(rememberScrollState()),407 ) {408 body()409 Spacer(Modifier.height(24.dp))410 }411 }412 } else {413 414 Box(Modifier.fillMaxSize(), contentAlignment = Alignment.CenterStart) {415 Text(416 spec.title,417 color = spec.fg,418 fontFamily = PeachType.Display,419 fontStyle = FontStyle.Italic,420 fontWeight = FontWeight.Normal,421 fontSize = 19.sp,422 maxLines = 1,423 overflow = TextOverflow.Ellipsis,424 )425 }426 }427 }428}429430431432433434435private fun DrawScope.cardGradient155(start: Color, end: Color): Brush {436 val a = Math.toRadians(155.0)437 val dx = kotlin.math.sin(a).toFloat()438 val dy = -kotlin.math.cos(a).toFloat()439 val cx = size.width / 2f440 val cy = size.height / 2f441 val half = (kotlin.math.abs(dx) * size.width + kotlin.math.abs(dy) * size.height) / 2f442 return Brush.linearGradient(443 colors = listOf(start, end),444 start = Offset(cx - dx * half, cy - dy * half),445 end = Offset(cx + dx * half, cy + dy * half),446 )447}448449450451452453454@Composable455private fun SparkleDust(modifier: Modifier = Modifier) {456 val reduceMotion = LocalReduceMotion.current457 val t = rememberInfiniteTransition(label = "dust")458 val drift by if (reduceMotion) {459 remember { mutableStateOf(0f) }460 } else {461 t.animateFloat(462 initialValue = 0f,463 targetValue = 1f,464 animationSpec = infiniteRepeatable(tween(14000, easing = androidx.compose.animation.core.LinearEasing)),465 label = "drift",466 )467 }468 Box(469 modifier.drawWithCache {470 val tile = 48.dp.toPx()471 val dx = drift * tile472 onDrawBehind {473 val cols = (size.width / tile).toInt() + 2474 val rows = (size.height / tile).toInt() + 2475 for (r in 0..rows) {476 for (c in 0..cols) {477 val baseX = c * tile + dx478 val baseY = r * tile + dx479 drawCircle(Color(0xFFFFF8DC), 0.6f, Offset(baseX % (size.width + tile), baseY % (size.height + tile)), alpha = 0.11f, blendMode = BlendMode.Screen)480 drawCircle(Color(0xFFFFEEAA), 1f, Offset((baseX + 19f) % (size.width + tile), (baseY + 15f) % (size.height + tile)), alpha = 0.08f, blendMode = BlendMode.Screen)481 }482 }483 }484 },485 )486}