拖拽排序重写为 sh.calvin.reorderable:组/令牌两级手势与动画由库接管,拖拽中本地排序松手持久化,未分组组头补上拖拽把手;令牌行与分组头点击/长按高亮覆盖整行,令牌行去分隔线,分组名黑色粗体,倒计时环用 CircularWavyProgressIndicator 官方默认形态
This commit is contained in:
@@ -67,6 +67,7 @@ android {
|
||||
|
||||
dependencies {
|
||||
implementation(platform(libs.androidx.compose.bom))
|
||||
implementation(libs.reorderable)
|
||||
implementation(libs.androidx.activity.compose)
|
||||
implementation(libs.androidx.compose.material3)
|
||||
implementation("androidx.compose.material:material-icons-extended")
|
||||
|
||||
@@ -5,20 +5,16 @@ import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.widget.Toast
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.core.Animatable
|
||||
import androidx.compose.animation.core.AnimationVector1D
|
||||
import androidx.compose.animation.core.Spring
|
||||
import androidx.compose.animation.core.spring
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.LocalIndication
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.combinedClickable
|
||||
import androidx.compose.foundation.gestures.awaitEachGesture
|
||||
import androidx.compose.foundation.gestures.awaitFirstDown
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.layout.offset
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
@@ -37,30 +33,24 @@ import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.draw.shadow
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.Shape
|
||||
import androidx.compose.ui.graphics.drawscope.Stroke
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.input.pointer.positionChange
|
||||
import androidx.compose.ui.input.pointer.changedToUp
|
||||
import androidx.compose.ui.layout.onSizeChanged
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.zIndex
|
||||
import androidx.compose.ui.platform.LocalHapticFeedback
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import kotlin.math.roundToInt
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import cn.airnan.a2fair.core.NativeLib
|
||||
import cn.airnan.a2fair.R
|
||||
@@ -69,7 +59,9 @@ import cn.airnan.a2fair.data.entity.Token
|
||||
import cn.airnan.a2fair.data.entity.TokenType
|
||||
import kotlin.math.abs
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import sh.calvin.reorderable.ReorderableColumn
|
||||
import sh.calvin.reorderable.ReorderableItem
|
||||
import sh.calvin.reorderable.rememberReorderableLazyListState
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class)
|
||||
@Composable
|
||||
@@ -85,16 +77,12 @@ fun TokensScreen(
|
||||
val focusManager = LocalFocusManager.current
|
||||
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
|
||||
|
||||
// Group & token drag-reorder state
|
||||
var draggedGroupKey by remember { mutableStateOf<Long?>(null) } // real id; -1 = Uncategorized
|
||||
var dragGroupTranslateY by remember { mutableFloatStateOf(0f) }
|
||||
var draggedTokenId by remember { mutableStateOf<Long?>(null) }
|
||||
var dragTokenTranslateY by remember { mutableFloatStateOf(0f) }
|
||||
val cardHeights = remember { mutableMapOf<String, Int>() }
|
||||
// Group drag-reorder state: pending local order while dragging, persisted on release
|
||||
val hapticFeedback = LocalHapticFeedback.current
|
||||
var pendingGroupOrder by remember { mutableStateOf<List<Group?>?>(null) }
|
||||
val lazyListState = rememberLazyListState()
|
||||
var renameTarget by remember { mutableStateOf<Group?>(null) }
|
||||
var deleteTarget by remember { mutableStateOf<Group?>(null) }
|
||||
val scope = rememberCoroutineScope()
|
||||
val tokenOffsetAnims = remember { mutableMapOf<Long, Animatable<Float, AnimationVector1D>>() }
|
||||
|
||||
LaunchedEffect(isSearchExpanded) {
|
||||
if (isSearchExpanded) {
|
||||
@@ -190,32 +178,41 @@ fun TokensScreen(
|
||||
}
|
||||
} else {
|
||||
// Token List
|
||||
val displayGroups =
|
||||
(pendingGroupOrder ?: latestUiState.groupedTokens.keys.toList())
|
||||
.mapNotNull { key -> latestUiState.groupedTokens[key]?.let { key to it } }
|
||||
|
||||
val reorderableLazyListState =
|
||||
rememberReorderableLazyListState(lazyListState) { from, to ->
|
||||
val keys = pendingGroupOrder ?: latestUiState.groupedTokens.keys.toList()
|
||||
val fromIndex = keys.indexOfFirst { groupItemKey(it) == from.key }
|
||||
val toIndex = keys.indexOfFirst { groupItemKey(it) == to.key }
|
||||
if (fromIndex in keys.indices && toIndex in keys.indices) {
|
||||
pendingGroupOrder =
|
||||
keys.toMutableList().apply { add(toIndex, removeAt(fromIndex)) }
|
||||
hapticFeedback.performHapticFeedback(HapticFeedbackType.SegmentFrequentTick)
|
||||
}
|
||||
}
|
||||
|
||||
LazyColumn(
|
||||
state = lazyListState,
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(bottom = 80.dp),
|
||||
userScrollEnabled = draggedGroupKey == null && draggedTokenId == null
|
||||
) {
|
||||
uiState.groupedTokens.forEach { (group, tokens) ->
|
||||
items(displayGroups, key = { groupItemKey(it.first) }) { (group, tokens) ->
|
||||
val groupId = group?.id
|
||||
val groupKey = groupId ?: -1L
|
||||
val isCollapsed = uiState.collapsedGroups.contains(groupId ?: -1L)
|
||||
|
||||
item(key = "group_${groupId ?: "unassigned"}") {
|
||||
ReorderableItem(reorderableLazyListState, groupItemKey(group)) { groupDragging ->
|
||||
val groupDragScope = this
|
||||
val groupInteractionSource = remember { MutableInteractionSource() }
|
||||
Card(
|
||||
modifier = (if (draggedGroupKey == groupKey) Modifier else Modifier.animateItem())
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp, vertical = 6.dp)
|
||||
.zIndex(if (draggedGroupKey == groupKey) 1f else 0f)
|
||||
.graphicsLayer {
|
||||
if (draggedGroupKey == groupKey) {
|
||||
translationY = dragGroupTranslateY
|
||||
shadowElevation = 16f
|
||||
scaleX = 1.01f
|
||||
scaleY = 1.01f
|
||||
}
|
||||
}
|
||||
.onSizeChanged { cardHeights["g:$groupKey"] = it.height },
|
||||
elevation = CardDefaults.cardElevation(defaultElevation = 0.dp)
|
||||
.padding(horizontal = 16.dp, vertical = 6.dp),
|
||||
elevation = CardDefaults.cardElevation(
|
||||
defaultElevation = if (groupDragging) 6.dp else 0.dp
|
||||
)
|
||||
) {
|
||||
Column {
|
||||
GroupHeader(
|
||||
@@ -224,133 +221,49 @@ fun TokensScreen(
|
||||
onToggle = { viewModel.toggleGroupCollapse(groupId) },
|
||||
onRename = { group?.let { renameTarget = it } },
|
||||
onDelete = { group?.let { deleteTarget = it } },
|
||||
dragModifier = Modifier.dragHandle(
|
||||
key = groupKey,
|
||||
onDragStart = {
|
||||
draggedGroupKey = groupKey
|
||||
dragGroupTranslateY = 0f
|
||||
dragModifier = with(groupDragScope) {
|
||||
Modifier.draggableHandle(
|
||||
interactionSource = groupInteractionSource,
|
||||
onDragStarted = {
|
||||
hapticFeedback.performHapticFeedback(HapticFeedbackType.GestureThresholdActivate)
|
||||
},
|
||||
onDragDelta = { dy ->
|
||||
dragGroupTranslateY += dy
|
||||
|
||||
// Swap with neighbour once dragged past its height
|
||||
val keys = latestUiState.groupedTokens.keys.toList()
|
||||
val index = keys.indexOfFirst { (it?.id ?: -1L) == groupKey }
|
||||
if (index >= 0) {
|
||||
if (dragGroupTranslateY > 0f && index < keys.lastIndex) {
|
||||
val next = keys[index + 1]
|
||||
val nextKey = next?.id ?: -1L
|
||||
val nextHeight = cardHeights["g:$nextKey"] ?: 0
|
||||
if (nextHeight > 0 && dragGroupTranslateY >= nextHeight) {
|
||||
dragGroupTranslateY -= nextHeight
|
||||
val newKeys = keys.toMutableList()
|
||||
newKeys[index] = next
|
||||
newKeys[index + 1] = group
|
||||
viewModel.applyGroupOrder(newKeys)
|
||||
}
|
||||
} else if (dragGroupTranslateY < 0f && index > 0) {
|
||||
val prev = keys[index - 1]
|
||||
val prevKey = prev?.id ?: -1L
|
||||
val prevHeight = cardHeights["g:$prevKey"] ?: 0
|
||||
if (prevHeight > 0 && -dragGroupTranslateY >= prevHeight) {
|
||||
dragGroupTranslateY += prevHeight
|
||||
val newKeys = keys.toMutableList()
|
||||
newKeys[index] = prev
|
||||
newKeys[index - 1] = group
|
||||
viewModel.applyGroupOrder(newKeys)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
onDragEnd = {
|
||||
draggedGroupKey = null
|
||||
dragGroupTranslateY = 0f
|
||||
onDragStopped = {
|
||||
pendingGroupOrder?.let { viewModel.applyGroupOrder(it) }
|
||||
pendingGroupOrder = null
|
||||
}
|
||||
)
|
||||
},
|
||||
interactionSource = groupInteractionSource
|
||||
)
|
||||
|
||||
if (!isCollapsed) {
|
||||
tokens.forEachIndexed { index, token ->
|
||||
val offsetAnim = remember(token.id) {
|
||||
tokenOffsetAnims.getOrPut(token.id) { Animatable(0f) }
|
||||
}
|
||||
ReorderableColumn(
|
||||
list = tokens,
|
||||
onMove = {
|
||||
hapticFeedback.performHapticFeedback(HapticFeedbackType.SegmentFrequentTick)
|
||||
},
|
||||
onSettle = { from, to ->
|
||||
val newOrder = tokens.toMutableList().apply { add(to, removeAt(from)) }
|
||||
viewModel.applyTokenOrder(newOrder)
|
||||
},
|
||||
) { index, token, tokenDragging ->
|
||||
key(token.id) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.offset { IntOffset(0, offsetAnim.value.roundToInt()) }
|
||||
.zIndex(if (draggedTokenId == token.id) 1f else 0f)
|
||||
.graphicsLayer {
|
||||
if (draggedTokenId == token.id) {
|
||||
translationY = dragTokenTranslateY
|
||||
shadowElevation = 12f
|
||||
}
|
||||
}
|
||||
.onSizeChanged { cardHeights["t:${token.id}"] = it.height }
|
||||
) {
|
||||
ReorderableItem {
|
||||
val tokenDragScope = this
|
||||
val rowInteractionSource = remember { MutableInteractionSource() }
|
||||
TokenCard(
|
||||
token = token,
|
||||
onIncrement = { viewModel.incrementCounter(token) },
|
||||
onEdit = { onNavigateToTokenEdit(token.id) },
|
||||
dragModifier = Modifier.dragHandle(
|
||||
key = token.id,
|
||||
onDragStart = {
|
||||
draggedTokenId = token.id
|
||||
dragTokenTranslateY = 0f
|
||||
},
|
||||
onDragDelta = { dy ->
|
||||
dragTokenTranslateY += dy
|
||||
|
||||
// Reorder within the same group only
|
||||
val groupTokens = latestUiState.groupedTokens[group]
|
||||
if (groupTokens != null) {
|
||||
val idx = groupTokens.indexOfFirst { it.id == token.id }
|
||||
if (idx >= 0) {
|
||||
if (idx < groupTokens.lastIndex && dragTokenTranslateY > 0f) {
|
||||
val next = groupTokens[idx + 1]
|
||||
val nextHeight = cardHeights["t:${next.id}"] ?: 0
|
||||
if (nextHeight > 0 && dragTokenTranslateY >= nextHeight) {
|
||||
dragTokenTranslateY -= nextHeight
|
||||
val newOrder = groupTokens.toMutableList()
|
||||
newOrder[idx] = next
|
||||
newOrder[idx + 1] = token
|
||||
tokenOffsetAnims[next.id]?.let { anim ->
|
||||
scope.launch {
|
||||
anim.snapTo(nextHeight.toFloat())
|
||||
anim.animateTo(0f, spring(dampingRatio = Spring.DampingRatioNoBouncy, stiffness = Spring.StiffnessMediumLow))
|
||||
}
|
||||
}
|
||||
viewModel.applyTokenOrder(newOrder)
|
||||
}
|
||||
} else if (idx > 0 && dragTokenTranslateY < 0f) {
|
||||
val prev = groupTokens[idx - 1]
|
||||
val prevHeight = cardHeights["t:${prev.id}"] ?: 0
|
||||
if (prevHeight > 0 && -dragTokenTranslateY >= prevHeight) {
|
||||
dragTokenTranslateY += prevHeight
|
||||
val newOrder = groupTokens.toMutableList()
|
||||
newOrder[idx] = prev
|
||||
newOrder[idx - 1] = token
|
||||
tokenOffsetAnims[prev.id]?.let { anim ->
|
||||
scope.launch {
|
||||
anim.snapTo(-prevHeight.toFloat())
|
||||
anim.animateTo(0f, spring(dampingRatio = Spring.DampingRatioNoBouncy, stiffness = Spring.StiffnessMediumLow))
|
||||
}
|
||||
}
|
||||
viewModel.applyTokenOrder(newOrder)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
onDragEnd = {
|
||||
draggedTokenId = null
|
||||
dragTokenTranslateY = 0f
|
||||
}
|
||||
)
|
||||
isDragging = tokenDragging,
|
||||
interactionSource = rowInteractionSource,
|
||||
dragModifier = with(tokenDragScope) {
|
||||
Modifier.draggableHandle(
|
||||
interactionSource = rowInteractionSource
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
if (index < tokens.lastIndex) {
|
||||
HorizontalDivider(modifier = Modifier.padding(horizontal = 16.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -429,35 +342,6 @@ fun TokensScreen(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drag handle: consumes the down event immediately so ancestor click/long-press
|
||||
* (copy, detail drawer, collapse) never fires; drag deltas reported as cumulative dy.
|
||||
*/
|
||||
private fun Modifier.dragHandle(
|
||||
key: Any?,
|
||||
onDragStart: () -> Unit,
|
||||
onDragDelta: (Float) -> Unit,
|
||||
onDragEnd: () -> Unit
|
||||
): Modifier = pointerInput(key) {
|
||||
awaitEachGesture {
|
||||
val down = awaitFirstDown(requireUnconsumed = false)
|
||||
down.consume()
|
||||
onDragStart()
|
||||
try {
|
||||
while (true) {
|
||||
val event = awaitPointerEvent()
|
||||
val change = event.changes.firstOrNull() ?: break
|
||||
if (change.changedToUp()) break
|
||||
val dy = change.positionChange().y
|
||||
change.consume()
|
||||
onDragDelta(dy)
|
||||
}
|
||||
} finally {
|
||||
onDragEnd()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
|
||||
@Composable
|
||||
private fun EmptyState(
|
||||
@@ -520,17 +404,21 @@ fun GroupHeader(
|
||||
onToggle: () -> Unit,
|
||||
onRename: () -> Unit = {},
|
||||
onDelete: () -> Unit = {},
|
||||
dragModifier: Modifier = Modifier
|
||||
dragModifier: Modifier = Modifier,
|
||||
interactionSource: MutableInteractionSource? = null
|
||||
) {
|
||||
var menuExpanded by remember { mutableStateOf(false) }
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.combinedClickable(onClick = onToggle)
|
||||
.combinedClickable(
|
||||
interactionSource = interactionSource,
|
||||
indication = LocalIndication.current,
|
||||
onClick = onToggle
|
||||
)
|
||||
.padding(horizontal = 16.dp, vertical = 12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
if (group != null) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.DragIndicator,
|
||||
contentDescription = stringResource(R.string.drag_handle_desc),
|
||||
@@ -538,12 +426,9 @@ fun GroupHeader(
|
||||
modifier = dragModifier
|
||||
)
|
||||
Spacer(modifier = Modifier.width(10.dp))
|
||||
}
|
||||
Text(
|
||||
text = group?.name ?: stringResource(R.string.uncategorized),
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold),
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(4.dp))
|
||||
@@ -587,7 +472,9 @@ fun TokenCard(
|
||||
token: Token,
|
||||
onIncrement: () -> Unit,
|
||||
onEdit: () -> Unit,
|
||||
dragModifier: Modifier = Modifier
|
||||
dragModifier: Modifier = Modifier,
|
||||
isDragging: Boolean = false,
|
||||
interactionSource: MutableInteractionSource? = null
|
||||
) {
|
||||
val warningColor = Color(0xFFFF1744)
|
||||
val otpPlaceholder = stringResource(R.string.otp_placeholder)
|
||||
@@ -671,7 +558,15 @@ fun TokenCard(
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.then(
|
||||
if (isDragging) Modifier
|
||||
.shadow(elevation = 8.dp)
|
||||
.background(MaterialTheme.colorScheme.surface)
|
||||
else Modifier
|
||||
)
|
||||
.combinedClickable(
|
||||
interactionSource = interactionSource,
|
||||
indication = LocalIndication.current,
|
||||
onClick = {
|
||||
copyToClipboard(context, currentCode, token.serviceName)
|
||||
},
|
||||
@@ -739,14 +634,11 @@ fun TokenCard(
|
||||
modifier = Modifier.size(44.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
val ringStroke = Stroke(width = with(LocalDensity.current) { 3.dp.toPx() })
|
||||
CircularWavyProgressIndicator(
|
||||
progress = { progress },
|
||||
modifier = Modifier.size(44.dp),
|
||||
color = if (showNextPreview) warningColor else MaterialTheme.colorScheme.primary,
|
||||
trackColor = MaterialTheme.colorScheme.outlineVariant,
|
||||
stroke = ringStroke,
|
||||
trackStroke = ringStroke,
|
||||
)
|
||||
Text(
|
||||
text = "$remainingSeconds",
|
||||
@@ -819,3 +711,5 @@ private fun copyToClipboard(context: Context, code: String, label: String) {
|
||||
clipboard.setPrimaryClip(clip)
|
||||
Toast.makeText(context, context.getString(R.string.code_copied, label), Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
private fun groupItemKey(group: Group?): String = "group_${group?.id ?: "unassigned"}"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
[versions]
|
||||
agp = "9.2.1"
|
||||
reorderable = "3.1.0"
|
||||
coreKtx = "1.15.0"
|
||||
junit = "4.13.2"
|
||||
junitVersion = "1.3.0"
|
||||
@@ -36,6 +37,7 @@ androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "u
|
||||
androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
|
||||
androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
|
||||
androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3", version = "1.5.0-beta01" }
|
||||
reorderable = { module = "sh.calvin.reorderable:reorderable", version.ref = "reorderable" }
|
||||
androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigationCompose" }
|
||||
androidx-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
|
||||
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
|
||||
|
||||
Reference in New Issue
Block a user