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/persistance/Databases.kt b/common/src/commonMain/kotlin/com/greenmiststudios/common/persistance/Databases.kt index 5f1aa82..c8f0d29 100644 --- a/common/src/commonMain/kotlin/com/greenmiststudios/common/persistance/Databases.kt +++ b/common/src/commonMain/kotlin/com/greenmiststudios/common/persistance/Databases.kt @@ -3,9 +3,19 @@ package com.greenmiststudios.common.persistance import com.greenmiststudios.common.data.TidyList import com.greenmiststudios.tidy.TidyListQueries + +public val USER_ID: String = "USER" + public fun TidyListQueries.updateList(tidyList: TidyList) { updateListName(name = tidyList.name, id = tidyList.id) tidyList.items.forEach { - addOrUpdateListItem(it.id, tidyList.id, it.text, it.completed,"USER") + addOrUpdateListItem(it.id, tidyList.id, it.text, it.completed, USER_ID) + } +} + +public fun TidyListQueries.createList(tidyList: TidyList) { + addList(id = tidyList.id, name = tidyList.name, "", USER_ID) + tidyList.items.forEach { + addOrUpdateListItem(it.id, tidyList.id, it.text, it.completed,USER_ID) } } \ No newline at end of file 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..46f53b5 100644 --- a/common/src/commonMain/kotlin/com/greenmiststudios/common/presenters/EditListPresenter.kt +++ b/common/src/commonMain/kotlin/com/greenmiststudios/common/presenters/EditListPresenter.kt @@ -14,6 +14,7 @@ import com.greenmiststudios.common.components.screens.EditListScreen import com.greenmiststudios.common.data.TidyList import com.greenmiststudios.common.data.TidyListItem import com.greenmiststudios.common.data.asTidyList +import com.greenmiststudios.common.persistance.createList import com.greenmiststudios.common.persistance.updateList import com.greenmiststudios.common.viewmodels.EditListViewEvent import com.greenmiststudios.common.viewmodels.EditListViewModel @@ -67,7 +68,11 @@ public class EditListPresenter( is EditListViewEvent.UpdateList -> { launch(ioContext) { - tidyListQueries.updateList(tidyList) + if (newItemText.isNotEmpty()) { + tidyList = tidyList.copy(items = tidyList.items + TidyListItem(id = uuid4().toString(), text = newItemText)) + newItemText = "" + } + tidyListQueries.createList(tidyList) } } @@ -86,6 +91,7 @@ public class EditListPresenter( newItemText = "" } is EditListViewEvent.UpdateNewItemText -> newItemText = event.text + EditListViewEvent.DeleteList -> Unit } } @@ -94,6 +100,7 @@ public class EditListPresenter( return EditListViewModel.Loaded.Add( title = "Add List", name = tidyList.name, + newItemText = newItemText, items = tidyList.items, ) } @@ -155,6 +162,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..8aeb65c 100644 --- a/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq +++ b/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq @@ -43,8 +43,17 @@ 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; + +deleteListItemById: +DELETE FROM TidyListItem WHERE id = :id; \ No newline at end of file