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.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
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.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Add
|
||||
|
|
@ -30,7 +27,6 @@ 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.ui.pages.main.components.TodoCard
|
||||
import cn.super12138.todo.ui.viewmodels.MainViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
|
|
@ -44,7 +40,6 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
|
|
@ -95,6 +90,7 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
|||
) { innerPadding ->
|
||||
Column(modifier = Modifier.padding(innerPadding)) {
|
||||
ProgressFragment(
|
||||
viewModel = viewModel,
|
||||
modifier = Modifier
|
||||
.weight(2f)
|
||||
.fillMaxSize()
|
||||
|
|
@ -103,6 +99,19 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
|||
ManagerFragment(
|
||||
state = listState,
|
||||
list = toDoList.value,
|
||||
onItemClick = {},
|
||||
onItemChecked = { item ->
|
||||
item.apply {
|
||||
viewModel.updateTodo(
|
||||
TodoEntity(
|
||||
content = content,
|
||||
subject = subject,
|
||||
isCompleted = true,
|
||||
id = id
|
||||
)
|
||||
)
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
.weight(3f)
|
||||
.fillMaxSize()
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ import cn.super12138.todo.ui.pages.main.components.TodoCard
|
|||
@Composable
|
||||
fun ManagerFragment(
|
||||
state: LazyListState,
|
||||
onItemClick: (TodoEntity) -> Unit = {},
|
||||
onItemChecked: (TodoEntity) -> Unit = {},
|
||||
list: List<TodoEntity>,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
|
|
@ -32,6 +34,8 @@ fun ManagerFragment(
|
|||
TodoCard(
|
||||
content = item.content,
|
||||
subject = item.subject,
|
||||
onCardClick = { onItemClick(item) },
|
||||
onChecked = { onItemChecked(item) },
|
||||
modifier = Modifier
|
||||
.padding(vertical = 5.dp)
|
||||
.animateItem()
|
||||
|
|
|
|||
|
|
@ -10,18 +10,25 @@ import androidx.compose.material3.MaterialTheme
|
|||
import androidx.compose.material3.ProgressIndicatorDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
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.Modifier
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cn.super12138.todo.ui.viewmodels.MainViewModel
|
||||
|
||||
@Composable
|
||||
fun ProgressFragment(modifier: Modifier = Modifier) {
|
||||
var progress by rememberSaveable { mutableFloatStateOf(0.1f) }
|
||||
fun ProgressFragment(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
||||
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(
|
||||
targetValue = progress,
|
||||
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec,
|
||||
|
|
@ -40,7 +47,7 @@ fun ProgressFragment(modifier: Modifier = Modifier) {
|
|||
Column {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(
|
||||
text = "0",
|
||||
text = completedTasks.toString(),
|
||||
style = MaterialTheme.typography.displaySmall.copy(
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
|
@ -52,7 +59,7 @@ fun ProgressFragment(modifier: Modifier = Modifier) {
|
|||
)
|
||||
)
|
||||
Text(
|
||||
text = "0",
|
||||
text = totalTask.toString(),
|
||||
style = MaterialTheme.typography.displayMedium.copy(
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
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.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
|
|
@ -17,7 +18,6 @@ import androidx.compose.runtime.Composable
|
|||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
|
||||
@Composable
|
||||
fun TodoCard(
|
||||
|
|
@ -34,13 +34,16 @@ fun TodoCard(
|
|||
onClick = onCardClick
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(start = 15.dp, end = 15.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
.padding(start = 15.dp, end = 15.dp)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.weight(1f)
|
||||
verticalArrangement = Arrangement.Center,
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.fillMaxSize()
|
||||
) {
|
||||
Text(
|
||||
text = content,
|
||||
|
|
@ -48,9 +51,7 @@ fun TodoCard(
|
|||
)
|
||||
Text(
|
||||
text = subject,
|
||||
style = MaterialTheme.typography.labelLarge.copy(
|
||||
fontSize = 11.sp
|
||||
)
|
||||
style = MaterialTheme.typography.labelMedium
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -61,6 +62,23 @@ fun TodoCard(
|
|||
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)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateTodo(toDo: TodoEntity) {
|
||||
viewModelScope.launch {
|
||||
Repository.updateTodo(toDo)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue