Browse Source

adding tasks now working

master
Xaver 1 week ago
parent
commit
d18bc9f1e4
  1. 11
      app/src/main/java/at/xaxa/demonstrator2/ui/DemoUI.kt
  2. 5
      app/src/main/java/at/xaxa/demonstrator2/ui/DemoViewModel.kt

11
app/src/main/java/at/xaxa/demonstrator2/ui/DemoUI.kt

@ -35,6 +35,7 @@ import androidx.compose.ui.tooling.preview.Preview @@ -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) { @@ -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( @@ -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( @@ -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( @@ -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")
}

5
app/src/main/java/at/xaxa/demonstrator2/ui/DemoViewModel.kt

@ -2,6 +2,7 @@ package at.xaxa.demonstrator2.ui @@ -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() { @@ -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)
}
}
}

Loading…
Cancel
Save