From a0c4858fc8f6f5b3910efad012cc8cb1a96fb935 Mon Sep 17 00:00:00 2001 From: Brais Date: Mon, 29 Apr 2024 21:03:07 +0200 Subject: [PATCH] Added ids to the views that needed them, to the navigation and refactor --- .../add_edit_execution/AddEditExecutionScreen.kt | 13 +++++++++++-- .../AddEditExecutionViewModel.kt | 3 --- .../state/AddEditExecutionUiState.kt | 3 +++ .../add_edit_task/AddEditTaskViewModel.kt | 2 -- .../show_task_detail/TaskDetailScreen.kt | 15 ++++++++++++++- .../show_task_detail/TaskDetailViewModel.kt | 2 +- .../components/CompletedExecutionComponent.kt | 1 + .../components/UnCompletedExecutionComponent.kt | 1 + .../show_task_detail/state/ExecutionUiState.kt | 2 ++ .../show_task_detail/state/TaskDetailUiState.kt | 2 ++ .../braiso_22/cozycave/router/AppNavigation.kt | 4 +++- app/src/main/res/values/strings.xml | 1 + 12 files changed, 39 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/AddEditExecutionScreen.kt b/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/AddEditExecutionScreen.kt index 001ea66..25be6f9 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/AddEditExecutionScreen.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/AddEditExecutionScreen.kt @@ -1,6 +1,5 @@ package com.braiso_22.cozycave.feature_execution.presentation.add_edit_execution -import android.widget.Space import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.material.icons.Icons @@ -83,7 +82,17 @@ fun AddEditExecutionScreenContent( modifier = modifier, topBar = { TopAppBar( - title = { Text(stringResource(R.string.add_execution)) }, + title = { + Text( + stringResource( + if (state.isEditExecution) { + R.string.edit_execution + } else { + R.string.add_execution + } + ) + ) + }, colors = TopAppBarDefaults.topAppBarColors( containerColor = MaterialTheme.colorScheme.primaryContainer, titleContentColor = MaterialTheme.colorScheme.primary diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/AddEditExecutionViewModel.kt b/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/AddEditExecutionViewModel.kt index 5fbffbf..2b210ac 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/AddEditExecutionViewModel.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/AddEditExecutionViewModel.kt @@ -10,11 +10,9 @@ import androidx.lifecycle.viewModelScope import com.braiso_22.cozycave.R import com.braiso_22.cozycave.feature_execution.domain.use_case.AddExecutionUseCase import com.braiso_22.cozycave.feature_execution.domain.use_case.GetExecutionByIdUseCase -import com.braiso_22.cozycave.feature_execution.domain.use_case.GetExecutionsByRelatedIdUseCase import com.braiso_22.cozycave.feature_execution.presentation.add_edit_execution.state.AddEditExecutionUiState import com.braiso_22.cozycave.feature_execution.presentation.add_edit_execution.state.asExecution import com.braiso_22.cozycave.feature_execution.presentation.add_edit_execution.state.toUiState -import com.braiso_22.cozycave.feature_task.presentation.add_edit_task.state.toUiState import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow @@ -94,4 +92,3 @@ class AddEditExecutionViewModel @Inject constructor( data class SavedCloseScreen(val message: Int) : UiEvent() } } - diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/state/AddEditExecutionUiState.kt b/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/state/AddEditExecutionUiState.kt index 9a94a96..d6f3c33 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/state/AddEditExecutionUiState.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_execution/presentation/add_edit_execution/state/AddEditExecutionUiState.kt @@ -7,6 +7,7 @@ import java.time.LocalTime import java.time.format.DateTimeFormatter data class AddEditExecutionUiState( + val id: Int = 0, val startDate: String = "", val startTime: String = "", val endDate: String = "", @@ -17,6 +18,7 @@ data class AddEditExecutionUiState( ) fun Execution.toUiState() = AddEditExecutionUiState( + id = id, startDate = startDateTime.toFormatedDate(), startTime = startDateTime.toFormatedTime(), endDate = endDateTime.toFormatedDate(), @@ -39,6 +41,7 @@ fun LocalDateTime.toFormatedTime(): String { fun AddEditExecutionUiState.asExecution(): Execution { return Execution( + id = id, startDateTime = LocalDateTime.of( LocalDate.parse(startDate, DateTimeFormatter.ofPattern("dd/MM/yyyy")), LocalTime.parse(startTime) diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/add_edit_task/AddEditTaskViewModel.kt b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/add_edit_task/AddEditTaskViewModel.kt index d6423cf..7fba0b8 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/add_edit_task/AddEditTaskViewModel.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/add_edit_task/AddEditTaskViewModel.kt @@ -6,7 +6,6 @@ import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.braiso_22.cozycave.feature_task.domain.Task import com.braiso_22.cozycave.feature_task.domain.use_case.AddTaskUseCase import com.braiso_22.cozycave.feature_task.domain.use_case.GetTaskByIdUseCase import com.braiso_22.cozycave.feature_task.presentation.add_edit_task.AddEditTaskEvent.* @@ -14,7 +13,6 @@ import com.braiso_22.cozycave.feature_task.presentation.add_edit_task.state.AddE import com.braiso_22.cozycave.feature_task.presentation.add_edit_task.state.toTask import com.braiso_22.cozycave.feature_task.presentation.add_edit_task.state.toUiState import dagger.hilt.android.lifecycle.HiltViewModel -import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.launch diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/TaskDetailScreen.kt b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/TaskDetailScreen.kt index 80ba463..b6f733d 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/TaskDetailScreen.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/TaskDetailScreen.kt @@ -1,5 +1,6 @@ package com.braiso_22.cozycave.feature_task.presentation.show_task_detail +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed @@ -27,6 +28,7 @@ import com.braiso_22.cozycave.feature_task.presentation.show_task_detail.state.T fun TaskDetailScreen( windowSizeClass: WindowSizeClass, onBack: () -> Unit, + onClickTaskExecution: (taskId: Int, executionId: Int) -> Unit, modifier: Modifier = Modifier, viewModel: TaskDetailViewModel = hiltViewModel(), ) { @@ -35,6 +37,7 @@ fun TaskDetailScreen( state = state, windowSizeClass = windowSizeClass, onBack = onBack, + onClickTaskExecution = onClickTaskExecution, modifier = modifier, ) } @@ -44,6 +47,7 @@ fun TaskDetailScreen( fun TaskDetailScreenContent( state: TaskDetailUiState, windowSizeClass: WindowSizeClass, + onClickTaskExecution: (Int, Int) -> Unit, onBack: () -> Unit, modifier: Modifier = Modifier, ) { @@ -113,6 +117,9 @@ fun TaskDetailScreenContent( modifier = Modifier .fillMaxSize() .padding(16.dp) + .clickable { + onClickTaskExecution(state.taskId, execution.id) + } ) if (index != state.unFinishedExecutions.lastIndex) HorizontalDivider( @@ -139,6 +146,9 @@ fun TaskDetailScreenContent( modifier = Modifier .fillMaxSize() .padding(8.dp) + .clickable { + onClickTaskExecution(state.taskId, execution.id) + } ) if (index != state.completedExecutions.lastIndex) HorizontalDivider( @@ -156,13 +166,16 @@ fun TaskDetailScreenContent( private fun TaskDetailScreenContentPreview(dpSize: DpSize) { val state = remember { mutableStateOf( - TaskDetailUiState(), + TaskDetailUiState(taskId = 0), ) } TaskDetailScreenContent( state = state.value, windowSizeClass = WindowSizeClass.calculateFromSize(dpSize), onBack = {}, + onClickTaskExecution = { taskId, executionId -> + + }, modifier = Modifier.fillMaxSize() ) } diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/TaskDetailViewModel.kt b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/TaskDetailViewModel.kt index 4e58ba1..06434a4 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/TaskDetailViewModel.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/TaskDetailViewModel.kt @@ -23,7 +23,7 @@ class TaskDetailViewModel @Inject constructor( private val savedStateHandle: SavedStateHandle, ) : ViewModel() { - private val _state = mutableStateOf(TaskDetailUiState()) + private val _state = mutableStateOf(TaskDetailUiState(taskId = 0)) val state: State = _state private val _taskId = mutableIntStateOf(0) diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/components/CompletedExecutionComponent.kt b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/components/CompletedExecutionComponent.kt index e301b67..d750116 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/components/CompletedExecutionComponent.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/components/CompletedExecutionComponent.kt @@ -71,6 +71,7 @@ fun CompletedExecutionComponent( private fun ExecutionComponentPreview() { CompletedExecutionComponent( state = ExecutionUiState( + id = 0, startDate = "00/00/0000", endDate = "00/07/0000", startHour = "00:00", diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/components/UnCompletedExecutionComponent.kt b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/components/UnCompletedExecutionComponent.kt index 5ee915d..488a304 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/components/UnCompletedExecutionComponent.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/components/UnCompletedExecutionComponent.kt @@ -45,6 +45,7 @@ fun UnCompletedExecutionComponent( private fun UnCompletedExecutionComponentPreview() { UnCompletedExecutionComponent( state = ExecutionUiState( + id = 0, startDate = "22/03/2002", endDate = "22/03/2002", startHour = "01:00", diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/state/ExecutionUiState.kt b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/state/ExecutionUiState.kt index ecde9c9..615d42b 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/state/ExecutionUiState.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/state/ExecutionUiState.kt @@ -5,6 +5,7 @@ import com.braiso_22.cozycave.feature_execution.presentation.add_edit_execution. import com.braiso_22.cozycave.feature_execution.presentation.add_edit_execution.state.toFormatedTime data class ExecutionUiState( + val id: Int, val startDate: String, val endDate: String, val startHour: String, @@ -12,6 +13,7 @@ data class ExecutionUiState( ) fun Execution.toUiState() = ExecutionUiState( + id = this.id, startDate = this.startDateTime.toFormatedDate(), startHour = this.startDateTime.toFormatedTime(), endDate = this.endDateTime.toFormatedDate(), diff --git a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/state/TaskDetailUiState.kt b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/state/TaskDetailUiState.kt index 2b062d7..21aaafe 100644 --- a/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/state/TaskDetailUiState.kt +++ b/app/src/main/java/com/braiso_22/cozycave/feature_task/presentation/show_task_detail/state/TaskDetailUiState.kt @@ -4,12 +4,14 @@ import com.braiso_22.cozycave.feature_execution.domain.Execution import com.braiso_22.cozycave.feature_task.domain.TaskWithExecutions data class TaskDetailUiState( + val taskId: Int, val name: String = "Task", val completedExecutions: List = emptyList(), val unFinishedExecutions: List = emptyList(), ) fun TaskWithExecutions.toUiState(): TaskDetailUiState = TaskDetailUiState( + taskId = this.task.id, name = this.task.name, completedExecutions = this.executions.filter { it.finished diff --git a/app/src/main/java/com/braiso_22/cozycave/router/AppNavigation.kt b/app/src/main/java/com/braiso_22/cozycave/router/AppNavigation.kt index d8b52e5..1b9ac6c 100644 --- a/app/src/main/java/com/braiso_22/cozycave/router/AppNavigation.kt +++ b/app/src/main/java/com/braiso_22/cozycave/router/AppNavigation.kt @@ -1,7 +1,6 @@ package com.braiso_22.cozycave.router import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold import androidx.compose.material3.SnackbarHost @@ -153,6 +152,9 @@ fun AppNavigation( onBack = { navController.popBackStack() }, + onClickTaskExecution = { taskId, executionId -> + navController.navigate(AppScreens.AddEditExecution.route + taskId + "/true/" + executionId) + }, modifier = modifier.fillMaxSize() ) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b5cda0a..1814933 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,4 +13,5 @@ Add execution Execution saved correctly Task detail + Edit execution \ No newline at end of file