Skip to content

Commit

Permalink
Merge branch 'feature/create_screen_showing_data_from_origin' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
braiso-22 committed Mar 17, 2024
2 parents d69f8fa + d77ab7a commit 7f03f04
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,98 @@ package com.braiso_22.cozycave.feature_task.ui.tasks

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material3.Text
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.*
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.braiso_22.cozycave.feature_task.ui.tasks.components.TasksList
import com.braiso_22.cozycave.ui.common.isVertical

@Composable
fun TasksScreen(viewModel: TasksViewModel = hiltViewModel()) {
fun TasksScreen(
windowSizeClass: WindowSizeClass,
onClickAddTask: () -> Unit,
onClickTask: (Int) -> Unit,
modifier: Modifier = Modifier,
viewModel: TasksViewModel = hiltViewModel(),
) {
val state = viewModel.state.value
Column {
Row {
Text(text = "Tasks")
TasksScreenContent(
tasks = state.tasks,
windowSizeClass = windowSizeClass,
onClickAddTask = onClickAddTask,
onClickTask = onClickTask,
modifier = modifier,
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TasksScreenContent(
tasks: List<TaskUiState>,
windowSizeClass: WindowSizeClass,
onClickAddTask: () -> Unit,
onClickTask: (Int) -> Unit,
modifier: Modifier = Modifier,
) {
if (windowSizeClass.isVertical()) {
Scaffold(
modifier = modifier,
topBar = {
TopAppBar(
title = { Text("Tasks") },
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primaryContainer,
titleContentColor = MaterialTheme.colorScheme.primary
),
)
},
) {

TasksList(tasks = tasks, modifier = Modifier.padding(it))

}
TasksList(tasks = state.tasks)
} else {
TasksList(tasks = tasks)
}
}
}

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
@Composable
fun TasksScreenContentPreview(dpSize: DpSize) {
val tasks = remember {
mutableStateListOf(
TaskUiState("Task 1", "Description 1"),
TaskUiState("Task 2", "Description 2"),
TaskUiState("Task 3", "Description 3"),
)
}
TasksScreenContent(
tasks = tasks,
windowSizeClass = WindowSizeClass.calculateFromSize(dpSize),
onClickAddTask = {},
onClickTask = {},
modifier = Modifier.fillMaxSize()
)
}

@Preview(showBackground = true, widthDp = 360, heightDp = 720)
@Composable
fun TasksScreenContentVerticalPreview() {
TasksScreenContentPreview(DpSize(360.dp, 720.dp))
}

@Preview(showBackground = true, widthDp = 720, heightDp = 360)
@Composable
fun TasksScreenContentHorizontalPreview() {
TasksScreenContentPreview(DpSize(720.dp, 360.dp))
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.braiso_22.cozycave.feature_task.domain.use_case.DeleteTasksUseCase
import com.braiso_22.cozycave.feature_task.domain.use_case.DeleteTaskUseCase
import com.braiso_22.cozycave.feature_task.domain.use_case.GetTasksUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
Expand All @@ -15,7 +15,7 @@ import javax.inject.Inject
@HiltViewModel
class TasksViewModel @Inject constructor(
private val getTasksUseCase: GetTasksUseCase,
deleteTasksUseCase: DeleteTasksUseCase,
deleteTasksUseCase: DeleteTaskUseCase,
) : ViewModel() {
private val _state = mutableStateOf(TasksUiState())
val state: State<TasksUiState> = _state
Expand All @@ -27,9 +27,9 @@ class TasksViewModel @Inject constructor(

private fun updateState() {
updateStateJob?.cancel()
updateStateJob = getTasksUseCase().onEach { tasks ->
updateStateJob = getTasksUseCase().onEach { taskList ->
_state.value = state.value.copy(
tasks = tasks.map {
tasks = taskList.map {
TaskUiState(
name = it.name,
description = it.description,
Expand Down

0 comments on commit 7f03f04

Please sign in to comment.