diff --git a/app/src/main/java/at/xaxa/demonstrator2/data/TaskRepository.kt b/app/src/main/java/at/xaxa/demonstrator2/data/TaskRepository.kt index 21bb475..526a263 100644 --- a/app/src/main/java/at/xaxa/demonstrator2/data/TaskRepository.kt +++ b/app/src/main/java/at/xaxa/demonstrator2/data/TaskRepository.kt @@ -23,6 +23,10 @@ class TaskRepository(private val taskDao: TaskDao) { taskDao.addTask(TaskEntity(_id=0, task.name, task.details)) } + suspend fun deleteTask(task: Task) { + taskDao.deleteTask(TaskEntity(_id=task.id, task.name, task.details)) + } + suspend fun updateTask(task: Task) { taskDao.updateTask(TaskEntity(task.id, task.name, task.details)) } diff --git a/app/src/main/java/at/xaxa/demonstrator2/ui/AppViewModelProvider.kt b/app/src/main/java/at/xaxa/demonstrator2/ui/AppViewModelProvider.kt index 079f5a7..ec81ed8 100644 --- a/app/src/main/java/at/xaxa/demonstrator2/ui/AppViewModelProvider.kt +++ b/app/src/main/java/at/xaxa/demonstrator2/ui/AppViewModelProvider.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.createSavedStateHandle import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import at.xaxa.demonstrator2.TaskApplication +import at.xaxa.demonstrator2.ui.edit.TaskEditViewModel object AppViewModelProvider { val Factory = viewModelFactory { @@ -15,5 +16,9 @@ object AppViewModelProvider { initializer { DemoDetailsViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as TaskApplication).taskRepository) } + + initializer { + TaskEditViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as TaskApplication).taskRepository) + } } } \ No newline at end of file diff --git a/app/src/main/java/at/xaxa/demonstrator2/ui/DemoUI.kt b/app/src/main/java/at/xaxa/demonstrator2/ui/DemoUI.kt index 282644f..0e75699 100644 --- a/app/src/main/java/at/xaxa/demonstrator2/ui/DemoUI.kt +++ b/app/src/main/java/at/xaxa/demonstrator2/ui/DemoUI.kt @@ -14,6 +14,7 @@ import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.List import androidx.compose.material.icons.filled.Add +import androidx.compose.material.icons.outlined.Delete import androidx.compose.material.icons.outlined.Edit import androidx.compose.material3.FilledTonalButton import androidx.compose.material3.Icon @@ -127,6 +128,8 @@ fun ListScreen( onCardClick(task.id) }, onEditClick = { onEditClick(task.id) + },onDeleteClick = { + demoViewModel.onDeleteButtonClicked(task) }) } } @@ -150,7 +153,7 @@ fun TaskDetails(task: Task, modifier: Modifier = Modifier) { Column(Modifier.padding(16.dp)) { Text(task.name, style = Typography.headlineMedium) Row { - Text("Details: ${task.details}", style = Typography.headlineMedium) + Text("Details: ${task.details}", style = Typography.bodyLarge) } } } @@ -207,7 +210,7 @@ fun AddScreen( } @Composable -fun TaskListItem(task: Task, onCardClick: () -> Unit, onEditClick: ()->Unit, modifier: Modifier = Modifier) { +fun TaskListItem(task: Task, onCardClick: () -> Unit, onEditClick: ()->Unit, onDeleteClick: ()->Unit, modifier: Modifier = Modifier) { OutlinedCard( onClick = { onCardClick() }, modifier = modifier .fillMaxWidth() @@ -215,8 +218,13 @@ fun TaskListItem(task: Task, onCardClick: () -> Unit, onEditClick: ()->Unit, mod ) { Row(Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically) { Text(task.name, style = Typography.headlineMedium) - IconButton(onEditClick) { - Icon(Icons.Outlined.Edit, "Edit contact") + Row { + IconButton(onEditClick) { + Icon(Icons.Outlined.Edit, "Edit Task") + } + IconButton(onDeleteClick) { + Icon(Icons.Outlined.Delete, "Delete Task") + } } } } @@ -225,5 +233,5 @@ fun TaskListItem(task: Task, onCardClick: () -> Unit, onEditClick: ()->Unit, mod @Preview @Composable fun PreviewTaskListItem(){ - TaskListItem(Task(0, "Buy Milk", "buy milk"), {}, {}) + TaskListItem(Task(0, "Buy Milk", "buy milk"), {}, {}, {}) } \ No newline at end of file diff --git a/app/src/main/java/at/xaxa/demonstrator2/ui/DemoViewModel.kt b/app/src/main/java/at/xaxa/demonstrator2/ui/DemoViewModel.kt index 1bf0be8..92cb093 100644 --- a/app/src/main/java/at/xaxa/demonstrator2/ui/DemoViewModel.kt +++ b/app/src/main/java/at/xaxa/demonstrator2/ui/DemoViewModel.kt @@ -31,4 +31,10 @@ class DemoViewModel(val repository: TaskRepository) : ViewModel() { repository.addTask(task) } } + + fun onDeleteButtonClicked(task: Task) { + viewModelScope.launch { + repository.deleteTask(task) + } + } }