This commit is contained in:
Stranni15k 2023-12-20 18:29:14 +04:00
parent af8f416ea8
commit 90aef7e59a
6 changed files with 88 additions and 99 deletions

View File

@ -60,6 +60,4 @@ class RestTaskRepository(
override suspend fun deleteTask(task: Task) {
service.deleteTask(task.uid).toTask()
}
}

View File

@ -27,7 +27,7 @@ data class Task(
@ColumnInfo(name = "endDate")
val endDate: String,
@ColumnInfo(name = "favorite")
val favorite: Boolean,
var favorite: Boolean,
@ColumnInfo(name = "user_id", index = true)
val userId: Int,
) {

View File

@ -27,6 +27,9 @@ class OfflineTaskRepository(private val taskDao: TaskDao) : TaskRepository {
override suspend fun updateTask(task: Task) = taskDao.update(task)
override suspend fun deleteTask(task: Task) = taskDao.delete(task)
override suspend fun favoriteTask(task: Task) {
TODO("Not yet implemented")
}
fun getAllTasksPagingSource(): PagingSource<Int, Task> = taskDao.getAll()

View File

@ -15,9 +15,12 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.IconButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
@ -93,10 +96,22 @@ fun TaskList(
val route = Screen.TaskEdit.route.replace("{id}", uid.toString())
navController.navigate(route)
},
onSwipe = { task: Task ->
onDeleteClick = { task: Task ->
// Обработка удаления задачи
coroutineScope.launch {
viewModel.deleteTask(task)
}
},
onAddToFavoritesClick = { task: Task ->
// Обработка добавления задачи в избранное
coroutineScope.launch {
viewModel.favoriteTask(task)
}
},
onEditClick = { task: Task ->
// Обработка редактирования задачи
val route = Screen.TaskEdit.route.replace("{id}", task.uid.toString())
navController.navigate(route)
}
)
}
@ -130,29 +145,64 @@ fun DismissBackground(dismissState: DismissState) {
}
}
@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class)
@Composable
private fun SwipeToDelete(
dismissState: DismissState,
task: Task,
onClick: (uid: Int) -> Unit
onClick: (uid: Int) -> Unit,
onDeleteClick: (task: Task) -> Unit,
onAddToFavoritesClick: (task: Task) -> Unit,
onEditClick: (task: Task) -> Unit
) {
SwipeToDismiss(
modifier = Modifier.zIndex(1f),
state = dismissState,
directions = setOf(
DismissDirection.EndToStart
),
background = {
DismissBackground(dismissState)
},
dismissContent = {
TaskListItem(task = task,
Card(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 7.dp)
.clickable { onClick(task.uid) })
}
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = String.format("%s %s", task.name, task.description),
modifier = Modifier.weight(1f)
)
IconButton(
onClick = { onEditClick(task) },
modifier = Modifier.padding(start = 8.dp)
) {
Icon(
imageVector = Icons.Default.Edit,
contentDescription = "Редактировать",
tint = Color.Blue
)
}
IconButton(
onClick = { onDeleteClick(task) },
modifier = Modifier.padding(start = 8.dp)
) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = "Удалить",
tint = Color.Red
)
}
IconButton(
onClick = { onAddToFavoritesClick(task) },
modifier = Modifier.padding(start = 8.dp)
) {
Icon(
imageVector = Icons.Default.Favorite,
contentDescription = "Добавить в избранное"
)
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class)
@ -161,7 +211,9 @@ private fun TaskList(
modifier: Modifier = Modifier,
taskList: LazyPagingItems<Task>,
onClick: (uid: Int) -> Unit,
onSwipe: (task: Task) -> Unit
onDeleteClick: (task: Task) -> Unit,
onAddToFavoritesClick: (task: Task) -> Unit,
onEditClick: (task: Task) -> Unit
) {
val refreshScope = rememberCoroutineScope()
var refreshing by remember { mutableStateOf(false) }
@ -186,35 +238,14 @@ private fun TaskList(
) { index ->
val task = taskList[index]
task?.let {
var show by remember { mutableStateOf(true) }
val dismissState = rememberDismissState(
confirmValueChange = {
if (it == DismissValue.DismissedToStart ||
it == DismissValue.DismissedToEnd
) {
show = false
true
} else false
}, positionalThreshold = { 200.dp.toPx() }
)
AnimatedVisibility(
show, exit = fadeOut(spring())
) {
SwipeToDelete(
dismissState = dismissState,
task = task,
onClick = onClick
onClick = onClick,
onDeleteClick = onDeleteClick,
onAddToFavoritesClick = onAddToFavoritesClick,
onEditClick = onEditClick
)
}
LaunchedEffect(show) {
if (!show) {
delay(800)
onSwipe(task)
}
}
}
}
}
PullRefreshIndicator(
@ -227,6 +258,8 @@ private fun TaskList(
}
}
@Composable
private fun TaskListItem(
task: Task, modifier: Modifier = Modifier
@ -244,41 +277,3 @@ private fun TaskListItem(
}
}
}
@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun TaskListPreview() {
PmudemoTheme {
Surface(
color = MaterialTheme.colorScheme.background
) {
TaskList(
taskList = MutableStateFlow(
PagingData.from((1..20).map { i -> Task.getTask(i) })
).collectAsLazyPagingItems(),
onClick = {},
onSwipe = {}
)
}
}
}
@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun TaskEmptyListPreview() {
PmudemoTheme {
Surface(
color = MaterialTheme.colorScheme.background
) {
TaskList(
taskList = MutableStateFlow(
PagingData.empty<Task>()
).collectAsLazyPagingItems(),
onClick = {},
onSwipe = {}
)
}
}
}

View File

@ -15,4 +15,5 @@ class TaskListViewModel(
suspend fun deleteTask(task: Task) {
taskRepository.deleteTask(task)
}
}

View File

@ -14,19 +14,11 @@
"tasks": [
{
"id": 1,
"name": "Test",
"description": "Test",
"endDate": "20.12.2023",
"name": "123456",
"description": "32155",
"endDate": "55",
"favorite": false,
"userId": 1
},
{
"name": "Test1",
"description": "Test2",
"endDate": "Test3",
"favorite": false,
"userId": 1,
"id": 2
}
]
}