主页换 Flexible 大标题,空状态与令牌图标采用 Expressive 形状底座

This commit is contained in:
2026-10-14 14:47:59 +08:00
parent 666542ce17
commit e0bd974f59
@@ -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
) {