Skip to content

Commit

Permalink
Add delete all drafts button
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jul 31, 2024
1 parent 4bae04a commit 609ccaa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,10 @@ class AccountViewModel(
}
}

fun delete(notes: List<Note>) {
viewModelScope.launch(Dispatchers.IO) { account.delete(notes) }
}

fun delete(note: Note) {
viewModelScope.launch(Dispatchers.IO) { account.delete(note) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,32 @@ package com.vitorpamplona.amethyst.ui.screen.loggedIn
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.components.SwipeToDeleteContainer
import com.vitorpamplona.amethyst.ui.note.NoteCompose
import com.vitorpamplona.amethyst.ui.screen.FeedState
Expand Down Expand Up @@ -114,17 +125,73 @@ private fun DraftFeedLoaded(
accountViewModel: AccountViewModel,
nav: (String) -> Unit,
) {
var showDeleteDialog by remember { mutableStateOf(false) }

if (showDeleteDialog) {
AlertDialog(
onDismissRequest = {
showDeleteDialog = false
},
title = {
Text(text = stringResource(R.string.drafts))
},
text = {
Text(text = stringResource(R.string.delete_all_drafts_confirmation))
},
confirmButton = {
TextButton(
onClick = {
accountViewModel.delete(state.feed.value)
showDeleteDialog = false
},
) {
Text(text = stringResource(R.string.yes))
}
},
dismissButton = {
TextButton(
onClick = {
showDeleteDialog = false
},
) {
Text(text = stringResource(R.string.no))
}
},
)
}

LazyColumn(
contentPadding = FeedPadding,
state = listState,
) {
stickyHeader {
Row(
Modifier
.fillMaxWidth(),
Arrangement.Center,
) {
ElevatedButton(
onClick = { showDeleteDialog = true },
) {
Text(stringResource(R.string.delete_all))
}
}
}
itemsIndexed(state.feed.value, key = { _, item -> item.idHex }) { _, item ->
SwipeToDeleteContainer(
modifier = Modifier.fillMaxWidth().animateContentSize(),
modifier =
Modifier
.fillMaxWidth()
.animateContentSize(),
onStartToEnd = { accountViewModel.delete(item) },
onEndToStart = { accountViewModel.delete(item) },
) {
Row(Modifier.fillMaxWidth().animateItemPlacement().background(MaterialTheme.colorScheme.background)) {
Row(
Modifier
.fillMaxWidth()
.animateItemPlacement()
.background(MaterialTheme.colorScheme.background),
) {
NoteCompose(
item,
routeForLastRead = routeForLastRead,
Expand Down
2 changes: 2 additions & 0 deletions amethyst/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -949,4 +949,6 @@
<string name="http_status_511">Network Authentication Required - The client must be authenticated to access the network</string>

<string name="add_a_nip96_server">Add a NIP-96 Server</string>
<string name="delete_all">Delete all</string>
<string name="delete_all_drafts_confirmation">Are you sure you want to delete all drafts?</string>
</resources>

0 comments on commit 609ccaa

Please sign in to comment.