feat: 进度条和数据绑定 & 支持标记待办为已完成
This commit is contained in:
parent
35c67e418a
commit
68e282c944
5 changed files with 63 additions and 19 deletions
|
|
@ -2,12 +2,9 @@ package cn.super12138.todo.ui.pages.main
|
||||||
|
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
|
||||||
import androidx.compose.foundation.lazy.items
|
|
||||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.outlined.Add
|
import androidx.compose.material.icons.outlined.Add
|
||||||
|
|
@ -30,7 +27,6 @@ import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import cn.super12138.todo.R
|
import cn.super12138.todo.R
|
||||||
import cn.super12138.todo.logic.database.TodoEntity
|
import cn.super12138.todo.logic.database.TodoEntity
|
||||||
import cn.super12138.todo.ui.pages.main.components.TodoCard
|
|
||||||
import cn.super12138.todo.ui.viewmodels.MainViewModel
|
import cn.super12138.todo.ui.viewmodels.MainViewModel
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
|
|
@ -44,7 +40,6 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
topBar = {
|
topBar = {
|
||||||
TopAppBar(
|
TopAppBar(
|
||||||
|
|
@ -95,6 +90,7 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
||||||
) { innerPadding ->
|
) { innerPadding ->
|
||||||
Column(modifier = Modifier.padding(innerPadding)) {
|
Column(modifier = Modifier.padding(innerPadding)) {
|
||||||
ProgressFragment(
|
ProgressFragment(
|
||||||
|
viewModel = viewModel,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(2f)
|
.weight(2f)
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
|
|
@ -103,6 +99,19 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
||||||
ManagerFragment(
|
ManagerFragment(
|
||||||
state = listState,
|
state = listState,
|
||||||
list = toDoList.value,
|
list = toDoList.value,
|
||||||
|
onItemClick = {},
|
||||||
|
onItemChecked = { item ->
|
||||||
|
item.apply {
|
||||||
|
viewModel.updateTodo(
|
||||||
|
TodoEntity(
|
||||||
|
content = content,
|
||||||
|
subject = subject,
|
||||||
|
isCompleted = true,
|
||||||
|
id = id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(3f)
|
.weight(3f)
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ import cn.super12138.todo.ui.pages.main.components.TodoCard
|
||||||
@Composable
|
@Composable
|
||||||
fun ManagerFragment(
|
fun ManagerFragment(
|
||||||
state: LazyListState,
|
state: LazyListState,
|
||||||
|
onItemClick: (TodoEntity) -> Unit = {},
|
||||||
|
onItemChecked: (TodoEntity) -> Unit = {},
|
||||||
list: List<TodoEntity>,
|
list: List<TodoEntity>,
|
||||||
modifier: Modifier = Modifier
|
modifier: Modifier = Modifier
|
||||||
) {
|
) {
|
||||||
|
|
@ -32,6 +34,8 @@ fun ManagerFragment(
|
||||||
TodoCard(
|
TodoCard(
|
||||||
content = item.content,
|
content = item.content,
|
||||||
subject = item.subject,
|
subject = item.subject,
|
||||||
|
onCardClick = { onItemClick(item) },
|
||||||
|
onChecked = { onItemChecked(item) },
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(vertical = 5.dp)
|
.padding(vertical = 5.dp)
|
||||||
.animateItem()
|
.animateItem()
|
||||||
|
|
|
||||||
|
|
@ -10,18 +10,25 @@ import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.ProgressIndicatorDefaults
|
import androidx.compose.material3.ProgressIndicatorDefaults
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableFloatStateOf
|
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import cn.super12138.todo.ui.viewmodels.MainViewModel
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ProgressFragment(modifier: Modifier = Modifier) {
|
fun ProgressFragment(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
||||||
var progress by rememberSaveable { mutableFloatStateOf(0.1f) }
|
val toDoList = viewModel.toDos.collectAsState(initial = emptyList())
|
||||||
|
val totalTask = toDoList.value.size
|
||||||
|
val completedTasks = toDoList.value.count { it.isCompleted }
|
||||||
|
val progress = if (toDoList.value.isNotEmpty()) {
|
||||||
|
completedTasks / totalTask.toFloat()
|
||||||
|
} else {
|
||||||
|
// 没有任务时
|
||||||
|
0f
|
||||||
|
}
|
||||||
val animatedProgress by animateFloatAsState(
|
val animatedProgress by animateFloatAsState(
|
||||||
targetValue = progress,
|
targetValue = progress,
|
||||||
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec,
|
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec,
|
||||||
|
|
@ -40,7 +47,7 @@ fun ProgressFragment(modifier: Modifier = Modifier) {
|
||||||
Column {
|
Column {
|
||||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||||
Text(
|
Text(
|
||||||
text = "0",
|
text = completedTasks.toString(),
|
||||||
style = MaterialTheme.typography.displaySmall.copy(
|
style = MaterialTheme.typography.displaySmall.copy(
|
||||||
fontWeight = FontWeight.Bold
|
fontWeight = FontWeight.Bold
|
||||||
)
|
)
|
||||||
|
|
@ -52,7 +59,7 @@ fun ProgressFragment(modifier: Modifier = Modifier) {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
Text(
|
Text(
|
||||||
text = "0",
|
text = totalTask.toString(),
|
||||||
style = MaterialTheme.typography.displayMedium.copy(
|
style = MaterialTheme.typography.displayMedium.copy(
|
||||||
fontWeight = FontWeight.Bold
|
fontWeight = FontWeight.Bold
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package cn.super12138.todo.ui.pages.main.components
|
package cn.super12138.todo.ui.pages.main.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
|
@ -17,7 +18,6 @@ import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun TodoCard(
|
fun TodoCard(
|
||||||
|
|
@ -34,13 +34,16 @@ fun TodoCard(
|
||||||
onClick = onCardClick
|
onClick = onCardClick
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(start = 15.dp, end = 15.dp),
|
.padding(start = 15.dp, end = 15.dp)
|
||||||
verticalAlignment = Alignment.CenterVertically
|
|
||||||
) {
|
) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.weight(1f)
|
verticalArrangement = Arrangement.Center,
|
||||||
|
modifier = Modifier
|
||||||
|
.weight(1f)
|
||||||
|
.fillMaxSize()
|
||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
text = content,
|
text = content,
|
||||||
|
|
@ -48,9 +51,7 @@ fun TodoCard(
|
||||||
)
|
)
|
||||||
Text(
|
Text(
|
||||||
text = subject,
|
text = subject,
|
||||||
style = MaterialTheme.typography.labelLarge.copy(
|
style = MaterialTheme.typography.labelMedium
|
||||||
fontSize = 11.sp
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,6 +62,23 @@ fun TodoCard(
|
||||||
contentDescription = ""
|
contentDescription = ""
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Box(
|
||||||
|
contentAlignment = Alignment.Center,
|
||||||
|
modifier = Modifier
|
||||||
|
.width(50.dp)
|
||||||
|
.fillMaxHeight()
|
||||||
|
.background(MaterialTheme.colorScheme.tertiaryContainer)
|
||||||
|
.clickable {
|
||||||
|
onChecked()
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Outlined.Check,
|
||||||
|
tint = MaterialTheme.colorScheme.primary,
|
||||||
|
contentDescription = ""
|
||||||
|
)
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -15,4 +15,10 @@ class MainViewModel : ViewModel() {
|
||||||
Repository.insertTodo(toDo)
|
Repository.insertTodo(toDo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun updateTodo(toDo: TodoEntity) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
Repository.updateTodo(toDo)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue