From e360cf52fd6ebe56f6a1c6edcb640aa9547c2778 Mon Sep 17 00:00:00 2001 From: Geoff Powell Date: Wed, 13 Dec 2023 20:11:11 -0500 Subject: [PATCH] Merge branch 'add-list-delete' of github.com:geoff-powell/tidy into add-list-delete * 'add-list-delete' of github.com:geoff-powell/tidy: Add delete function for list Cleanup edit list --- .../greenmiststudios/common/persistance/Databases.kt | 12 +++++++++++- .../common/presenters/EditListPresenter.kt | 8 +++++++- .../sqldelight/com/greenmiststudios/tidy/TidyList.sq | 5 ++++- 3 files changed, 22 insertions(+), 3 deletions(-) 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 028bc1f..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) } } @@ -95,6 +100,7 @@ public class EditListPresenter( return EditListViewModel.Loaded.Add( title = "Add List", name = tidyList.name, + newItemText = newItemText, items = tidyList.items, ) } diff --git a/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq b/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq index 20550e8..8aeb65c 100644 --- a/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq +++ b/common/src/commonMain/sqldelight/com/greenmiststudios/tidy/TidyList.sq @@ -53,4 +53,7 @@ updateListItem: 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 +DELETE FROM TidyListItem WHERE list_id = :id; + +deleteListItemById: +DELETE FROM TidyListItem WHERE id = :id; \ No newline at end of file