From d18bc9f1e4ade9af1f8bdb807a01d160608abcb8 Mon Sep 17 00:00:00 2001 From: Xaver Date: Thu, 21 Nov 2024 15:06:49 +0100 Subject: [PATCH] adding tasks now working --- app/src/main/java/at/xaxa/demonstrator2/ui/DemoUI.kt | 11 +++++++---- .../java/at/xaxa/demonstrator2/ui/DemoViewModel.kt | 5 +++-- 2 files changed, 10 insertions(+), 6 deletions(-) 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 8ee428c..282644f 100644 --- a/app/src/main/java/at/xaxa/demonstrator2/ui/DemoUI.kt +++ b/app/src/main/java/at/xaxa/demonstrator2/ui/DemoUI.kt @@ -35,6 +35,7 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel +import androidx.navigation.NavHostController import androidx.navigation.NavType import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable @@ -81,13 +82,13 @@ fun DemoApp(modifier: Modifier = Modifier) { modifier = Modifier.padding(innerPadding) ) { composable(DemoRoutes.List.route){ - ListScreen( onEditClick = { + ListScreen(onEditClick = { navController.navigate(DemoRoutes.Edit.route.replace("{taskId}", "$it")) }){ navController.navigate(DemoRoutes.Detail.route.replace("{taskId}", "$it")) } } - composable(DemoRoutes.Add.route) { AddScreen() } + composable(DemoRoutes.Add.route) { AddScreen(navController = navController) } composable( route = DemoRoutes.Detail.route, arguments = listOf(navArgument("taskId") { @@ -115,7 +116,7 @@ fun ListScreen( modifier: Modifier = Modifier, demoViewModel: DemoViewModel = viewModel(factory = AppViewModelProvider.Factory), onEditClick: (Int) -> Unit, - onCardClick: (Int) -> Unit + onCardClick: (Int) -> Unit, ) { Box(Modifier.fillMaxSize()) { val state by demoViewModel.taskUiState.collectAsStateWithLifecycle(); @@ -160,6 +161,7 @@ fun AddScreen( //task: Task, modifier: Modifier = Modifier, demoViewModel: DemoViewModel = viewModel(factory = AppViewModelProvider.Factory), + navController: NavHostController ) { var taskText by remember { mutableStateOf("") } var descText by remember { mutableStateOf("") } @@ -195,7 +197,8 @@ fun AddScreen( FilledTonalButton( modifier = Modifier.fillMaxWidth(), onClick = { - demoViewModel.onAddButtonClicked(Task{0, "", ""}) + demoViewModel.onAddButtonClicked(Task(0, taskText, descText)) + navController.navigate(DemoRoutes.List.route) }) { Text("Save") } 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 1fd1f21..1bf0be8 100644 --- a/app/src/main/java/at/xaxa/demonstrator2/ui/DemoViewModel.kt +++ b/app/src/main/java/at/xaxa/demonstrator2/ui/DemoViewModel.kt @@ -2,6 +2,7 @@ package at.xaxa.demonstrator2.ui import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import at.xaxa.demonstrator2.data.Task import at.xaxa.demonstrator2.data.TaskRepository import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.map @@ -25,9 +26,9 @@ class DemoViewModel(val repository: TaskRepository) : ViewModel() { initialValue = DemoUiState(emptyList()) ) - fun onAddButtonClicked() { + fun onAddButtonClicked(task: Task) { viewModelScope.launch { - repository.addRandomTask() + repository.addTask(task) } } }