SettingsScreen.kt

16.9 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 reduceMotion = LocalReduceMotion.current86    val scope = rememberCoroutineScope()8788    var active by remember { mutableStateOf(CardId.Pro) }89    var deckOpen by remember { mutableStateOf(true) }90    var fanning by remember { mutableStateOf(false) }91    var celebrating by remember { mutableStateOf<CardId?>(null) }9293    val activeIndex = STACK_CARDS.indexOfFirst { it.id == active }.coerceAtLeast(0)9495    fun handleBack() {96        if (!deckOpen) deckOpen = true else onBack()97    }98    BackHandler { handleBack() }99100    // Pro celebration: replay every card in a quick fan, land back on Pro, shake.101    fun celebrate() {102        if (fanning) return103        scope.launch {104            fanning = true105            active = CardId.Pro106            CELEBRATION_FAN.forEachIndexed { i, id ->107                delay(150L)108                active = id109            }110            delay(150L)111            active = CardId.Pro112            fanning = false113            celebrating = CardId.Pro114            delay(900L)115            celebrating = null116        }117    }118119    ShellBackground {120        Column(Modifier.fillMaxSize().statusBarsPadding().padding(horizontal = 12.dp)) {121            // Masthead: back chevron · "Settings" · card counter.122            Row(123                Modifier.fillMaxWidth().padding(top = 10.dp, bottom = 8.dp),124                verticalAlignment = Alignment.CenterVertically,125            ) {126                Box(127                    Modifier.size(28.dp).clip(RoundedCornerShape(999.dp))128                        .background(Color(0x04F0E8D8))129                        .clickable { handleBack() },130                    contentAlignment = Alignment.Center,131                ) {132                    Text("‹", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 16.sp)133                }134                Text(135                    "Settings",136                    modifier = Modifier.weight(1f).padding(start = 10.dp),137                    color = PeachColors.T0,138                    fontFamily = PeachType.Display,139                    fontStyle = FontStyle.Italic,140                    fontSize = 22.sp,141                )142                Text(143                    "${activeIndex + 1} / ${STACK_CARDS.size}",144                    color = PeachColors.T2,145                    fontFamily = PeachType.Mono,146                    fontSize = 9.sp,147                    letterSpacing = 0.22.em,148                )149            }150151            BoxWithConstraints(Modifier.fillMaxSize()) {152                val focusHeight = maxHeight - 8.dp153                STACK_CARDS.forEachIndexed { index, card ->154                    val isActive = card.id == active155                    val distance = when {156                        index == activeIndex -> -1157                        index > activeIndex -> index - activeIndex - 1158                        else -> STACK_CARDS.size - activeIndex + index - 1159                    }160                    val targetTop = when {161                        isActive -> 0.dp162                        !deckOpen -> focusHeight + 44.dp + (distance * 8).dp163                        else -> EXPANDED_H - TAB_OVERLAP + (distance * (TAB_H - TAB_OVERLAP).value.toInt()).dp164                    }165                    val targetHeight = when {166                        isActive && deckOpen -> EXPANDED_H167                        isActive -> focusHeight168                        else -> TAB_CARD_H169                    }170                    val concealed = !deckOpen && !isActive171172                    val animSpec = if (fanning) {173                        tween<Float>(140, easing = PeachMotion.Celebrate)174                    } else {175                        tween<Float>(PeachMotion.Stack, easing = PeachMotion.StackSpring)176                    }177                    val top by animateDpAsState(178                        targetTop,179                        animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)180                        else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),181                        label = "top",182                    )183                    val cardH by animateDpAsState(184                        targetHeight,185                        animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)186                        else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),187                        label = "h",188                    )189                    val alpha by animateFloatAsState(if (concealed) 0f else 1f, animSpec, label = "a")190                    val zIndex = if (isActive) 99f else (50 - distance).toFloat()191192                    // Celebration shake — keyframed bob + tilt as the Pro card lands.193                    val shakeDy = remember { Animatable(0f) }194                    val shakeRot = remember { Animatable(0f) }195                    LaunchedEffect(celebrating == card.id) {196                        if (celebrating == card.id && !reduceMotion) {197                            launch {198                                shakeRot.animateTo(0f, keyframes {199                                    durationMillis = 880200                                    0f at 0; -1.3f at 211; 0.9f at 422; -0.45f at 616; 0f at 757201                                })202                            }203                            shakeDy.animateTo(0f, keyframes {204                                durationMillis = 880205                                0f at 0; -10f at 211; 5f at 422; -4f at 616; 2f at 757206                            })207                        }208                    }209210                    val density = LocalDensity.current211                    StackCard(212                        spec = card,213                        active = isActive,214                        focused = isActive && !deckOpen,215                        lede = vm.lede(card.id, entries.count { it.deletedAt == null }, settings),216                        showSparkle = card.id == CardId.Pro && vm.isPro,217                        onIconClick = if (card.id == CardId.Pro && vm.isPro) ({ celebrate() }) else null,218                        body = {219                            CardBody(220                                id = card.id,221                                fg = card.fg,222                                vm = vm,223                                settings = settings,224                                entriesCount = entries.count { it.deletedAt == null },225                                onNavigate = onNavigate,226                            )227                        },228                        modifier = Modifier229                            .fillMaxWidth()230                            .align(Alignment.TopStart)231                            .zIndex(zIndex)232                            .graphicsLayer {233                                translationY = top.toPx() + with(density) { shakeDy.value.dp.toPx() }234                                rotationZ = shakeRot.value235                                this.alpha = alpha236                            }237                            .height(cardH),238                        onClick = {239                            if (isActive) {240                                if (deckOpen) deckOpen = false241                            } else {242                                active = card.id243                                deckOpen = false244                            }245                        },246                        enabled = !concealed,247                    )248                }249            }250        }251    }252}253254@Composable255private fun StackCard(256    spec: StackCardSpec,257    active: Boolean,258    focused: Boolean,259    lede: String,260    showSparkle: Boolean,261    onIconClick: (() -> Unit)?,262    body: @Composable () -> Unit,263    modifier: Modifier,264    onClick: () -> Unit,265    enabled: Boolean,266) {267    val interaction = remember { MutableInteractionSource() }268    Box(269        modifier270            .shadow(if (active) 18.dp else 8.dp, RoundedCornerShape(PeachStackColors.CardRadius), clip = false)271            .clip(RoundedCornerShape(PeachStackColors.CardRadius))272            .drawBehind { drawRect(brush = cardGradient155(spec.bgStart, spec.bgEnd)) }273            .clickable(274                interactionSource = interaction,275                indication = null,276                enabled = enabled && !active || (active && !focused),277            ) { onClick() }278            .padding(279                horizontal = if (active) 18.dp else 16.dp,280                vertical = if (active) 16.dp else 12.dp,281            ),282    ) {283        if (showSparkle) SparkleDust(Modifier.fillMaxSize())284        if (active) {285            Column(Modifier.fillMaxSize()) {286                Row(287                    Modifier.fillMaxWidth(),288                    horizontalArrangement = Arrangement.SpaceBetween,289                    verticalAlignment = Alignment.CenterVertically,290                ) {291                    Text(292                        spec.num,293                        color = spec.fg.copy(alpha = 0.7f),294                        fontFamily = PeachType.Mono,295                        fontSize = 8.5.sp,296                        letterSpacing = 0.26.em,297                    )298                    Box(299                        Modifier.clip(RoundedCornerShape(999.dp))300                            .then(if (onIconClick != null) Modifier.clickable { onIconClick() } else Modifier)301                            .padding(2.dp),302                    ) {303                        CardIcon(spec.icon, spec.fg.copy(alpha = 0.85f))304                    }305                }306                Text(307                    spec.title,308                    modifier = Modifier.padding(top = 6.dp),309                    color = spec.fg,310                    fontFamily = PeachType.Display,311                    fontStyle = FontStyle.Italic,312                    fontSize = 26.sp,313                )314                Text(315                    lede,316                    modifier = Modifier.padding(top = 4.dp),317                    color = spec.fg.copy(alpha = 0.72f),318                    fontFamily = PeachType.Display,319                    fontStyle = FontStyle.Italic,320                    fontSize = 12.5.sp,321                )322                // Body scrolls independently; only revealed at full height in323                // focus mode, but kept mounted so deck-mode shows a preview.324                Column(325                    Modifier.fillMaxWidth().weight(1f)326                        .padding(top = 14.dp)327                        .verticalScroll(rememberScrollState()),328                ) {329                    body()330                    Spacer(Modifier.height(24.dp))331                }332            }333        } else {334            // Tab: just the title, single line, vertically centred.335            Box(Modifier.fillMaxSize(), contentAlignment = Alignment.CenterStart) {336                Text(337                    spec.title,338                    color = spec.fg,339                    fontFamily = PeachType.Display,340                    fontStyle = FontStyle.Italic,341                    fontWeight = FontWeight.Normal,342                    fontSize = 19.sp,343                    maxLines = 1,344                    overflow = TextOverflow.Ellipsis,345                )346            }347        }348    }349}350351/**352 * Settings card gradient at CSS 155° across the drawn card (spec §5.5). Computed353 * from the live draw size so the angle holds on any card height, unlike Compose's354 * default corner-to-corner linear gradient.355 */356private fun DrawScope.cardGradient155(start: Color, end: Color): Brush {357    val a = Math.toRadians(155.0)358    val dx = kotlin.math.sin(a).toFloat()359    val dy = -kotlin.math.cos(a).toFloat()360    val cx = size.width / 2f361    val cy = size.height / 2f362    val half = (kotlin.math.abs(dx) * size.width + kotlin.math.abs(dy) * size.height) / 2f363    return Brush.linearGradient(364        colors = listOf(start, end),365        start = Offset(cx - dx * half, cy - dy * half),366        end = Offset(cx + dx * half, cy + dy * half),367    )368}369370/**371 * Ambient drifting sparkle dust for the Pro card. Three offset dot grids slowly372 * translate diagonally and loop, screen-blended at low opacity — the Compose373 * analogue of the browser's `stk-pro-dust` keyframes.374 */375@Composable376private fun SparkleDust(modifier: Modifier = Modifier) {377    val reduceMotion = LocalReduceMotion.current378    val t = rememberInfiniteTransition(label = "dust")379    val drift by if (reduceMotion) {380        remember { mutableStateOf(0f) }381    } else {382        t.animateFloat(383            initialValue = 0f,384            targetValue = 1f,385            animationSpec = infiniteRepeatable(tween(14000, easing = androidx.compose.animation.core.LinearEasing)),386            label = "drift",387        )388    }389    Box(390        modifier.drawWithCache {391            val tile = 48.dp.toPx()392            val dx = drift * tile393            onDrawBehind {394                val cols = (size.width / tile).toInt() + 2395                val rows = (size.height / tile).toInt() + 2396                for (r in 0..rows) {397                    for (c in 0..cols) {398                        val baseX = c * tile + dx399                        val baseY = r * tile + dx400                        drawCircle(Color(0xFFFFF8DC), 0.6f, Offset(baseX % (size.width + tile), baseY % (size.height + tile)), alpha = 0.11f, blendMode = BlendMode.Screen)401                        drawCircle(Color(0xFFFFEEAA), 1f, Offset((baseX + 19f) % (size.width + tile), (baseY + 15f) % (size.height + tile)), alpha = 0.08f, blendMode = BlendMode.Screen)402                    }403                }404            }405        },406    )407}