Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
01c619e925 | ||
|
|
d3459e69c1 |
@@ -5,6 +5,10 @@ import android.content.ClipboardManager
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
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.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
@@ -12,6 +16,7 @@ import androidx.compose.foundation.combinedClickable
|
|||||||
import androidx.compose.foundation.gestures.awaitEachGesture
|
import androidx.compose.foundation.gestures.awaitEachGesture
|
||||||
import androidx.compose.foundation.gestures.awaitFirstDown
|
import androidx.compose.foundation.gestures.awaitFirstDown
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
|
import androidx.compose.foundation.layout.offset
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.items
|
import androidx.compose.foundation.lazy.items
|
||||||
import androidx.compose.foundation.shape.CircleShape
|
import androidx.compose.foundation.shape.CircleShape
|
||||||
@@ -52,8 +57,10 @@ import androidx.compose.ui.zIndex
|
|||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
|
import androidx.compose.ui.unit.IntOffset
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
|
import kotlin.math.roundToInt
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import cn.airnan.a2fair.core.NativeLib
|
import cn.airnan.a2fair.core.NativeLib
|
||||||
import cn.airnan.a2fair.R
|
import cn.airnan.a2fair.R
|
||||||
@@ -62,6 +69,7 @@ import cn.airnan.a2fair.data.entity.Token
|
|||||||
import cn.airnan.a2fair.data.entity.TokenType
|
import cn.airnan.a2fair.data.entity.TokenType
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class)
|
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
@@ -85,6 +93,8 @@ fun TokensScreen(
|
|||||||
val cardHeights = remember { mutableMapOf<String, Int>() }
|
val cardHeights = remember { mutableMapOf<String, Int>() }
|
||||||
var renameTarget by remember { mutableStateOf<Group?>(null) }
|
var renameTarget by remember { mutableStateOf<Group?>(null) }
|
||||||
var deleteTarget 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) {
|
LaunchedEffect(isSearchExpanded) {
|
||||||
if (isSearchExpanded) {
|
if (isSearchExpanded) {
|
||||||
@@ -182,7 +192,8 @@ fun TokensScreen(
|
|||||||
// Token List
|
// Token List
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = Modifier.fillMaxSize(),
|
||||||
contentPadding = PaddingValues(bottom = 80.dp)
|
contentPadding = PaddingValues(bottom = 80.dp),
|
||||||
|
userScrollEnabled = draggedGroupKey == null && draggedTokenId == null
|
||||||
) {
|
) {
|
||||||
uiState.groupedTokens.forEach { (group, tokens) ->
|
uiState.groupedTokens.forEach { (group, tokens) ->
|
||||||
val groupId = group?.id
|
val groupId = group?.id
|
||||||
@@ -191,12 +202,17 @@ fun TokensScreen(
|
|||||||
|
|
||||||
item(key = "group_${groupId ?: "unassigned"}") {
|
item(key = "group_${groupId ?: "unassigned"}") {
|
||||||
Card(
|
Card(
|
||||||
modifier = Modifier
|
modifier = (if (draggedGroupKey == groupKey) Modifier else Modifier.animateItem())
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(horizontal = 16.dp, vertical = 6.dp)
|
.padding(horizontal = 16.dp, vertical = 6.dp)
|
||||||
.zIndex(if (draggedGroupKey == groupKey) 1f else 0f)
|
.zIndex(if (draggedGroupKey == groupKey) 1f else 0f)
|
||||||
.graphicsLayer {
|
.graphicsLayer {
|
||||||
if (draggedGroupKey == groupKey) translationY = dragGroupTranslateY
|
if (draggedGroupKey == groupKey) {
|
||||||
|
translationY = dragGroupTranslateY
|
||||||
|
shadowElevation = 16f
|
||||||
|
scaleX = 1.01f
|
||||||
|
scaleY = 1.01f
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.onSizeChanged { cardHeights["g:$groupKey"] = it.height },
|
.onSizeChanged { cardHeights["g:$groupKey"] = it.height },
|
||||||
elevation = CardDefaults.cardElevation(defaultElevation = 0.dp)
|
elevation = CardDefaults.cardElevation(defaultElevation = 0.dp)
|
||||||
@@ -255,62 +271,83 @@ fun TokensScreen(
|
|||||||
|
|
||||||
if (!isCollapsed) {
|
if (!isCollapsed) {
|
||||||
tokens.forEachIndexed { index, token ->
|
tokens.forEachIndexed { index, token ->
|
||||||
Box(
|
val offsetAnim = remember(token.id) {
|
||||||
modifier = Modifier
|
tokenOffsetAnims.getOrPut(token.id) { Animatable(0f) }
|
||||||
.zIndex(if (draggedTokenId == token.id) 1f else 0f)
|
}
|
||||||
.graphicsLayer {
|
key(token.id) {
|
||||||
if (draggedTokenId == token.id) translationY = dragTokenTranslateY
|
Box(
|
||||||
}
|
modifier = Modifier
|
||||||
.onSizeChanged { cardHeights["t:${token.id}"] = it.height }
|
.offset { IntOffset(0, offsetAnim.value.roundToInt()) }
|
||||||
) {
|
.zIndex(if (draggedTokenId == token.id) 1f else 0f)
|
||||||
TokenCard(
|
.graphicsLayer {
|
||||||
token = token,
|
if (draggedTokenId == token.id) {
|
||||||
onIncrement = { viewModel.incrementCounter(token) },
|
translationY = dragTokenTranslateY
|
||||||
onEdit = { onNavigateToTokenEdit(token.id) },
|
shadowElevation = 12f
|
||||||
dragModifier = Modifier.dragHandle(
|
}
|
||||||
key = token.id,
|
}
|
||||||
onDragStart = {
|
.onSizeChanged { cardHeights["t:${token.id}"] = it.height }
|
||||||
draggedTokenId = token.id
|
) {
|
||||||
dragTokenTranslateY = 0f
|
TokenCard(
|
||||||
},
|
token = token,
|
||||||
onDragDelta = { dy ->
|
onIncrement = { viewModel.incrementCounter(token) },
|
||||||
dragTokenTranslateY += dy
|
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
|
// Reorder within the same group only
|
||||||
val groupTokens = latestUiState.groupedTokens[group]
|
val groupTokens = latestUiState.groupedTokens[group]
|
||||||
if (groupTokens != null) {
|
if (groupTokens != null) {
|
||||||
val idx = groupTokens.indexOfFirst { it.id == token.id }
|
val idx = groupTokens.indexOfFirst { it.id == token.id }
|
||||||
if (idx >= 0) {
|
if (idx >= 0) {
|
||||||
if (idx < groupTokens.lastIndex && dragTokenTranslateY > 0f) {
|
if (idx < groupTokens.lastIndex && dragTokenTranslateY > 0f) {
|
||||||
val next = groupTokens[idx + 1]
|
val next = groupTokens[idx + 1]
|
||||||
val nextHeight = cardHeights["t:${next.id}"] ?: 0
|
val nextHeight = cardHeights["t:${next.id}"] ?: 0
|
||||||
if (nextHeight > 0 && dragTokenTranslateY >= nextHeight) {
|
if (nextHeight > 0 && dragTokenTranslateY >= nextHeight) {
|
||||||
dragTokenTranslateY -= nextHeight
|
dragTokenTranslateY -= nextHeight
|
||||||
val newOrder = groupTokens.toMutableList()
|
val newOrder = groupTokens.toMutableList()
|
||||||
newOrder[idx] = next
|
newOrder[idx] = next
|
||||||
newOrder[idx + 1] = token
|
newOrder[idx + 1] = token
|
||||||
viewModel.applyTokenOrder(newOrder)
|
tokenOffsetAnims[next.id]?.let { anim ->
|
||||||
}
|
scope.launch {
|
||||||
} else if (idx > 0 && dragTokenTranslateY < 0f) {
|
anim.snapTo(nextHeight.toFloat())
|
||||||
val prev = groupTokens[idx - 1]
|
anim.animateTo(0f, spring(dampingRatio = Spring.DampingRatioNoBouncy, stiffness = Spring.StiffnessMediumLow))
|
||||||
val prevHeight = cardHeights["t:${prev.id}"] ?: 0
|
}
|
||||||
if (prevHeight > 0 && -dragTokenTranslateY >= prevHeight) {
|
}
|
||||||
dragTokenTranslateY += prevHeight
|
viewModel.applyTokenOrder(newOrder)
|
||||||
val newOrder = groupTokens.toMutableList()
|
}
|
||||||
newOrder[idx] = prev
|
} else if (idx > 0 && dragTokenTranslateY < 0f) {
|
||||||
newOrder[idx - 1] = token
|
val prev = groupTokens[idx - 1]
|
||||||
viewModel.applyTokenOrder(newOrder)
|
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
|
||||||
}
|
}
|
||||||
},
|
)
|
||||||
onDragEnd = {
|
|
||||||
draggedTokenId = null
|
|
||||||
dragTokenTranslateY = 0f
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
if (index < tokens.lastIndex) {
|
if (index < tokens.lastIndex) {
|
||||||
HorizontalDivider(modifier = Modifier.padding(horizontal = 16.dp))
|
HorizontalDivider(modifier = Modifier.padding(horizontal = 16.dp))
|
||||||
|
|||||||
Reference in New Issue
Block a user