SettingsScreen.kt

17.1 KB · raw

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.interaction.MutableInteractionSource15import androidx.compose.foundation.layout.Arrangement16import androidx.compose.foundation.layout.Box17import androidx.compose.foundation.layout.BoxWithConstraints18import androidx.compose.foundation.layout.Column19import androidx.compose.foundation.layout.Row20import androidx.compose.foundation.layout.Spacer21import androidx.compose.foundation.layout.fillMaxSize22import androidx.compose.foundation.layout.fillMaxWidth23import androidx.compose.foundation.layout.height24import androidx.compose.foundation.layout.padding25import androidx.compose.foundation.layout.size26import androidx.compose.foundation.layout.statusBarsPadding27import androidx.compose.foundation.rememberScrollState28import androidx.compose.foundation.shape.RoundedCornerShape29import androidx.compose.foundation.verticalScroll30import androidx.compose.runtime.Composable31import androidx.compose.runtime.LaunchedEffect32import androidx.compose.runtime.getValue33import androidx.compose.runtime.mutableStateOf34import androidx.compose.runtime.remember35import androidx.compose.runtime.rememberCoroutineScope36import androidx.compose.runtime.setValue37import androidx.compose.runtime.collectAsState38import androidx.compose.ui.Alignment39import androidx.compose.ui.Modifier40import androidx.compose.ui.draw.clip41import androidx.compose.ui.draw.drawWithCache42import androidx.compose.ui.draw.shadow43import androidx.compose.ui.draw.drawBehind44import androidx.compose.ui.geometry.Offset45import androidx.compose.ui.graphics.BlendMode46import androidx.compose.ui.graphics.Brush47import androidx.compose.ui.graphics.Color48import androidx.compose.ui.graphics.drawscope.DrawScope49import androidx.compose.ui.graphics.graphicsLayer50import androidx.compose.ui.platform.LocalDensity51import androidx.compose.ui.text.font.FontStyle52import androidx.compose.ui.text.font.FontWeight53import androidx.compose.ui.text.style.TextOverflow54import androidx.compose.ui.unit.dp55import androidx.compose.ui.unit.em56import androidx.compose.ui.unit.sp57import androidx.compose.ui.zIndex58import androidx.hilt.navigation.compose.hiltViewModel59import androidx.compose.material3.Text60import com.peachpasswords.ui.theme.LocalReduceMotion61import com.peachpasswords.ui.theme.PeachColors62import com.peachpasswords.ui.theme.PeachMotion63import com.peachpasswords.ui.theme.PeachStackColors64import com.peachpasswords.ui.theme.PeachType65import com.peachpasswords.ui.theme.ShellBackground66import kotlinx.coroutines.delay67import kotlinx.coroutines.launch6869// Deck geometry — ported verbatim from StackDeck.tsx constants. The active card70// sits at top:0; every other card is a thin tab fanned below it, ordered71// cyclically starting just after the active card so the stack reads as a loop.72private val EXPANDED_H = 258.dp73private val TAB_H = 46.dp74private val TAB_OVERLAP = 14.dp75private val TAB_CARD_H = TAB_H + 4.dp7677@Composable78fun SettingsScreen(79    onBack: () -> Unit,80    onNavigate: (com.peachpasswords.ui.PeachNav) -> Unit = {},81    vm: SettingsViewModel = hiltViewModel(),82) {83    val entries by vm.entries.collectAsState()84    val settings by vm.settings.collectAsState()85    val isPro by vm.isPro.collectAsState()86    val showCelebration by vm.showCelebration.collectAsState()87    val reduceMotion = LocalReduceMotion.current88    val scope = rememberCoroutineScope()8990    var active by remember { mutableStateOf(CardId.Pro) }91    var deckOpen by remember { mutableStateOf(true) }92    var fanning by remember { mutableStateOf(false) }93    var celebrating by remember { mutableStateOf<CardId?>(null) }9495    val activeIndex = STACK_CARDS.indexOfFirst { it.id == active }.coerceAtLeast(0)9697    fun handleBack() {98        if (!deckOpen) deckOpen = true else onBack()99    }100    BackHandler { handleBack() }101102    // Pro celebration: replay every card in a quick fan, land back on Pro, shake.103    fun celebrate() {104        if (fanning) return105        scope.launch {106            fanning = true107            active = CardId.Pro108            CELEBRATION_FAN.forEachIndexed { i, id ->109                delay(150L)110                active = id111            }112            delay(150L)113            active = CardId.Pro114            fanning = false115            celebrating = CardId.Pro116            delay(900L)117            celebrating = null118        }119    }120121    ShellBackground {122        Column(Modifier.fillMaxSize().statusBarsPadding().padding(horizontal = 12.dp)) {123            // Masthead: back chevron · "Settings" · card counter.124            Row(125                Modifier.fillMaxWidth().padding(top = 10.dp, bottom = 8.dp),126                verticalAlignment = Alignment.CenterVertically,127            ) {128                Box(129                    Modifier.size(28.dp).clip(RoundedCornerShape(999.dp))130                        .background(Color(0x04F0E8D8))131                        .clickable { handleBack() },132                    contentAlignment = Alignment.Center,133                ) {134                    Text("‹", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 16.sp)135                }136                Text(137                    "Settings",138                    modifier = Modifier.weight(1f).padding(start = 10.dp),139                    color = PeachColors.T0,140                    fontFamily = PeachType.Display,141                    fontStyle = FontStyle.Italic,142                    fontSize = 22.sp,143                )144                Text(145                    "${activeIndex + 1} / ${STACK_CARDS.size}",146                    color = PeachColors.T2,147                    fontFamily = PeachType.Mono,148                    fontSize = 9.sp,149                    letterSpacing = 0.22.em,150                )151            }152153            BoxWithConstraints(Modifier.fillMaxSize()) {154                val focusHeight = maxHeight - 8.dp155                STACK_CARDS.forEachIndexed { index, card ->156                    val isActive = card.id == active157                    val distance = when {158                        index == activeIndex -> -1159                        index > activeIndex -> index - activeIndex - 1160                        else -> STACK_CARDS.size - activeIndex + index - 1161                    }162                    val targetTop = when {163                        isActive -> 0.dp164                        !deckOpen -> focusHeight + 44.dp + (distance * 8).dp165                        else -> EXPANDED_H - TAB_OVERLAP + (distance * (TAB_H - TAB_OVERLAP).value.toInt()).dp166                    }167                    val targetHeight = when {168                        isActive && deckOpen -> EXPANDED_H169                        isActive -> focusHeight170                        else -> TAB_CARD_H171                    }172                    val concealed = !deckOpen && !isActive173174                    val animSpec = if (fanning) {175                        tween<Float>(140, easing = PeachMotion.Celebrate)176                    } else {177                        tween<Float>(PeachMotion.Stack, easing = PeachMotion.StackSpring)178                    }179                    val top by animateDpAsState(180                        targetTop,181                        animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)182                        else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),183                        label = "top",184                    )185                    val cardH by animateDpAsState(186                        targetHeight,187                        animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)188                        else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),189                        label = "h",190                    )191                    val alpha by animateFloatAsState(if (concealed) 0f else 1f, animSpec, label = "a")192                    val zIndex = if (isActive) 99f else (50 - distance).toFloat()193194                    // Celebration shake — keyframed bob + tilt as the Pro card lands.195                    val shakeDy = remember { Animatable(0f) }196                    val shakeRot = remember { Animatable(0f) }197                    LaunchedEffect(celebrating == card.id) {198                        if (celebrating == card.id && !reduceMotion) {199                            launch {200                                shakeRot.animateTo(0f, keyframes {201                                    durationMillis = 880202                                    0f at 0; -1.3f at 211; 0.9f at 422; -0.45f at 616; 0f at 757203                                })204                            }205                            shakeDy.animateTo(0f, keyframes {206                                durationMillis = 880207                                0f at 0; -10f at 211; 5f at 422; -4f at 616; 2f at 757208                            })209                        }210                    }211212                    val density = LocalDensity.current213                    StackCard(214                        spec = card,215                        active = isActive,216                        focused = isActive && !deckOpen,217                        lede = vm.lede(card.id, entries.count { it.deletedAt == null }, settings),218                        showSparkle = card.id == CardId.Pro && isPro,219                        onIconClick = if (card.id == CardId.Pro && isPro) ({ celebrate() }) else null,220                        body = {221                            CardBody(222                                id = card.id,223                                fg = card.fg,224                                vm = vm,225                                settings = settings,226                                entriesCount = entries.count { it.deletedAt == null },227                                onNavigate = onNavigate,228                            )229                        },230                        modifier = Modifier231                            .fillMaxWidth()232                            .align(Alignment.TopStart)233                            .zIndex(zIndex)234                            .graphicsLayer {235                                translationY = top.toPx() + with(density) { shakeDy.value.dp.toPx() }236                                rotationZ = shakeRot.value237                                this.alpha = alpha238                            }239                            .height(cardH),240                        onClick = {241                            if (isActive) {242                                if (deckOpen) deckOpen = false243                            } else {244                                active = card.id245                                deckOpen = false246                            }247                        },248                        enabled = !concealed,249                    )250                }251            }252        }253        if (showCelebration) {254            ProCelebration(onDone = { vm.dismissCelebration(); celebrate() })255        }256    }257}258259@Composable260private fun StackCard(261    spec: StackCardSpec,262    active: Boolean,263    focused: Boolean,264    lede: String,265    showSparkle: Boolean,266    onIconClick: (() -> Unit)?,267    body: @Composable () -> Unit,268    modifier: Modifier,269    onClick: () -> Unit,270    enabled: Boolean,271) {272    val interaction = remember { MutableInteractionSource() }273    Box(274        modifier275            .shadow(if (active) 18.dp else 8.dp, RoundedCornerShape(PeachStackColors.CardRadius), clip = false)276            .clip(RoundedCornerShape(PeachStackColors.CardRadius))277            .drawBehind { drawRect(brush = cardGradient155(spec.bgStart, spec.bgEnd)) }278            .clickable(279                interactionSource = interaction,280                indication = null,281                enabled = enabled && !active || (active && !focused),282            ) { onClick() }283            .padding(284                horizontal = if (active) 18.dp else 16.dp,285                vertical = if (active) 16.dp else 12.dp,286            ),287    ) {288        if (showSparkle) SparkleDust(Modifier.fillMaxSize())289        if (active) {290            Column(Modifier.fillMaxSize()) {291                Row(292                    Modifier.fillMaxWidth(),293                    horizontalArrangement = Arrangement.SpaceBetween,294                    verticalAlignment = Alignment.CenterVertically,295                ) {296                    Text(297                        spec.num,298                        color = spec.fg.copy(alpha = 0.7f),299                        fontFamily = PeachType.Mono,300                        fontSize = 8.5.sp,301                        letterSpacing = 0.26.em,302                    )303                    Box(304                        Modifier.clip(RoundedCornerShape(999.dp))305                            .then(if (onIconClick != null) Modifier.clickable { onIconClick() } else Modifier)306                            .padding(2.dp),307                    ) {308                        CardIcon(spec.icon, spec.fg.copy(alpha = 0.85f))309                    }310                }311                Text(312                    spec.title,313                    modifier = Modifier.padding(top = 6.dp),314                    color = spec.fg,315                    fontFamily = PeachType.Display,316                    fontStyle = FontStyle.Italic,317                    fontSize = 26.sp,318                )319                Text(320                    lede,321                    modifier = Modifier.padding(top = 4.dp),322                    color = spec.fg.copy(alpha = 0.72f),323                    fontFamily = PeachType.Display,324                    fontStyle = FontStyle.Italic,325                    fontSize = 12.5.sp,326                )327                // Body scrolls independently; only revealed at full height in328                // focus mode, but kept mounted so deck-mode shows a preview.329                Column(330                    Modifier.fillMaxWidth().weight(1f)331                        .padding(top = 14.dp)332                        .verticalScroll(rememberScrollState()),333                ) {334                    body()335                    Spacer(Modifier.height(24.dp))336                }337            }338        } else {339            // Tab: just the title, single line, vertically centred.340            Box(Modifier.fillMaxSize(), contentAlignment = Alignment.CenterStart) {341                Text(342                    spec.title,343                    color = spec.fg,344                    fontFamily = PeachType.Display,345                    fontStyle = FontStyle.Italic,346                    fontWeight = FontWeight.Normal,347                    fontSize = 19.sp,348                    maxLines = 1,349                    overflow = TextOverflow.Ellipsis,350                )351            }352        }353    }354}355356/**357 * Settings card gradient at CSS 155° across the drawn card (spec §5.5). Computed358 * from the live draw size so the angle holds on any card height, unlike Compose's359 * default corner-to-corner linear gradient.360 */361private fun DrawScope.cardGradient155(start: Color, end: Color): Brush {362    val a = Math.toRadians(155.0)363    val dx = kotlin.math.sin(a).toFloat()364    val dy = -kotlin.math.cos(a).toFloat()365    val cx = size.width / 2f366    val cy = size.height / 2f367    val half = (kotlin.math.abs(dx) * size.width + kotlin.math.abs(dy) * size.height) / 2f368    return Brush.linearGradient(369        colors = listOf(start, end),370        start = Offset(cx - dx * half, cy - dy * half),371        end = Offset(cx + dx * half, cy + dy * half),372    )373}374375/**376 * Ambient drifting sparkle dust for the Pro card. Three offset dot grids slowly377 * translate diagonally and loop, screen-blended at low opacity — the Compose378 * analogue of the browser's `stk-pro-dust` keyframes.379 */380@Composable381private fun SparkleDust(modifier: Modifier = Modifier) {382    val reduceMotion = LocalReduceMotion.current383    val t = rememberInfiniteTransition(label = "dust")384    val drift by if (reduceMotion) {385        remember { mutableStateOf(0f) }386    } else {387        t.animateFloat(388            initialValue = 0f,389            targetValue = 1f,390            animationSpec = infiniteRepeatable(tween(14000, easing = androidx.compose.animation.core.LinearEasing)),391            label = "drift",392        )393    }394    Box(395        modifier.drawWithCache {396            val tile = 48.dp.toPx()397            val dx = drift * tile398            onDrawBehind {399                val cols = (size.width / tile).toInt() + 2400                val rows = (size.height / tile).toInt() + 2401                for (r in 0..rows) {402                    for (c in 0..cols) {403                        val baseX = c * tile + dx404                        val baseY = r * tile + dx405                        drawCircle(Color(0xFFFFF8DC), 0.6f, Offset(baseX % (size.width + tile), baseY % (size.height + tile)), alpha = 0.11f, blendMode = BlendMode.Screen)406                        drawCircle(Color(0xFFFFEEAA), 1f, Offset((baseX + 19f) % (size.width + tile), (baseY + 15f) % (size.height + tile)), alpha = 0.08f, blendMode = BlendMode.Screen)407                    }408                }409            }410        },411    )412}