b6a502bb7352

feat: pull-to-refresh on the vault list — custom nestedScroll spinner triggers SyncViewModel.syncNow(); dependency-free, accent-styled (v2.3.1)

time
2026-06-18 17:48 UTC · 5 months ago
machine
koh2
agent
claude-opus-4-8
session
android sync ux parity
parent
af8b5b404e22
1 file app/src/main/java/com/peachpasswords/ui/vault/VaultListScreen.kt+149 −25
app/src/main/java/com/peachpasswords/ui/vault/VaultListScreen.kt+149 −25
@@ -1,6 +1,13 @@11 package com.peachpasswords.ui.vault22 33 import androidx.compose.animation.animateColorAsState4+import androidx.compose.animation.core.LinearEasing5+import androidx.compose.animation.core.animate6+import androidx.compose.animation.core.animateFloat7+import androidx.compose.animation.core.infiniteRepeatable8+import androidx.compose.animation.core.rememberInfiniteTransition9+import androidx.compose.animation.core.tween10+import androidx.compose.foundation.Canvas411 import androidx.compose.foundation.background512 import androidx.compose.foundation.border613 import androidx.compose.foundation.clickable@@ -55,16 +62,27 @@5562 import androidx.compose.ui.geometry.Size5663 import androidx.compose.ui.graphics.Color5764 import androidx.compose.ui.graphics.SolidColor65+import androidx.compose.ui.graphics.StrokeCap66+import androidx.compose.ui.graphics.drawscope.Stroke67+import androidx.compose.ui.graphics.drawscope.rotate68+import androidx.compose.ui.graphics.graphicsLayer69+import androidx.compose.ui.input.nestedscroll.NestedScrollConnection70+import androidx.compose.ui.input.nestedscroll.NestedScrollSource71+import androidx.compose.ui.input.nestedscroll.nestedScroll5872 import androidx.compose.ui.input.pointer.pointerInput5973 import androidx.compose.ui.input.pointer.positionChange6074 import androidx.compose.ui.platform.LocalContext75+import androidx.compose.ui.platform.LocalDensity6176 import androidx.compose.ui.platform.LocalView6277 import androidx.compose.ui.text.TextStyle78+import androidx.compose.ui.unit.Velocity6379 import androidx.compose.ui.unit.dp6480 import androidx.compose.ui.unit.sp6581 import androidx.hilt.navigation.compose.hiltViewModel6682 import androidx.compose.runtime.collectAsState6783 import com.peachpasswords.haptics.PeachHaptics84+import com.peachpasswords.sync.SyncStatus85+import com.peachpasswords.ui.sync.SyncViewModel6886 import com.peachpasswords.ui.PeachNav6987 import com.peachpasswords.ui.components.FaviconInitials7088 import com.peachpasswords.ui.theme.LocalPeachAccent@@ -97,6 +115,9 @@97115     val snackbarHostState = remember { SnackbarHostState() }98116     var menuEntry by remember { mutableStateOf<VaultEntry?>(null) }99117     val view = LocalView.current118+    // Pull-to-refresh drives a manual sync round against paired devices.119+    val syncVm: SyncViewModel = hiltViewModel()120+    val syncStatus by syncVm.status.collectAsState()100121 101122     LaunchedEffect(undoEvent) {102123         val ev = undoEvent ?: return@LaunchedEffect@@ -145,32 +166,38 @@145166                 onHealth = { onNavigate(PeachNav.Health) },146167                 onTrash = { onNavigate(PeachNav.Trash) },147168             )148-            LazyColumn(Modifier.fillMaxSize().weight(1f)) {149-                if (entries.isEmpty()) {150-                    item { EmptyState(onSeed = vm::seedDemo) }169+            PeachPullToRefresh(170+                refreshing = syncStatus is SyncStatus.Syncing,171+                onRefresh = { syncVm.syncNow() },172+                modifier = Modifier.fillMaxWidth().weight(1f),173+            ) {174+                LazyColumn(Modifier.fillMaxSize()) {175+                    if (entries.isEmpty()) {176+                        item { EmptyState(onSeed = vm::seedDemo) }177+                    }178+                    items(entries, key = { it.id }) { entry ->179+                        EntryRow(180+                            entry = entry,181+                            flashing = flashId == entry.id,182+                            canSwipeCopy = vm.canSwipeCopy(entry) && !isMultiSelect,183+                            multiSelect = isMultiSelect,184+                            selected = entry.id in selectedIds,185+                            onTap = {186+                                if (isMultiSelect) vm.toggleSelected(entry.id)187+                                else onNavigate(entryTapDestination(entry))188+                            },189+                            onLongPress = {190+                                if (isMultiSelect) vm.toggleSelected(entry.id)191+                                else menuEntry = entry192+                            },193+                            onSwipeCopy = { vm.copyPassword(entry) },194+                        )195+                    }196+                    item {197+                        NewEntryRow(onClick = { onNavigate(PeachNav.EntryEdit(null)) })198+                        Spacer(Modifier.navigationBarsPadding())199+                    }151200                 }152-                items(entries, key = { it.id }) { entry ->153-                    EntryRow(154-                        entry = entry,155-                        flashing = flashId == entry.id,156-                        canSwipeCopy = vm.canSwipeCopy(entry) && !isMultiSelect,157-                        multiSelect = isMultiSelect,158-                        selected = entry.id in selectedIds,159-                        onTap = {160-                            if (isMultiSelect) vm.toggleSelected(entry.id)161-                            else onNavigate(entryTapDestination(entry))162-                        },163-                        onLongPress = {164-                            if (isMultiSelect) vm.toggleSelected(entry.id)165-                            else menuEntry = entry166-                        },167-                        onSwipeCopy = { vm.copyPassword(entry) },168-                    )169-                }170-                item {171-                    NewEntryRow(onClick = { onNavigate(PeachNav.EntryEdit(null)) })172-                    Spacer(Modifier.navigationBarsPadding())173-                }174201             }175202         }176203         SnackbarHost(@@ -458,6 +485,103 @@458485     ) {459486         Icon(Icons.Rounded.Add, contentDescription = null, tint = accent, modifier = Modifier.size(16.dp))460487         Text("new entry", color = accent, style = PeachType.labelMono)488+    }489+}490+491+/**492+ * Lightweight pull-to-refresh: overscrolling the top of [content] drags an accent493+ * spinner down; releasing past the trigger fires [onRefresh]. Self-contained (no494+ * Material pull-refresh dependency) so it matches Peach's bespoke styling and adds495+ * no new libraries. The content stays put — only the spinner travels, the common496+ * "spinner descends from under the header" pattern.497+ */498+@Composable499+private fun PeachPullToRefresh(500+    refreshing: Boolean,501+    onRefresh: () -> Unit,502+    modifier: Modifier = Modifier,503+    content: @Composable () -> Unit,504+) {505+    val density = LocalDensity.current506+    val triggerPx = with(density) { 64.dp.toPx() }507+    val maxPx = with(density) { 96.dp.toPx() }508+    var pull by remember { mutableStateOf(0f) }509+510+    // When a refresh round finishes, ease the spinner back to rest.511+    LaunchedEffect(refreshing) {512+        if (!refreshing && pull > 0f) animate(pull, 0f) { v, _ -> pull = v }513+    }514+515+    val connection = remember(refreshing) {516+        object : NestedScrollConnection {517+            override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {518+                // Scrolling back up retracts any pull we own before the list moves.519+                if (available.y < 0f && pull > 0f) {520+                    val used = (-available.y).coerceAtMost(pull)521+                    pull -= used522+                    return Offset(0f, -used)523+                }524+                return Offset.Zero525+            }526+527+            override fun onPostScroll(consumed: Offset, available: Offset, source: NestedScrollSource): Offset {528+                // Leftover downward drag at the top of the list (list consumed none).529+                if (!refreshing && source == NestedScrollSource.Drag && available.y > 0f) {530+                    pull = (pull + available.y * 0.5f).coerceAtMost(maxPx)531+                    return Offset(0f, available.y)532+                }533+                return Offset.Zero534+            }535+536+            override suspend fun onPreFling(available: Velocity): Velocity {537+                if (!refreshing && pull >= triggerPx) {538+                    onRefresh()539+                    animate(pull, triggerPx) { v, _ -> pull = v }540+                } else if (!refreshing) {541+                    animate(pull, 0f) { v, _ -> pull = v }542+                }543+                return Velocity.Zero544+            }545+        }546+    }547+548+    Box(modifier.nestedScroll(connection)) {549+        content()550+        PullSpinner(pull = pull, triggerPx = triggerPx, refreshing = refreshing, modifier = Modifier.align(Alignment.TopCenter))551+    }552+}553+554+@Composable555+private fun PullSpinner(pull: Float, triggerPx: Float, refreshing: Boolean, modifier: Modifier = Modifier) {556+    if (pull <= 0.5f && !refreshing) return557+    val accent = LocalPeachAccent.current558+    val progress = (pull / triggerPx).coerceIn(0f, 1f)559+    val rotation = if (refreshing) {560+        val spin = rememberInfiniteTransition(label = "pull-spin")561+        spin.animateFloat(562+            initialValue = 0f, targetValue = 360f,563+            animationSpec = infiniteRepeatable(tween(820, easing = LinearEasing)),564+            label = "pull-rot",565+        ).value566+    } else {567+        progress * 270f568+    }569+    val sweep = if (refreshing) 300f else 8f + progress * 292f570+    Box(571+        modifier.padding(top = 6.dp).graphicsLayer { translationY = pull * 0.5f },572+        contentAlignment = Alignment.Center,573+    ) {574+        Canvas(Modifier.size(22.dp)) {575+            rotate(rotation) {576+                drawArc(577+                    color = accent.copy(alpha = if (refreshing) 1f else progress),578+                    startAngle = 0f,579+                    sweepAngle = sweep,580+                    useCenter = false,581+                    style = Stroke(width = 2.dp.toPx(), cap = StrokeCap.Round),582+                )583+            }584+        }461585     }462586 }463587 

view file