Skip to content

Commit

Permalink
Add delete function for list (#31)
Browse files Browse the repository at this point in the history
* Cleanup edit list

* Add delete function for list

* 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
  • Loading branch information
geoff-powell authored Dec 15, 2023
1 parent 7c5d60c commit 7afc0b8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -52,13 +54,33 @@ public data class EditListScreen(override val params: Config) : Screen<EditListS
}
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
public fun EditListScreen(viewModel: EditListViewModel, onEvent: (EditListViewEvent) -> 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)) {
Expand All @@ -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")
}
}
}
}
Expand All @@ -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)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -86,6 +91,7 @@ public class EditListPresenter(
newItemText = ""
}
is EditListViewEvent.UpdateNewItemText -> newItemText = event.text
EditListViewEvent.DeleteList -> Unit
}

}
Expand All @@ -94,6 +100,7 @@ public class EditListPresenter(
return EditListViewModel.Loaded.Add(
title = "Add List",
name = tidyList.name,
newItemText = newItemText,
items = tidyList.items,
)
}
Expand Down Expand Up @@ -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)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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;

0 comments on commit 7afc0b8

Please sign in to comment.