From 0f27eab03b865014f1acc4e197a6a6ee25034097 Mon Sep 17 00:00:00 2001 From: Super12138 <70494801+Super12138@users.noreply.github.com> Date: Wed, 15 Jan 2025 16:55:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BB=84=E4=BB=B6=E5=8C=96?= =?UTF-8?q?=E9=A1=B6=E9=83=A8=E5=BA=94=E7=94=A8=E6=A0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../super12138/todo/ui/pages/main/MainPage.kt | 68 ++----------- .../ui/pages/main/components/TodoTopAppBar.kt | 97 +++++++++++++++++++ 2 files changed, 105 insertions(+), 60 deletions(-) create mode 100644 app/src/main/kotlin/cn/super12138/todo/ui/pages/main/components/TodoTopAppBar.kt diff --git a/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/MainPage.kt b/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/MainPage.kt index 3e81df6..5e92814 100644 --- a/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/MainPage.kt +++ b/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/MainPage.kt @@ -43,6 +43,7 @@ import androidx.window.core.layout.WindowWidthSizeClass import cn.super12138.todo.R import cn.super12138.todo.logic.database.TodoEntity import cn.super12138.todo.ui.pages.main.components.TodoFAB +import cn.super12138.todo.ui.pages.main.components.TodoTopAppBar import cn.super12138.todo.ui.viewmodels.MainViewModel import kotlinx.coroutines.launch @@ -71,10 +72,6 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) { } } - val animatedTopAppBarColors by animateColorAsState( - targetValue = if (isSelectedIdsEmpty) MaterialTheme.colorScheme.surface else MaterialTheme.colorScheme.surfaceContainerHighest - ) - BackHandler(enabled = !isSelectedIdsEmpty) { // 当按下返回键(或进行返回操作)时清空选择 viewModel.clearAllTodoSelection() @@ -82,62 +79,13 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) { Scaffold( topBar = { - TopAppBar( - navigationIcon = { - AnimatedVisibility(!isSelectedIdsEmpty) { - IconButton(onClick = { viewModel.clearAllTodoSelection() }) { - Icon( - imageVector = Icons.Outlined.Close, - contentDescription = stringResource(R.string.tip_clear_selected_items) - ) - } - } - }, - title = { - Text( - text = if (isSelectedIdsEmpty) { - stringResource(R.string.app_name) - } else { - stringResource( - R.string.title_selected_count, - selectedTodoIds.value.size - ) - } - ) - }, - actions = { - AnimatedContent(isSelectedIdsEmpty) { isEmpty -> - if (isEmpty) { - IconButton( - onClick = {} - ) { - Icon( - imageVector = Icons.Outlined.Settings, - contentDescription = stringResource(R.string.page_settings) - ) - } - } else { - Row { - IconButton(onClick = { viewModel.toggleAllSelected() }) { - Icon( - imageVector = Icons.Outlined.SelectAll, - contentDescription = stringResource(R.string.tip_select_all) - ) - } - IconButton(onClick = { viewModel.deleteSelectedTodo() }) { - Icon( - imageVector = Icons.Outlined.Delete, - contentDescription = stringResource(R.string.action_delete) - ) - } - } - } - } - - }, - colors = TopAppBarDefaults.largeTopAppBarColors().copy( - containerColor = animatedTopAppBarColors - ) + TodoTopAppBar( + selectedTodoIds = selectedTodoIds.value, + selectedMode = !isSelectedIdsEmpty, + onCancelSelect = { viewModel.clearAllTodoSelection() }, + onSelectAll = { viewModel.toggleAllSelected() }, + onDeleteSelectedTodo = { viewModel.deleteSelectedTodo() }, + toSettingsPage = {} ) }, floatingActionButton = { diff --git a/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/components/TodoTopAppBar.kt b/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/components/TodoTopAppBar.kt new file mode 100644 index 0000000..72359e9 --- /dev/null +++ b/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/components/TodoTopAppBar.kt @@ -0,0 +1,97 @@ +package cn.super12138.todo.ui.pages.main.components + +import androidx.compose.animation.AnimatedContent +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.animateColorAsState +import androidx.compose.foundation.layout.Row +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.Close +import androidx.compose.material.icons.outlined.Delete +import androidx.compose.material.icons.outlined.SelectAll +import androidx.compose.material.icons.outlined.Settings +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.material3.TopAppBar +import androidx.compose.material3.TopAppBarDefaults +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import cn.super12138.todo.R + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun TodoTopAppBar( + selectedTodoIds: List, + selectedMode: Boolean, + onCancelSelect: () -> Unit, + onSelectAll: () -> Unit, + onDeleteSelectedTodo: () -> Unit, + toSettingsPage: () -> Unit, + modifier: Modifier = Modifier +) { + val animatedTopAppBarColors by animateColorAsState( + targetValue = if (selectedMode) MaterialTheme.colorScheme.surfaceContainerHighest else MaterialTheme.colorScheme.surface + ) + + TopAppBar( + navigationIcon = { + AnimatedVisibility(selectedMode) { + IconButton(onClick = onCancelSelect) { + Icon( + imageVector = Icons.Outlined.Close, + contentDescription = stringResource(R.string.tip_clear_selected_items) + ) + } + } + }, + title = { + Text( + text = if (!selectedMode) { + stringResource(R.string.app_name) + } else { + stringResource( + R.string.title_selected_count, + selectedTodoIds.size + ) + } + ) + }, + actions = { + AnimatedContent(selectedMode) { inSelectedMode -> + if (!inSelectedMode) { + IconButton( + onClick = toSettingsPage + ) { + Icon( + imageVector = Icons.Outlined.Settings, + contentDescription = stringResource(R.string.page_settings) + ) + } + } else { + Row { + IconButton(onClick = onSelectAll) { + Icon( + imageVector = Icons.Outlined.SelectAll, + contentDescription = stringResource(R.string.tip_select_all) + ) + } + IconButton(onClick = onDeleteSelectedTodo) { + Icon( + imageVector = Icons.Outlined.Delete, + contentDescription = stringResource(R.string.action_delete) + ) + } + } + } + } + + }, + colors = TopAppBarDefaults.largeTopAppBarColors().copy( + containerColor = animatedTopAppBarColors + ) + ) +} \ No newline at end of file