Skip to content

Commit

Permalink
Merge pull request #31 from opatry/add-task-list-button
Browse files Browse the repository at this point in the history
Add task list button
  • Loading branch information
opatry authored Sep 30, 2024
2 parents 11db8bf + 00b75de commit 6f73a89
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ import org.jetbrains.compose.resources.stringResource

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
actual fun TaskListsMasterDetail(viewModel: TaskListsViewModel) {
actual fun TaskListsMasterDetail(
viewModel: TaskListsViewModel,
onNewTaskList: (String) -> Unit
) {
val taskLists by viewModel.taskLists.collectAsState(emptyList())

// need to store a saveable (Serializable/Parcelable) object
Expand All @@ -63,16 +66,16 @@ actual fun TaskListsMasterDetail(viewModel: TaskListsViewModel) {
listPane = {
AnimatedPane {
if (taskLists.isEmpty()) {
// TODO dialog to ask for the new task list name
val newTaskListName = stringResource(Res.string.default_task_list_title)
NoTaskListEmptyState {
viewModel.createTaskList(newTaskListName)
onNewTaskList(newTaskListName)
}
} else {
Row {
TaskListsColumn(
taskLists,
selectedItem = taskLists.find { it.id == navigator.currentDestination?.content },
onNewTaskList = { onNewTaskList("") },
onItemClick = { taskList ->
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, taskList.id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalWindowInfo
import androidx.compose.ui.unit.dp
import net.opatry.tasks.app.ui.component.EditTextDialog
import net.opatry.tasks.app.ui.component.MissingScreen
import net.opatry.tasks.app.ui.component.ProfileIcon
import net.opatry.tasks.app.ui.screen.TaskListsMasterDetail
Expand Down Expand Up @@ -88,6 +89,9 @@ fun TasksApp(userViewModel: UserViewModel, tasksViewModel: TaskListsViewModel) {
tasksViewModel.enableAutoRefresh(isFocused && isSigned)
}

var newTaskListDefaultTitle by remember { mutableStateOf("") }
var showNewTaskListDialog by remember { mutableStateOf(false) }

NavigationSuiteScaffold(navigationSuiteItems = {
// Only if expanded state
if (false) {
Expand Down Expand Up @@ -134,7 +138,24 @@ fun TasksApp(userViewModel: UserViewModel, tasksViewModel: TaskListsViewModel) {
}
}

TaskListsMasterDetail(tasksViewModel)
TaskListsMasterDetail(tasksViewModel) { title ->
newTaskListDefaultTitle = title
showNewTaskListDialog = true
}

if (showNewTaskListDialog) {
EditTextDialog(
onDismissRequest = { showNewTaskListDialog = false },
validateLabel = "Create",
onValidate = { title ->
showNewTaskListDialog = false
tasksViewModel.createTaskList(title)
},
dialogTitle = "New task list",
initialText = newTaskListDefaultTitle,
allowBlank = false
)
}
}

AppTasksScreen.Calendar -> MissingScreen(stringResource(AppTasksScreen.Calendar.labelRes), LucideIcons.Calendar)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2024 Olivier Patry
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.opatry.tasks.app.ui.component

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialogDefaults
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties

@Composable
fun EditTextDialog(
onDismissRequest: () -> Unit,
validateLabel: String,
onValidate: (String) -> Unit,
dialogTitle: String? = null,
initialText: String = "",
allowBlank: Boolean = true,
) {
Dialog(
onDismissRequest = onDismissRequest,
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false)
) {
var newTitle by remember { mutableStateOf(initialText) }
val hasError by remember(newTitle, allowBlank) {
derivedStateOf {
!allowBlank && newTitle.isBlank()
}
}

Surface(
shape = MaterialTheme.shapes.large,
tonalElevation = AlertDialogDefaults.TonalElevation
) {
Column(Modifier.padding(24.dp), verticalArrangement = Arrangement.spacedBy(16.dp)) {
if (dialogTitle != null) {
Text(dialogTitle, style = MaterialTheme.typography.titleLarge)
}
OutlinedTextField(
newTitle,
onValueChange = { newTitle = it },
maxLines = 1,
supportingText = if (allowBlank) null else {
{
AnimatedVisibility(visible = hasError) {
Text("Text cannot be empty")
}
}
},
isError = hasError,
)
Row(
Modifier.align(Alignment.End),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
TextButton(onClick = onDismissRequest) {
Text("Cancel")
}
Button(
onClick = { onValidate(newTitle) },
enabled = allowBlank || !hasError
) {
Text(validateLabel)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,28 @@

package net.opatry.tasks.app.ui.component

import CircleFadingPlus
import LucideIcons
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -44,16 +54,39 @@ import net.opatry.tasks.app.ui.tooling.TaskfolioPreview
import net.opatry.tasks.app.ui.tooling.TaskfolioThemedPreview


@OptIn(ExperimentalFoundationApi::class)
@Composable
fun TaskListsColumn(
taskLists: List<TaskListUIModel>,
selectedItem: TaskListUIModel? = null,
onNewTaskList: () -> Unit,
onItemClick: (TaskListUIModel) -> Unit
) {
LazyColumn(contentPadding = PaddingValues(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
val listState = rememberLazyListState()
LazyColumn(state = listState, verticalArrangement = Arrangement.spacedBy(8.dp)) {
stickyHeader {
Box(
Modifier
.background(MaterialTheme.colorScheme.background)
.fillMaxWidth()
.padding(horizontal = 8.dp)
) {
// TODO could be a "in-place" replace with a text field (no border)
TextButton(
onClick = onNewTaskList,
) {
RowWithIcon("Add task list…", LucideIcons.CircleFadingPlus)
}
}

AnimatedVisibility(listState.firstVisibleItemScrollOffset > 0) {
HorizontalDivider()
}
}
items(taskLists) { taskList ->
TaskListRow(
taskList,
Modifier.padding(horizontal = 8.dp),
isSelected = taskList.id == selectedItem?.id,
onClick = { onItemClick(taskList) }
)
Expand All @@ -64,6 +97,7 @@ fun TaskListsColumn(
@Composable
fun TaskListRow(
taskList: TaskListUIModel,
modifier: Modifier = Modifier,
isSelected: Boolean = false,
onClick: () -> Unit
) {
Expand All @@ -72,7 +106,7 @@ fun TaskListRow(
else -> Color.Transparent
}

Card(colors = CardDefaults.outlinedCardColors(), onClick = onClick) {
Card(onClick = onClick, modifier = modifier, colors = CardDefaults.outlinedCardColors()) {
ListItem(
headlineContent = {
Text(taskList.title, overflow = TextOverflow.Ellipsis, maxLines = 1)
Expand All @@ -96,8 +130,8 @@ private fun TaskListRowScaffold(
"TODO DATE",
tasks = emptyList(),
),
isSelected,
{}
isSelected = isSelected,
onClick = {}
)
}

Expand Down
Loading

0 comments on commit 6f73a89

Please sign in to comment.