#22 boilerplate for task screen

This commit is contained in:
brreynie 2023-05-02 21:39:21 +02:00
parent 7ad1047091
commit 1a2e192227
10 changed files with 200 additions and 81 deletions

View file

@ -0,0 +1,85 @@
package be.ugent.sel.studeez.screens.tasks
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import be.ugent.sel.studeez.R
import be.ugent.sel.studeez.common.composable.NewTaskSubjectButton
import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate
import be.ugent.sel.studeez.common.composable.SubjectEntry
import be.ugent.sel.studeez.common.composable.drawer.DrawerActions
import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions
import be.ugent.sel.studeez.data.local.models.task.Subject
import be.ugent.sel.studeez.resources
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
@Composable
fun SubjectRoute(
open: (String) -> Unit,
viewModel: SubjectViewModel,
drawerActions: DrawerActions,
navigationBarActions: NavigationBarActions,
) {
SubjectScreen(
drawerActions = drawerActions,
navigationBarActions = navigationBarActions,
addSubject = { viewModel.addSubject() },
) { viewModel.getSubjects() }
}
@Composable
fun SubjectScreen(
drawerActions: DrawerActions,
navigationBarActions: NavigationBarActions,
addSubject: () -> Unit,
getSubjects: () -> Flow<List<Subject>>,
) {
PrimaryScreenTemplate(
title = resources().getString(R.string.tasks),
drawerActions = drawerActions,
navigationBarActions = navigationBarActions,
barAction = {},
) {
val subjects = getSubjects().collectAsState(initial = emptyList())
Column(
modifier = Modifier.padding(top = 5.dp)
) {
LazyColumn {
// if (subjects.value.isNotEmpty()) {
// item {
// SubjectEntry(subject = subjects.value[0])
// }
// }
// if (subjects.value.size > 1) {
// items(subjects.value.subList(1, subjects.value.lastIndex + 1)) {
// Column {
// Divider(modifier = Modifier.padding(10.dp, 0.dp))
// SubjectEntry(subject = it)
// }
// }
// }
items(subjects.value) {
SubjectEntry(subject = it)
}
}
NewTaskSubjectButton(onClick = addSubject, R.string.new_subject)
}
}
}
@Preview
@Composable
fun SubjectScreenPreview() {
SubjectScreen(
drawerActions = DrawerActions({}, {}, {}, {}, {}),
navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}),
{},
) { flowOf() }
}

View file

@ -0,0 +1,30 @@
package be.ugent.sel.studeez.screens.tasks
import be.ugent.sel.studeez.data.local.models.task.Subject
import be.ugent.sel.studeez.domain.LogService
import be.ugent.sel.studeez.domain.SubjectDAO
import be.ugent.sel.studeez.screens.StudeezViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
@HiltViewModel
class SubjectViewModel @Inject constructor(
private val subjectDAO: SubjectDAO,
logService: LogService,
) : StudeezViewModel(logService) {
fun addSubject() {
subjectDAO.saveSubject(
Subject(
name = "Test Subject",
tasks = listOf(),
time = 0,
argb_color = 0xFFF44336,
)
)
}
fun getSubjects(): Flow<List<Subject>> {
return subjectDAO.getSubjects()
}
}

View file

@ -1,86 +1,34 @@
package be.ugent.sel.studeez.screens.tasks
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Divider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import be.ugent.sel.studeez.R
import be.ugent.sel.studeez.common.composable.NewTaskSubjectButton
import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate
import be.ugent.sel.studeez.common.composable.SubjectEntry
import be.ugent.sel.studeez.common.composable.drawer.DrawerActions
import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions
import be.ugent.sel.studeez.data.local.models.task.Subject
import be.ugent.sel.studeez.resources
import be.ugent.sel.studeez.data.local.models.task.Task
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
@Composable
fun TaskRoute(
open: (String) -> Unit,
viewModel: TaskViewModel,
drawerActions: DrawerActions,
navigationBarActions: NavigationBarActions,
) {
TaskScreen(
drawerActions = drawerActions,
navigationBarActions = navigationBarActions,
addSubject = { viewModel.addSubject() },
) { viewModel.getSubjects() }
addTask = viewModel::addTask,
getTasks = viewModel::getTasks,
)
}
@Composable
fun TaskScreen(
drawerActions: DrawerActions,
navigationBarActions: NavigationBarActions,
addSubject: () -> Unit,
getSubjects: () -> Flow<List<Subject>>,
addTask: () -> Unit,
getTasks: () -> Flow<List<Task>>,
) {
PrimaryScreenTemplate(
title = resources().getString(R.string.tasks),
drawerActions = drawerActions,
navigationBarActions = navigationBarActions,
barAction = {},
) {
val subjects = getSubjects().collectAsState(initial = emptyList())
Column(
modifier = Modifier.padding(top = 5.dp)
) {
LazyColumn {
// if (subjects.value.isNotEmpty()) {
// item {
// SubjectEntry(subject = subjects.value[0])
// }
// }
// if (subjects.value.size > 1) {
// items(subjects.value.subList(1, subjects.value.lastIndex + 1)) {
// Column {
// Divider(modifier = Modifier.padding(10.dp, 0.dp))
// SubjectEntry(subject = it)
// }
// }
// }
items(subjects.value) {
SubjectEntry(subject = it)
}
}
NewTaskSubjectButton(onClick = addSubject, R.string.new_subject)
}
}
}
@Preview
@Composable
fun TaskScreenPreview() {
TaskScreen(
drawerActions = DrawerActions({}, {}, {}, {}, {}),
navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}),
{},
) { flowOf() }
addTask = {},
getTasks = { flowOf() }
)
}

View file

@ -1,30 +1,33 @@
package be.ugent.sel.studeez.screens.tasks
import be.ugent.sel.studeez.data.local.models.task.Subject
import be.ugent.sel.studeez.data.local.models.task.Task
import be.ugent.sel.studeez.domain.LogService
import be.ugent.sel.studeez.domain.SubjectDAO
import be.ugent.sel.studeez.domain.TaskDAO
import be.ugent.sel.studeez.screens.StudeezViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import javax.inject.Inject
@HiltViewModel
class TaskViewModel @Inject constructor(
private val subjectDAO: SubjectDAO,
private val taskDAO: TaskDAO,
logService: LogService,
) : StudeezViewModel(logService) {
fun addSubject() {
subjectDAO.saveSubject(
Subject(
name = "Test Subject",
tasks = listOf(),
time = 0,
argb_color = 0xFFF44336,
)
)
fun addTask() {
}
fun getSubjects(): Flow<List<Subject>> {
return subjectDAO.getSubjects()
fun getTasks() : Flow<List<Task>> {
return flowOf(listOf(
Task(
name = "Test Task",
completed = false,
),
Task(
name = "Test Task 2",
completed = true,
)
))
}
}