From 111e2bb7d2a0b2cbc5a367654fef6fd2bae1b7e7 Mon Sep 17 00:00:00 2001 From: Super12138 <70494801+Super12138@users.noreply.github.com> Date: Sun, 12 Jan 2025 15:07:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=9D=E8=AF=95=E9=80=82=E9=85=8D?= =?UTF-8?q?=E6=A8=AA=E5=B1=8F=E4=B8=8B=E7=9A=84=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 但貌似重复的代码太多了,后面改改 --- .../todo/logic/model/Orientation.kt | 6 + .../super12138/todo/ui/pages/main/MainPage.kt | 171 ++++++++++++------ 2 files changed, 117 insertions(+), 60 deletions(-) create mode 100644 app/src/main/kotlin/cn/super12138/todo/logic/model/Orientation.kt diff --git a/app/src/main/kotlin/cn/super12138/todo/logic/model/Orientation.kt b/app/src/main/kotlin/cn/super12138/todo/logic/model/Orientation.kt new file mode 100644 index 0000000..0ee211b --- /dev/null +++ b/app/src/main/kotlin/cn/super12138/todo/logic/model/Orientation.kt @@ -0,0 +1,6 @@ +package cn.super12138.todo.logic.model + +enum class Orientation { + Portrait, + Landscape +} \ No newline at end of file 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 0c0c8a5..2462bf6 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 @@ -1,5 +1,6 @@ package cn.super12138.todo.ui.pages.main +import android.content.res.Configuration import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -28,10 +29,12 @@ import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import cn.super12138.todo.R import cn.super12138.todo.logic.database.TodoEntity +import cn.super12138.todo.logic.model.Orientation import cn.super12138.todo.ui.viewmodels.MainViewModel import kotlinx.coroutines.launch @@ -51,6 +54,15 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) { val bottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true) val scope = rememberCoroutineScope() + + // TODO: 寻找更好的适配方式 + val configuration = LocalConfiguration.current + val orientation = when (configuration.orientation) { + Configuration.ORIENTATION_PORTRAIT -> Orientation.Portrait + Configuration.ORIENTATION_LANDSCAPE -> Orientation.Landscape + else -> Orientation.Portrait + } + Scaffold( topBar = { TopAppBar( @@ -94,70 +106,109 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) { }, modifier = modifier ) { innerPadding -> - Column(modifier = Modifier.padding(innerPadding)) { - ProgressFragment( - totalTasks = toDoList.value.size, - completedTasks = toDoList.value.count { it.isCompleted }, - modifier = Modifier - .weight(2f) - .fillMaxSize() - ) + when (orientation) { + Orientation.Portrait -> { + Column(modifier = Modifier.padding(innerPadding)) { + ProgressFragment( + totalTasks = toDoList.value.size, + completedTasks = toDoList.value.count { it.isCompleted }, + modifier = Modifier + .weight(2f) + .fillMaxSize() + ) - ManagerFragment( - state = listState, - list = toDoList.value.filter { item -> !item.isCompleted }, - onItemClick = { item -> - openBottomSheet = true - selectedTodoItem = item - }, - onItemChecked = { item -> - item.apply { - viewModel.updateTodo( - TodoEntity( - content = content, - subject = subject, - isCompleted = true, - id = id - ) - ) - } - }, - modifier = Modifier - .weight(3f) - .fillMaxSize() - ) + ManagerFragment( + state = listState, + list = toDoList.value.filter { item -> !item.isCompleted }, + onItemClick = { item -> + openBottomSheet = true + selectedTodoItem = item + }, + onItemChecked = { item -> + item.apply { + viewModel.updateTodo( + TodoEntity( + content = content, + subject = subject, + isCompleted = true, + id = id + ) + ) + } + }, + modifier = Modifier + .weight(3f) + .fillMaxSize() + ) + } + } - if (openBottomSheet) { - TodoBottomSheet( - sheetState = bottomSheetState, - onDismissRequest = { - openBottomSheet = false - selectedTodoItem = null - }, - toDo = selectedTodoItem, - onSave = { toDo -> - selectedTodoItem = null - viewModel.addTodo(toDo) - scope - .launch { bottomSheetState.hide() } - .invokeOnCompletion { - if (!bottomSheetState.isVisible) { - openBottomSheet = false - } + Orientation.Landscape -> { + Row(modifier = Modifier.padding(innerPadding)) { + ProgressFragment( + totalTasks = toDoList.value.size, + completedTasks = toDoList.value.count { it.isCompleted }, + modifier = Modifier + .weight(2f) + .fillMaxSize() + ) + + ManagerFragment( + state = listState, + list = toDoList.value.filter { item -> !item.isCompleted }, + onItemClick = { item -> + openBottomSheet = true + selectedTodoItem = item + }, + onItemChecked = { item -> + item.apply { + viewModel.updateTodo( + TodoEntity( + content = content, + subject = subject, + isCompleted = true, + id = id + ) + ) } - }, - onClose = { - selectedTodoItem = null - scope - .launch { bottomSheetState.hide() } - .invokeOnCompletion { - if (!bottomSheetState.isVisible) { - openBottomSheet = false - } - } - } - ) + }, + modifier = Modifier + .weight(3f) + .fillMaxSize() + ) + } } } } + if (openBottomSheet) { + TodoBottomSheet( + sheetState = bottomSheetState, + onDismissRequest = { + openBottomSheet = false + selectedTodoItem = null + }, + toDo = selectedTodoItem, + onSave = { toDo -> + selectedTodoItem = null + viewModel.addTodo(toDo) + scope + .launch { bottomSheetState.hide() } + .invokeOnCompletion { + if (!bottomSheetState.isVisible) { + openBottomSheet = false + } + } + }, + onClose = { + selectedTodoItem = null + scope + .launch { bottomSheetState.hide() } + .invokeOnCompletion { + if (!bottomSheetState.isVisible) { + openBottomSheet = false + } + } + } + ) + } } \ No newline at end of file