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
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavType import androidx.navigation.NavType
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
@ -81,13 +82,13 @@ fun DemoApp(modifier: Modifier = Modifier) {
modifier = Modifier.padding(innerPadding) modifier = Modifier.padding(innerPadding)
) { ) {
composable(DemoRoutes.List.route){ composable(DemoRoutes.List.route){
ListScreen( onEditClick = { ListScreen(onEditClick = {
navController.navigate(DemoRoutes.Edit.route.replace("{taskId}", "$it")) navController.navigate(DemoRoutes.Edit.route.replace("{taskId}", "$it"))
}){ }){
navController.navigate(DemoRoutes.Detail.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( composable(
route = DemoRoutes.Detail.route, route = DemoRoutes.Detail.route,
arguments = listOf(navArgument("taskId") { arguments = listOf(navArgument("taskId") {
@ -115,7 +116,7 @@ fun ListScreen(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
demoViewModel: DemoViewModel = viewModel(factory = AppViewModelProvider.Factory), demoViewModel: DemoViewModel = viewModel(factory = AppViewModelProvider.Factory),
onEditClick: (Int) -> Unit, onEditClick: (Int) -> Unit,
onCardClick: (Int) -> Unit onCardClick: (Int) -> Unit,
) { ) {
Box(Modifier.fillMaxSize()) { Box(Modifier.fillMaxSize()) {
val state by demoViewModel.taskUiState.collectAsStateWithLifecycle(); val state by demoViewModel.taskUiState.collectAsStateWithLifecycle();
@ -160,6 +161,7 @@ fun AddScreen(
//task: Task, //task: Task,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
demoViewModel: DemoViewModel = viewModel(factory = AppViewModelProvider.Factory), demoViewModel: DemoViewModel = viewModel(factory = AppViewModelProvider.Factory),
navController: NavHostController
) { ) {
var taskText by remember { mutableStateOf("") } var taskText by remember { mutableStateOf("") }
var descText by remember { mutableStateOf("") } var descText by remember { mutableStateOf("") }
@ -195,7 +197,8 @@ fun AddScreen(
FilledTonalButton( FilledTonalButton(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
onClick = { onClick = {
demoViewModel.onAddButtonClicked(Task{0, "", ""}) demoViewModel.onAddButtonClicked(Task(0, taskText, descText))
navController.navigate(DemoRoutes.List.route)
}) { }) {
Text("Save") Text("Save")
} }

5
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.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import at.xaxa.demonstrator2.data.Task
import at.xaxa.demonstrator2.data.TaskRepository import at.xaxa.demonstrator2.data.TaskRepository
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
@ -25,9 +26,9 @@ class DemoViewModel(val repository: TaskRepository) : ViewModel() {
initialValue = DemoUiState(emptyList()) initialValue = DemoUiState(emptyList())
) )
fun onAddButtonClicked() { fun onAddButtonClicked(task: Task) {
viewModelScope.launch { viewModelScope.launch {
repository.addRandomTask() repository.addTask(task)
} }
} }
} }

Loading…
Cancel
Save