Skip to content

Commit

Permalink
Add delete function for list
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-powell committed Dec 14, 2023
1 parent 234ed5c commit 87dea48
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 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 @@ -86,6 +86,7 @@ public class EditListPresenter(
newItemText = ""
}
is EditListViewEvent.UpdateNewItemText -> newItemText = event.text
EditListViewEvent.DeleteList -> Unit
}

}
Expand Down Expand Up @@ -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)
}
}
}
}
}
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,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;
UPDATE TidyListItem SET text = :text, completed = :completed, updated_at = CURRENT_TIMESTAMP WHERE id = :id;

deleteListItems:
DELETE FROM TidyListItem WHERE list_id = :id;

0 comments on commit 87dea48

Please sign in to comment.