SettingsScreen.kt

16.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.geometry.Offset44import androidx.compose.ui.graphics.BlendMode45import androidx.compose.ui.graphics.Brush46import androidx.compose.ui.graphics.Color47import androidx.compose.ui.graphics.graphicsLayer48import androidx.compose.ui.platform.LocalDensity49import androidx.compose.ui.text.font.FontStyle50import androidx.compose.ui.text.font.FontWeight51import androidx.compose.ui.text.style.TextOverflow52import androidx.compose.ui.unit.dp53import androidx.compose.ui.unit.em54import androidx.compose.ui.unit.sp55import androidx.compose.ui.zIndex56import androidx.hilt.navigation.compose.hiltViewModel57import androidx.compose.material3.Text58import com.peachpasswords.ui.theme.LocalReduceMotion59import com.peachpasswords.ui.theme.PeachColors60import com.peachpasswords.ui.theme.PeachMotion61import com.peachpasswords.ui.theme.PeachStackColors62import com.peachpasswords.ui.theme.PeachType63import com.peachpasswords.ui.theme.ShellBackground64import kotlinx.coroutines.delay65import kotlinx.coroutines.launch6667// Deck geometry — ported verbatim from StackDeck.tsx constants. The active card68// sits at top:0; every other card is a thin tab fanned below it, ordered69// cyclically starting just after the active card so the stack reads as a loop.70private val EXPANDED_H = 258.dp71private val TAB_H = 46.dp72private val TAB_OVERLAP = 14.dp73private val TAB_CARD_H = TAB_H + 4.dp7475@Composable76fun SettingsScreen(77    onBack: () -> Unit,78    onNavigate: (com.peachpasswords.ui.PeachNav) -> Unit = {},79    vm: SettingsViewModel = hiltViewModel(),80) {81    val entries by vm.entries.collectAsState()82    val settings by vm.settings.collectAsState()83    val reduceMotion = LocalReduceMotion.current84    val scope = rememberCoroutineScope()8586    var active by remember { mutableStateOf(CardId.Pro) }87    var deckOpen by remember { mutableStateOf(true) }88    var fanning by remember { mutableStateOf(false) }89    var celebrating by remember { mutableStateOf<CardId?>(null) }9091    val activeIndex = STACK_CARDS.indexOfFirst { it.id == active }.coerceAtLeast(0)9293    fun handleBack() {94        if (!deckOpen) deckOpen = true else onBack()95    }96    BackHandler { handleBack() }9798    // Pro celebration: replay every card in a quick fan, land back on Pro, shake.99    fun celebrate() {100        if (fanning) return101        scope.launch {102            fanning = true103            active = CardId.Pro104            CELEBRATION_FAN.forEachIndexed { i, id ->105                delay(150L)106                active = id107            }108            delay(150L)109            active = CardId.Pro110            fanning = false111            celebrating = CardId.Pro112            delay(900L)113            celebrating = null114        }115    }116117    ShellBackground {118        Column(Modifier.fillMaxSize().statusBarsPadding().padding(horizontal = 12.dp)) {119            // Masthead: back chevron · "Settings" · card counter.120            Row(121                Modifier.fillMaxWidth().padding(top = 10.dp, bottom = 8.dp),122                verticalAlignment = Alignment.CenterVertically,123            ) {124                Box(125                    Modifier.size(28.dp).clip(RoundedCornerShape(999.dp))126                        .background(Color(0x04F0E8D8))127                        .clickable { handleBack() },128                    contentAlignment = Alignment.Center,129                ) {130                    Text("‹", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 16.sp)131                }132                Text(133                    "Settings",134                    modifier = Modifier.weight(1f).padding(start = 10.dp),135                    color = PeachColors.T0,136                    fontFamily = PeachType.Display,137                    fontStyle = FontStyle.Italic,138                    fontSize = 22.sp,139                )140                Text(141                    "${activeIndex + 1} / ${STACK_CARDS.size}",142                    color = PeachColors.T2,143                    fontFamily = PeachType.Mono,144                    fontSize = 9.sp,145                    letterSpacing = 0.22.em,146                )147            }148149            BoxWithConstraints(Modifier.fillMaxSize()) {150                val focusHeight = maxHeight - 8.dp151                STACK_CARDS.forEachIndexed { index, card ->152                    val isActive = card.id == active153                    val distance = when {154                        index == activeIndex -> -1155                        index > activeIndex -> index - activeIndex - 1156                        else -> STACK_CARDS.size - activeIndex + index - 1157                    }158                    val targetTop = when {159                        isActive -> 0.dp160                        !deckOpen -> focusHeight + 44.dp + (distance * 8).dp161                        else -> EXPANDED_H - TAB_OVERLAP + (distance * (TAB_H - TAB_OVERLAP).value.toInt()).dp162                    }163                    val targetHeight = when {164                        isActive && deckOpen -> EXPANDED_H165                        isActive -> focusHeight166                        else -> TAB_CARD_H167                    }168                    val concealed = !deckOpen && !isActive169170                    val animSpec = if (fanning) {171                        tween<Float>(140, easing = PeachMotion.Celebrate)172                    } else {173                        tween<Float>(PeachMotion.Stack, easing = PeachMotion.StackSpring)174                    }175                    val top by animateDpAsState(176                        targetTop,177                        animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)178                        else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),179                        label = "top",180                    )181                    val cardH by animateDpAsState(182                        targetHeight,183                        animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)184                        else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),185                        label = "h",186                    )187                    val alpha by animateFloatAsState(if (concealed) 0f else 1f, animSpec, label = "a")188                    val zIndex = if (isActive) 99f else (50 - distance).toFloat()189190                    // Celebration shake — keyframed bob + tilt as the Pro card lands.191                    val shakeDy = remember { Animatable(0f) }192                    val shakeRot = remember { Animatable(0f) }193                    LaunchedEffect(celebrating == card.id) {194                        if (celebrating == card.id && !reduceMotion) {195                            launch {196                                shakeRot.animateTo(0f, keyframes {197                                    durationMillis = 880198                                    0f at 0; -1.3f at 211; 0.9f at 422; -0.45f at 616; 0f at 757199                                })200                            }201                            shakeDy.animateTo(0f, keyframes {202                                durationMillis = 880203                                0f at 0; -10f at 211; 5f at 422; -4f at 616; 2f at 757204                            })205                        }206                    }207208                    val density = LocalDensity.current209                    StackCard(210                        spec = card,211                        active = isActive,212                        focused = isActive && !deckOpen,213                        lede = vm.lede(card.id, entries.count { it.deletedAt == null }, settings),214                        showSparkle = card.id == CardId.Pro && vm.isPro,215                        onIconClick = if (card.id == CardId.Pro && vm.isPro) ({ celebrate() }) else null,216                        body = {217                            CardBody(218                                id = card.id,219                                fg = card.fg,220                                vm = vm,221                                settings = settings,222                                entriesCount = entries.count { it.deletedAt == null },223                                onNavigate = onNavigate,224                            )225                        },226                        modifier = Modifier227                            .fillMaxWidth()228                            .align(Alignment.TopStart)229                            .zIndex(zIndex)230                            .graphicsLayer {231                                translationY = top.toPx() + with(density) { shakeDy.value.dp.toPx() }232                                rotationZ = shakeRot.value233                                this.alpha = alpha234                            }235                            .height(cardH),236                        onClick = {237                            if (isActive) {238                                if (deckOpen) deckOpen = false239                            } else {240                                active = card.id241                                deckOpen = false242                            }243                        },244                        enabled = !concealed,245                    )246                }247            }248        }249    }250}251252@Composable253private fun StackCard(254    spec: StackCardSpec,255    active: Boolean,256    focused: Boolean,257    lede: String,258    showSparkle: Boolean,259    onIconClick: (() -> Unit)?,260    body: @Composable () -> Unit,261    modifier: Modifier,262    onClick: () -> Unit,263    enabled: Boolean,264) {265    val interaction = remember { MutableInteractionSource() }266    Box(267        modifier268            .shadow(if (active) 18.dp else 8.dp, RoundedCornerShape(PeachStackColors.CardRadius), clip = false)269            .clip(RoundedCornerShape(PeachStackColors.CardRadius))270            .background(Brush.linearGradient(listOf(spec.bgStart, spec.bgEnd)))271            .clickable(272                interactionSource = interaction,273                indication = null,274                enabled = enabled && !active || (active && !focused),275            ) { onClick() }276            .padding(277                horizontal = if (active) 18.dp else 16.dp,278                vertical = if (active) 16.dp else 12.dp,279            ),280    ) {281        if (showSparkle) SparkleDust(Modifier.fillMaxSize())282        if (active) {283            Column(Modifier.fillMaxSize()) {284                Row(285                    Modifier.fillMaxWidth(),286                    horizontalArrangement = Arrangement.SpaceBetween,287                    verticalAlignment = Alignment.CenterVertically,288                ) {289                    Text(290                        spec.num,291                        color = spec.fg.copy(alpha = 0.7f),292                        fontFamily = PeachType.Mono,293                        fontSize = 8.5.sp,294                        letterSpacing = 0.26.em,295                    )296                    Box(297                        Modifier.clip(RoundedCornerShape(999.dp))298                            .then(if (onIconClick != null) Modifier.clickable { onIconClick() } else Modifier)299                            .padding(2.dp),300                    ) {301                        CardIcon(spec.icon, spec.fg.copy(alpha = 0.85f))302                    }303                }304                Text(305                    spec.title,306                    modifier = Modifier.padding(top = 6.dp),307                    color = spec.fg,308                    fontFamily = PeachType.Display,309                    fontStyle = FontStyle.Italic,310                    fontSize = 26.sp,311                )312                Text(313                    lede,314                    modifier = Modifier.padding(top = 4.dp),315                    color = spec.fg.copy(alpha = 0.72f),316                    fontFamily = PeachType.Display,317                    fontStyle = FontStyle.Italic,318                    fontSize = 12.5.sp,319                )320                // Body scrolls independently; only revealed at full height in321                // focus mode, but kept mounted so deck-mode shows a preview.322                Column(323                    Modifier.fillMaxWidth().weight(1f)324                        .padding(top = 14.dp)325                        .verticalScroll(rememberScrollState()),326                ) {327                    body()328                    Spacer(Modifier.height(24.dp))329                }330            }331        } else {332            // Tab: just the title, single line, vertically centred.333            Box(Modifier.fillMaxSize(), contentAlignment = Alignment.CenterStart) {334                Text(335                    spec.title,336                    color = spec.fg,337                    fontFamily = PeachType.Display,338                    fontStyle = FontStyle.Italic,339                    fontWeight = FontWeight.Normal,340                    fontSize = 19.sp,341                    maxLines = 1,342                    overflow = TextOverflow.Ellipsis,343                )344            }345        }346    }347}348349/**350 * Ambient drifting sparkle dust for the Pro card. Three offset dot grids slowly351 * translate diagonally and loop, screen-blended at low opacity — the Compose352 * analogue of the browser's `stk-pro-dust` keyframes.353 */354@Composable355private fun SparkleDust(modifier: Modifier = Modifier) {356    val reduceMotion = LocalReduceMotion.current357    val t = rememberInfiniteTransition(label = "dust")358    val drift by if (reduceMotion) {359        remember { mutableStateOf(0f) }360    } else {361        t.animateFloat(362            initialValue = 0f,363            targetValue = 1f,364            animationSpec = infiniteRepeatable(tween(14000, easing = androidx.compose.animation.core.LinearEasing)),365            label = "drift",366        )367    }368    Box(369        modifier.drawWithCache {370            val tile = 48.dp.toPx()371            val dx = drift * tile372            onDrawBehind {373                val cols = (size.width / tile).toInt() + 2374                val rows = (size.height / tile).toInt() + 2375                for (r in 0..rows) {376                    for (c in 0..cols) {377                        val baseX = c * tile + dx378                        val baseY = r * tile + dx379                        drawCircle(Color(0xFFFFF8DC), 0.6f, Offset(baseX % (size.width + tile), baseY % (size.height + tile)), alpha = 0.11f, blendMode = BlendMode.Screen)380                        drawCircle(Color(0xFFFFEEAA), 1f, Offset((baseX + 19f) % (size.width + tile), (baseY + 15f) % (size.height + tile)), alpha = 0.08f, blendMode = BlendMode.Screen)381                    }382                }383            }384        },385    )386}