From e0bd974f59c94e24b27be8b8bd099e61a05f6ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=81=E5=86=9C=E9=98=BF=E6=A5=A0?= Date: Wed, 14 Oct 2026 14:47:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E9=A1=B5=E6=8D=A2=20Flexible=20?= =?UTF-8?q?=E5=A4=A7=E6=A0=87=E9=A2=98=EF=BC=8C=E7=A9=BA=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E4=B8=8E=E4=BB=A4=E7=89=8C=E5=9B=BE=E6=A0=87=E9=87=87=E7=94=A8?= =?UTF-8?q?=20Expressive=20=E5=BD=A2=E7=8A=B6=E5=BA=95=E5=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../airnan/a2fair/ui/tokens/TokensScreen.kt | 126 ++++++++++-------- 1 file changed, 68 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/cn/airnan/a2fair/ui/tokens/TokensScreen.kt b/app/src/main/java/cn/airnan/a2fair/ui/tokens/TokensScreen.kt index 5f7a11a..a739ee5 100644 --- a/app/src/main/java/cn/airnan/a2fair/ui/tokens/TokensScreen.kt +++ b/app/src/main/java/cn/airnan/a2fair/ui/tokens/TokensScreen.kt @@ -31,7 +31,9 @@ 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.vector.ImageVector +import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource @@ -45,9 +47,10 @@ import cn.airnan.a2fair.R import cn.airnan.a2fair.data.entity.Group import cn.airnan.a2fair.data.entity.Token import cn.airnan.a2fair.data.entity.TokenType +import kotlin.math.abs import kotlinx.coroutines.delay -@OptIn(ExperimentalMaterial3Api::class) +@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class) @Composable fun TokensScreen( viewModel: TokensViewModel, @@ -59,6 +62,7 @@ fun TokensScreen( var searchFieldHasFocus by remember { mutableStateOf(false) } val searchFocusRequester = remember { FocusRequester() } val focusManager = LocalFocusManager.current + val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior() LaunchedEffect(isSearchExpanded) { if (isSearchExpanded) { @@ -67,68 +71,61 @@ fun TokensScreen( } Scaffold( + modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection), topBar = { - TopAppBar( - title = { - Row( - modifier = Modifier.fillMaxWidth(), - verticalAlignment = Alignment.CenterVertically - ) { - AnimatedVisibility(visible = !isSearchExpanded) { - Text(stringResource(R.string.tokens)) - } - AnimatedVisibility( - visible = isSearchExpanded, + if (isSearchExpanded) { + TopAppBar( + title = { + Surface( + shape = MaterialTheme.shapes.large, + color = MaterialTheme.colorScheme.surfaceContainerHighest, modifier = Modifier - .weight(1f) + .fillMaxWidth() .padding(end = 16.dp) ) { - Surface( - shape = MaterialTheme.shapes.large, - color = MaterialTheme.colorScheme.surfaceContainerHighest, - modifier = Modifier.fillMaxWidth() - ) { - TextField( - value = uiState.searchQuery, - onValueChange = { viewModel.updateSearchQuery(it) }, - modifier = Modifier - .fillMaxWidth() - .focusRequester(searchFocusRequester) - .onFocusChanged { focusState -> - if (searchFieldHasFocus && !focusState.isFocused) { - isSearchExpanded = false - } - searchFieldHasFocus = focusState.isFocused - }, - placeholder = { Text(stringResource(R.string.search_tokens)) }, - leadingIcon = { Icon(Icons.Default.Search, contentDescription = stringResource(R.string.search)) }, - trailingIcon = { - IconButton(onClick = { - viewModel.updateSearchQuery("") - focusManager.clearFocus(force = true) + TextField( + value = uiState.searchQuery, + onValueChange = { viewModel.updateSearchQuery(it) }, + modifier = Modifier + .fillMaxWidth() + .focusRequester(searchFocusRequester) + .onFocusChanged { focusState -> + if (searchFieldHasFocus && !focusState.isFocused) { isSearchExpanded = false - }) { - Icon(Icons.Default.Close, contentDescription = "Clear search") } + searchFieldHasFocus = focusState.isFocused }, - singleLine = true, - colors = TextFieldDefaults.colors( - unfocusedIndicatorColor = Color.Transparent, - focusedIndicatorColor = Color.Transparent - ) + placeholder = { Text(stringResource(R.string.search_tokens)) }, + leadingIcon = { Icon(Icons.Default.Search, contentDescription = stringResource(R.string.search)) }, + trailingIcon = { + IconButton(onClick = { + viewModel.updateSearchQuery("") + focusManager.clearFocus(force = true) + isSearchExpanded = false + }) { + Icon(Icons.Default.Close, contentDescription = "Clear search") + } + }, + singleLine = true, + colors = TextFieldDefaults.colors( + unfocusedIndicatorColor = Color.Transparent, + focusedIndicatorColor = Color.Transparent ) - } + ) } } - }, - actions = { - AnimatedVisibility(visible = !isSearchExpanded) { + ) + } else { + MediumFlexibleTopAppBar( + title = { Text(stringResource(R.string.tokens)) }, + actions = { IconButton(onClick = { isSearchExpanded = true }) { Icon(Icons.Default.Search, contentDescription = stringResource(R.string.search)) } - } - } - ) + }, + scrollBehavior = scrollBehavior + ) + } }, floatingActionButton = { FloatingActionButton(onClick = { showAddBottomSheet = true }) { @@ -154,7 +151,8 @@ fun TokensScreen( EmptyState( icon = Icons.Outlined.SearchOff, title = stringResource(R.string.empty_search_title), - hint = stringResource(R.string.empty_search_hint) + hint = stringResource(R.string.empty_search_hint), + iconShape = MaterialShapes.Sunny.toShape() ) } } else { @@ -214,12 +212,14 @@ fun TokensScreen( } } +@OptIn(ExperimentalMaterial3ExpressiveApi::class) @Composable private fun EmptyState( icon: ImageVector, title: String, hint: String, modifier: Modifier = Modifier, + iconShape: Shape = MaterialShapes.Cookie9Sided.toShape(), actionLabel: String? = null, onActionClick: (() -> Unit)? = null ) { @@ -233,15 +233,15 @@ private fun EmptyState( Box( modifier = Modifier .size(96.dp) - .clip(CircleShape) - .background(MaterialTheme.colorScheme.surfaceContainerHighest), + .clip(iconShape) + .background(MaterialTheme.colorScheme.primaryContainer), contentAlignment = Alignment.Center ) { Icon( imageVector = icon, contentDescription = null, modifier = Modifier.size(44.dp), - tint = MaterialTheme.colorScheme.onSurfaceVariant + tint = MaterialTheme.colorScheme.onPrimaryContainer ) } Spacer(modifier = Modifier.height(20.dp)) @@ -471,16 +471,26 @@ fun TokenCard( } } +@OptIn(ExperimentalMaterial3ExpressiveApi::class) @Composable fun TokenIcon(token: Token) { - val letter = token.issuer.takeIf { it.isNotBlank() }?.firstOrNull()?.uppercaseChar() - ?: token.serviceName.firstOrNull()?.uppercaseChar() + val letter = token.issuer.takeIf { it.isNotBlank() }?.firstOrNull()?.uppercaseChar() + ?: token.serviceName.firstOrNull()?.uppercaseChar() ?: '?' - + + // Deterministic per-token shape: keep 4 understated options, no randomness between recompositions + val shapes = listOf( + MaterialShapes.Circle, + MaterialShapes.Cookie9Sided, + MaterialShapes.Pill, + MaterialShapes.Sunny, + ) + val shape = shapes[abs(token.issuer.hashCode() * 31 + token.serviceName.hashCode()) % shapes.size] + Box( modifier = Modifier .size(48.dp) - .clip(CircleShape) + .clip(shape.toShape()) .background(MaterialTheme.colorScheme.primaryContainer), contentAlignment = Alignment.Center ) {