diff --git a/app/src/main/java/ru/ulstu/is/pmu/api/task/RestTaskRepository.kt b/app/src/main/java/ru/ulstu/is/pmu/api/task/RestTaskRepository.kt index 18efb59..68dd86b 100644 --- a/app/src/main/java/ru/ulstu/is/pmu/api/task/RestTaskRepository.kt +++ b/app/src/main/java/ru/ulstu/is/pmu/api/task/RestTaskRepository.kt @@ -60,6 +60,4 @@ class RestTaskRepository( override suspend fun deleteTask(task: Task) { service.deleteTask(task.uid).toTask() } - - } \ No newline at end of file diff --git a/app/src/main/java/ru/ulstu/is/pmu/database/student/model/Task.kt b/app/src/main/java/ru/ulstu/is/pmu/database/student/model/Task.kt index 863437c..0b5b886 100644 --- a/app/src/main/java/ru/ulstu/is/pmu/database/student/model/Task.kt +++ b/app/src/main/java/ru/ulstu/is/pmu/database/student/model/Task.kt @@ -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, ) { diff --git a/app/src/main/java/ru/ulstu/is/pmu/database/student/repository/OfflineTaskRepository.kt b/app/src/main/java/ru/ulstu/is/pmu/database/student/repository/OfflineTaskRepository.kt index 108b496..dbb5a19 100644 --- a/app/src/main/java/ru/ulstu/is/pmu/database/student/repository/OfflineTaskRepository.kt +++ b/app/src/main/java/ru/ulstu/is/pmu/database/student/repository/OfflineTaskRepository.kt @@ -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 = taskDao.getAll() diff --git a/app/src/main/java/ru/ulstu/is/pmu/ui/student/list/StudentList.kt b/app/src/main/java/ru/ulstu/is/pmu/ui/student/list/StudentList.kt index 2d5e8cb..5725fc2 100644 --- a/app/src/main/java/ru/ulstu/is/pmu/ui/student/list/StudentList.kt +++ b/app/src/main/java/ru/ulstu/is/pmu/ui/student/list/StudentList.kt @@ -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, - modifier = Modifier - .padding(vertical = 7.dp) - .clickable { onClick(task.uid) }) + Card( + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 7.dp) + ) { + 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, 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,34 +238,13 @@ 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() } + SwipeToDelete( + task = task, + onClick = onClick, + onDeleteClick = onDeleteClick, + onAddToFavoritesClick = onAddToFavoritesClick, + onEditClick = onEditClick ) - - AnimatedVisibility( - show, exit = fadeOut(spring()) - ) { - SwipeToDelete( - dismissState = dismissState, - task = task, - onClick = onClick - ) - } - - LaunchedEffect(show) { - if (!show) { - delay(800) - onSwipe(task) - } - } } } } @@ -227,6 +258,8 @@ private fun TaskList( } } + + @Composable private fun TaskListItem( task: Task, modifier: Modifier = Modifier @@ -243,42 +276,4 @@ 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() - ).collectAsLazyPagingItems(), - onClick = {}, - onSwipe = {} - ) - } - } } \ No newline at end of file diff --git a/app/src/main/java/ru/ulstu/is/pmu/ui/student/list/StudentListViewModel.kt b/app/src/main/java/ru/ulstu/is/pmu/ui/student/list/StudentListViewModel.kt index c2a0337..06a0368 100644 --- a/app/src/main/java/ru/ulstu/is/pmu/ui/student/list/StudentListViewModel.kt +++ b/app/src/main/java/ru/ulstu/is/pmu/ui/student/list/StudentListViewModel.kt @@ -15,4 +15,5 @@ class TaskListViewModel( suspend fun deleteTask(task: Task) { taskRepository.deleteTask(task) } + } \ No newline at end of file diff --git a/server/data.json b/server/data.json index 949dca7..94ae9c7 100644 --- a/server/data.json +++ b/server/data.json @@ -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 } ] } \ No newline at end of file