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.LocalPeachAccent61import com.peachpasswords.ui.theme.LocalReduceMotion62import com.peachpasswords.ui.theme.peachColors63import com.peachpasswords.ui.theme.PeachMotion64import com.peachpasswords.ui.theme.PeachTheme65import com.peachpasswords.ui.theme.PeachStackColors66import com.peachpasswords.ui.theme.PeachType67import com.peachpasswords.ui.theme.ShellBackground68import kotlinx.coroutines.delay69import kotlinx.coroutines.launch7071727374private val EXPANDED_H = 258.dp75private val TAB_H = 46.dp76private val TAB_OVERLAP = 14.dp77private val TAB_CARD_H = TAB_H + 4.dp7879@Composable80fun SettingsScreen(81 onBack: () -> Unit,82 onNavigate: (com.peachpasswords.ui.PeachNav) -> Unit = {},83 vm: SettingsViewModel = hiltViewModel(),84) {85 val entries by vm.entries.collectAsState()86 val settings by vm.settings.collectAsState()87 val isPro by vm.isPro.collectAsState()88 val showCelebration by vm.showCelebration.collectAsState()89 val reduceMotion = LocalReduceMotion.current90 val accent = LocalPeachAccent.current91 val scope = rememberCoroutineScope()9293 var active by remember { mutableStateOf(CardId.Pro) }94 var deckOpen by remember { mutableStateOf(true) }95 var fanning by remember { mutableStateOf(false) }96 var celebrating by remember { mutableStateOf<CardId?>(null) }9798 val activeIndex = STACK_CARDS.indexOfFirst { it.id == active }.coerceAtLeast(0)99100 fun handleBack() {101 if (!deckOpen) deckOpen = true else onBack()102 }103 BackHandler { handleBack() }104105 106 fun celebrate() {107 if (fanning) return108 scope.launch {109 fanning = true110 active = CardId.Pro111 CELEBRATION_FAN.forEachIndexed { i, id ->112 delay(150L)113 active = id114 }115 delay(150L)116 active = CardId.Pro117 fanning = false118 celebrating = CardId.Pro119 delay(900L)120 celebrating = null121 }122 }123124 125 126 127 128 PeachTheme(darkTheme = true, accent = accent, reduceMotion = reduceMotion) {129 ShellBackground {130 Column(Modifier.fillMaxSize().statusBarsPadding().padding(horizontal = 12.dp)) {131 132 Row(133 Modifier.fillMaxWidth().padding(top = 10.dp, bottom = 8.dp),134 verticalAlignment = Alignment.CenterVertically,135 ) {136 Box(137 Modifier.size(28.dp).clip(RoundedCornerShape(999.dp))138 .background(Color(0x04F0E8D8))139 .clickable { handleBack() },140 contentAlignment = Alignment.Center,141 ) {142 Text("‹", color = peachColors.T1, fontFamily = PeachType.Mono, fontSize = 16.sp)143 }144 Text(145 "Settings",146 modifier = Modifier.weight(1f).padding(start = 10.dp),147 color = peachColors.T0,148 fontFamily = PeachType.Display,149 fontStyle = FontStyle.Italic,150 fontSize = 22.sp,151 )152 Text(153 "${activeIndex + 1} / ${STACK_CARDS.size}",154 color = peachColors.T2,155 fontFamily = PeachType.Mono,156 fontSize = 9.sp,157 letterSpacing = 0.22.em,158 )159 }160161 BoxWithConstraints(Modifier.fillMaxSize()) {162 val focusHeight = maxHeight - 8.dp163 STACK_CARDS.forEachIndexed { index, card ->164 val isActive = card.id == active165 val distance = when {166 index == activeIndex -> -1167 index > activeIndex -> index - activeIndex - 1168 else -> STACK_CARDS.size - activeIndex + index - 1169 }170 val targetTop = when {171 isActive -> 0.dp172 !deckOpen -> focusHeight + 44.dp + (distance * 8).dp173 else -> EXPANDED_H - TAB_OVERLAP + (distance * (TAB_H - TAB_OVERLAP).value.toInt()).dp174 }175 val targetHeight = when {176 isActive && deckOpen -> EXPANDED_H177 isActive -> focusHeight178 else -> TAB_CARD_H179 }180 val concealed = !deckOpen && !isActive181182 val animSpec = if (fanning) {183 tween<Float>(140, easing = PeachMotion.Celebrate)184 } else {185 tween<Float>(PeachMotion.Stack, easing = PeachMotion.StackSpring)186 }187 val top by animateDpAsState(188 targetTop,189 animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)190 else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),191 label = "top",192 )193 val cardH by animateDpAsState(194 targetHeight,195 animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate)196 else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring),197 label = "h",198 )199 val alpha by animateFloatAsState(if (concealed) 0f else 1f, animSpec, label = "a")200 val zIndex = if (isActive) 99f else (50 - distance).toFloat()201202 203 val shakeDy = remember { Animatable(0f) }204 val shakeRot = remember { Animatable(0f) }205 LaunchedEffect(celebrating == card.id) {206 if (celebrating == card.id && !reduceMotion) {207 launch {208 shakeRot.animateTo(0f, keyframes {209 durationMillis = 880210 0f at 0; -1.3f at 211; 0.9f at 422; -0.45f at 616; 0f at 757211 })212 }213 shakeDy.animateTo(0f, keyframes {214 durationMillis = 880215 0f at 0; -10f at 211; 5f at 422; -4f at 616; 2f at 757216 })217 }218 }219220 val density = LocalDensity.current221 StackCard(222 spec = card,223 active = isActive,224 focused = isActive && !deckOpen,225 lede = vm.lede(card.id, entries.count { it.deletedAt == null }, settings),226 showSparkle = card.id == CardId.Pro && isPro,227 onIconClick = if (card.id == CardId.Pro && isPro) ({ celebrate() }) else null,228 body = {229 CardBody(230 id = card.id,231 fg = card.fg,232 vm = vm,233 settings = settings,234 entriesCount = entries.count { it.deletedAt == null },235 onNavigate = onNavigate,236 )237 },238 modifier = Modifier239 .fillMaxWidth()240 .align(Alignment.TopStart)241 .zIndex(zIndex)242 .graphicsLayer {243 translationY = top.toPx() + with(density) { shakeDy.value.dp.toPx() }244 rotationZ = shakeRot.value245 this.alpha = alpha246 }247 .height(cardH),248 onClick = {249 if (isActive) {250 if (deckOpen) deckOpen = false251 } else {252 active = card.id253 deckOpen = false254 }255 },256 enabled = !concealed,257 )258 }259 }260 }261 if (showCelebration) {262 ProCelebration(onDone = { vm.dismissCelebration(); celebrate() })263 }264 }265 }266}267268@Composable269private fun StackCard(270 spec: StackCardSpec,271 active: Boolean,272 focused: Boolean,273 lede: String,274 showSparkle: Boolean,275 onIconClick: (() -> Unit)?,276 body: @Composable () -> Unit,277 modifier: Modifier,278 onClick: () -> Unit,279 enabled: Boolean,280) {281 val interaction = remember { MutableInteractionSource() }282 Box(283 modifier284 .shadow(if (active) 18.dp else 8.dp, RoundedCornerShape(PeachStackColors.CardRadius), clip = false)285 .clip(RoundedCornerShape(PeachStackColors.CardRadius))286 .drawBehind { drawRect(brush = cardGradient155(spec.bgStart, spec.bgEnd)) }287 .clickable(288 interactionSource = interaction,289 indication = null,290 enabled = enabled && !active || (active && !focused),291 ) { onClick() }292 .padding(293 horizontal = if (active) 18.dp else 16.dp,294 vertical = if (active) 16.dp else 12.dp,295 ),296 ) {297 if (showSparkle) SparkleDust(Modifier.fillMaxSize())298 if (active) {299 Column(Modifier.fillMaxSize()) {300 Row(301 Modifier.fillMaxWidth(),302 horizontalArrangement = Arrangement.SpaceBetween,303 verticalAlignment = Alignment.CenterVertically,304 ) {305 Text(306 spec.num,307 color = spec.fg.copy(alpha = 0.7f),308 fontFamily = PeachType.Mono,309 fontSize = 8.5.sp,310 letterSpacing = 0.26.em,311 )312 Box(313 Modifier.clip(RoundedCornerShape(999.dp))314 .then(if (onIconClick != null) Modifier.clickable { onIconClick() } else Modifier)315 .padding(2.dp),316 ) {317 CardIcon(spec.icon, spec.fg.copy(alpha = 0.85f))318 }319 }320 Text(321 spec.title,322 modifier = Modifier.padding(top = 6.dp),323 color = spec.fg,324 fontFamily = PeachType.Display,325 fontStyle = FontStyle.Italic,326 fontSize = 26.sp,327 )328 Text(329 lede,330 modifier = Modifier.padding(top = 4.dp),331 color = spec.fg.copy(alpha = 0.72f),332 fontFamily = PeachType.Display,333 fontStyle = FontStyle.Italic,334 fontSize = 12.5.sp,335 )336 337 338 Column(339 Modifier.fillMaxWidth().weight(1f)340 .padding(top = 14.dp)341 .verticalScroll(rememberScrollState()),342 ) {343 body()344 Spacer(Modifier.height(24.dp))345 }346 }347 } else {348 349 Box(Modifier.fillMaxSize(), contentAlignment = Alignment.CenterStart) {350 Text(351 spec.title,352 color = spec.fg,353 fontFamily = PeachType.Display,354 fontStyle = FontStyle.Italic,355 fontWeight = FontWeight.Normal,356 fontSize = 19.sp,357 maxLines = 1,358 overflow = TextOverflow.Ellipsis,359 )360 }361 }362 }363}364365366367368369370private fun DrawScope.cardGradient155(start: Color, end: Color): Brush {371 val a = Math.toRadians(155.0)372 val dx = kotlin.math.sin(a).toFloat()373 val dy = -kotlin.math.cos(a).toFloat()374 val cx = size.width / 2f375 val cy = size.height / 2f376 val half = (kotlin.math.abs(dx) * size.width + kotlin.math.abs(dy) * size.height) / 2f377 return Brush.linearGradient(378 colors = listOf(start, end),379 start = Offset(cx - dx * half, cy - dy * half),380 end = Offset(cx + dx * half, cy + dy * half),381 )382}383384385386387388389@Composable390private fun SparkleDust(modifier: Modifier = Modifier) {391 val reduceMotion = LocalReduceMotion.current392 val t = rememberInfiniteTransition(label = "dust")393 val drift by if (reduceMotion) {394 remember { mutableStateOf(0f) }395 } else {396 t.animateFloat(397 initialValue = 0f,398 targetValue = 1f,399 animationSpec = infiniteRepeatable(tween(14000, easing = androidx.compose.animation.core.LinearEasing)),400 label = "drift",401 )402 }403 Box(404 modifier.drawWithCache {405 val tile = 48.dp.toPx()406 val dx = drift * tile407 onDrawBehind {408 val cols = (size.width / tile).toInt() + 2409 val rows = (size.height / tile).toInt() + 2410 for (r in 0..rows) {411 for (c in 0..cols) {412 val baseX = c * tile + dx413 val baseY = r * tile + dx414 drawCircle(Color(0xFFFFF8DC), 0.6f, Offset(baseX % (size.width + tile), baseY % (size.height + tile)), alpha = 0.11f, blendMode = BlendMode.Screen)415 drawCircle(Color(0xFFFFEEAA), 1f, Offset((baseX + 19f) % (size.width + tile), (baseY + 15f) % (size.height + tile)), alpha = 0.08f, blendMode = BlendMode.Screen)416 }417 }418 }419 },420 )421}