From 87dea484314e4b568545514e26bd6580fcbd93bc Mon Sep 17 00:00:00 2001 From: Geoff Powell Date: Sun, 3 Dec 2023 07:42:45 -0500 Subject: [PATCH] Add delete function for list --- .../components/screens/EditListScreen.kt | 43 ++++++++++++------- .../common/presenters/EditListPresenter.kt | 9 ++++ .../common/viewmodels/EditListViewModel.kt | 1 + .../com/greenmiststudios/tidy/TidyList.sq | 8 +++- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/common/src/commonMain/kotlin/com/greenmiststudios/common/components/screens/EditListScreen.kt b/common/src/commonMain/kotlin/com/greenmiststudios/common/components/screens/EditListScreen.kt index 4eefebf..445ce04 100644 --- a/common/src/commonMain/kotlin/com/greenmiststudios/common/components/screens/EditListScreen.kt +++ b/common/src/commonMain/kotlin/com/greenmiststudios/common/components/screens/EditListScreen.kt @@ -11,9 +11,10 @@ import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Add import androidx.compose.material.icons.filled.ArrowBack +import androidx.compose.material.icons.filled.Delete import androidx.compose.material.icons.rounded.Add -import androidx.compose.material3.Button import androidx.compose.material3.Checkbox import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.Icon @@ -22,19 +23,20 @@ import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment -import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.vector.rememberVectorPainter import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.unit.dp import cafe.adriel.voyager.navigator.LocalNavigator +import com.greenmiststudios.common.components.ActionButton import com.greenmiststudios.common.components.TopAppBarWithContent import com.greenmiststudios.common.presenters.EditListPresenter import com.greenmiststudios.common.screens.Screen import com.greenmiststudios.common.screens.bindPresenter import com.greenmiststudios.common.viewmodels.EditListViewEvent import com.greenmiststudios.common.viewmodels.EditListViewEvent.AddNewItem +import com.greenmiststudios.common.viewmodels.EditListViewEvent.DeleteList import com.greenmiststudios.common.viewmodels.EditListViewEvent.UpdateItemCompletion import com.greenmiststudios.common.viewmodels.EditListViewEvent.UpdateList import com.greenmiststudios.common.viewmodels.EditListViewModel @@ -52,13 +54,33 @@ public data class EditListScreen(override val params: Config) : Screen Unit) { + val navigator = LocalNavigator.current!! TopAppBarWithContent( modifier = Modifier.fillMaxSize(), title = viewModel.title, navigationIcon = rememberVectorPainter(Icons.Default.ArrowBack), + actionButtons = buildList { + if (viewModel is EditListViewModel.Loaded.Edit) { + add( + ActionButton( + action = "Delete", + icon = rememberVectorPainter(Icons.Default.Delete) + ) { + onEvent(DeleteList) + navigator.pop() + } + ) + } + + add( + ActionButton("Save") { + onEvent(UpdateList) + navigator.pop() + } + ) + } ) { Column(Modifier.padding(16.dp).fillMaxSize()) { Column(Modifier.fillMaxWidth().weight(1f)) { @@ -69,24 +91,13 @@ public fun EditListScreen(viewModel: EditListViewModel, onEvent: (EditListViewEv Box(modifier = Modifier.height(128.dp)) { CircularProgressIndicator() } - return@Column + return@TopAppBarWithContent } } require(viewModel is EditListViewModel.Loaded) } - val navigator = LocalNavigator.current!! - Button( - modifier = Modifier - .fillMaxWidth(), - onClick = { - onEvent(UpdateList) - navigator.pop() - } - ) { - Text("Save") - } } } } @@ -106,7 +117,7 @@ private fun CreateListScreen( TextField( modifier = Modifier .fillMaxWidth(), - leadingIcon = { Icon(Icons.Rounded.Add, contentDescription = "Add Item") }, + leadingIcon = { Icon(Icons.Default.Add, contentDescription = "Add Item") }, placeholder = { Text("Add Item") }, value = viewModel.newItemText, onValueChange = { text -> onEvent(EditListViewEvent.UpdateNewItemText(text)) }, diff --git a/common/src/commonMain/kotlin/com/greenmiststudios/common/presenters/EditListPresenter.kt b/common/src/commonMain/kotlin/com/greenmiststudios/common/presenters/EditListPresenter.kt index 0780444..028bc1f 100644 --- a/common/src/commonMain/kotlin/com/greenmiststudios/common/presenters/EditListPresenter.kt +++ b/common/src/commonMain/kotlin/com/greenmiststudios/common/presenters/EditListPresenter.kt @@ -86,6 +86,7 @@ public class EditListPresenter( newItemText = "" } is EditListViewEvent.UpdateNewItemText -> newItemText = event.text + EditListViewEvent.DeleteList -> Unit } } @@ -155,6 +156,14 @@ public class EditListPresenter( newItemText = "" } is EditListViewEvent.UpdateNewItemText -> newItemText = it.text + EditListViewEvent.DeleteList -> { + launch(ioContext) { + val list = requireNotNull(editableTidyList) + // TODO: Add confirmation dialog + tidyListQueries.deleteListItems(list.id) + tidyListQueries.deleteList(list.id) + } + } } } } diff --git a/common/src/commonMain/kotlin/com/greenmiststudios/common/viewmodels/EditListViewModel.kt b/common/src/commonMain/kotlin/com/greenmiststudios/common/viewmodels/EditListViewModel.kt index 1c2689a..333873d 100644 --- a/common/src/commonMain/kotlin/com/greenmiststudios/common/viewmodels/EditListViewModel.kt +++ b/common/src/commonMain/kotlin/com/greenmiststudios/common/viewmodels/EditListViewModel.kt @@ -33,6 +33,7 @@ public sealed interface EditListViewModel { public sealed interface EditListViewEvent { public data object UpdateList : EditListViewEvent + public data object DeleteList : EditListViewEvent public data object AddItem : EditListViewEvent public data class AddNewItem(val text: String) : EditListViewEvent diff --git a/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq b/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq index c169f71..20550e8 100644 --- a/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq +++ b/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq @@ -43,8 +43,14 @@ INSERT INTO TidyList (id, name, description, created_by) VALUES (?, ?, ?, ?); updateListName: UPDATE TidyList SET name = :name, updated_at = CURRENT_TIMESTAMP WHERE id = :id; +deleteList: +DELETE FROM TidyList WHERE id = :id; + addOrUpdateListItem: INSERT OR REPLACE INTO TidyListItem (id, list_id, text, completed, created_by) VALUES (?, ?, ?, ?, ?); updateListItem: -UPDATE TidyListItem SET text = :text, completed = :completed, updated_at = CURRENT_TIMESTAMP WHERE id = :id; \ No newline at end of file +UPDATE TidyListItem SET text = :text, completed = :completed, updated_at = CURRENT_TIMESTAMP WHERE id = :id; + +deleteListItems: +DELETE FROM TidyListItem WHERE list_id = :id; \ No newline at end of file