Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loading for delete folder alert #6

Merged
merged 2 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.dianqk.ruslin.ui.component

import androidx.compose.animation.core.*
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.stringResource
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.dianqk.ruslin.R

@Composable
fun SuspendConfirmAlertDialog(
scope: CoroutineScope = rememberCoroutineScope(),
icon: @Composable (() -> Unit)? = null,
inProgressIcon: @Composable ((Modifier) -> Unit)? = null,
title: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
onDismissRequest: () -> Unit,
onConfirm: suspend () -> Unit,
onConfirmFinished: () -> Unit,
) {
var inProgress by remember { mutableStateOf(false) }
val inProgressAnimation by rememberInfiniteTransition().animateFloat(
initialValue = 1f,
targetValue = 0f,
animationSpec = infiniteRepeatable(
animation = tween(300, easing = LinearEasing)
)
)
AlertDialog(
onDismissRequest = {
if (!inProgress) {
onDismissRequest()
}
},
confirmButton = {
TextButton(enabled = !inProgress, onClick = {
inProgress = true
scope.launch {
onConfirm()
inProgress = false
onConfirmFinished()
}
}) {
Text(text = stringResource(id = R.string.confirm))
}
},
dismissButton = {
TextButton(enabled = !inProgress, onClick = {
onDismissRequest()
}) {
Text(text = stringResource(id = R.string.cancel))
}
},
icon = {
if (inProgress) {
if (inProgressIcon != null) {
inProgressIcon(Modifier.alpha(inProgressAnimation))
}
} else {
if (icon != null) {
icon()
}
}
},
title = title,
text = text
)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.dianqk.ruslin.ui.page.notes

import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.*
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CreateNewFolder
import androidx.compose.material.icons.filled.DeleteSweep
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material.icons.outlined.*
Expand All @@ -29,6 +31,7 @@ import org.dianqk.ruslin.R
import org.dianqk.ruslin.ui.component.CombinedClickableSurface
import org.dianqk.ruslin.ui.component.OutlinedButtonWithIcon
import org.dianqk.ruslin.ui.component.SubTitle
import org.dianqk.ruslin.ui.component.SuspendConfirmAlertDialog
import org.dianqk.ruslin.ui.theme.Shape32
import uniffi.ruslin.FfiFolder

Expand All @@ -44,7 +47,7 @@ fun NotesDrawerSheet(
openCreateFolderDialog: Boolean,
onCreateFolder: (String) -> Unit,
onRenameFolder: (FfiFolder) -> Unit,
onDeleteFolder: (FfiFolder) -> Unit,
onDeleteFolder: suspend (FfiFolder) -> Unit,
onChangeOpenCreateFolderDialogVisible: (Boolean) -> Unit,
onShowSettingsPage: () -> Unit
) {
Expand Down Expand Up @@ -75,27 +78,22 @@ fun NotesDrawerSheet(
}

openDeleteFolderAlertDialog?.let { deleteFolder ->
AlertDialog(
onDismissRequest = {
openDeleteFolderAlertDialog = null
},
confirmButton = {
TextButton(onClick = {
onDeleteFolder(deleteFolder.ffiFolder)
openDeleteFolderAlertDialog = null
}) {
Text(text = stringResource(id = R.string.confirm))
}
},
dismissButton = {
TextButton(onClick = {
openDeleteFolderAlertDialog = null
}) {
Text(text = stringResource(id = R.string.cancel))
}
SuspendConfirmAlertDialog(
onDismissRequest = { openDeleteFolderAlertDialog = null },
inProgressIcon = {
Icon(
modifier = it,
imageVector = Icons.Default.DeleteSweep,
contentDescription = null,
tint = MaterialTheme.colorScheme.tertiary
)
},
icon = {
Icon(imageVector = Icons.Default.Warning, contentDescription = null)
Icon(
imageVector = Icons.Default.Warning,
contentDescription = null,
tint = MaterialTheme.colorScheme.error
)
},
title = {
Text(
Expand All @@ -107,7 +105,12 @@ fun NotesDrawerSheet(
},
text = {
Text(text = stringResource(id = R.string.ask_delete_folder_description))
})
},
onConfirm = {
onDeleteFolder(deleteFolder.ffiFolder)
}) {
openDeleteFolderAlertDialog = null
}
}

if (openCreateFolderDialog) {
Expand Down
80 changes: 25 additions & 55 deletions app/src/main/java/org/dianqk/ruslin/ui/page/notes/NotesPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material.icons.outlined.DeleteForever
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
Expand All @@ -24,6 +22,7 @@ import kotlinx.coroutines.launch
import org.dianqk.ruslin.R
import org.dianqk.ruslin.ui.component.ContentEmptyState
import org.dianqk.ruslin.ui.component.ContentLoadingState
import org.dianqk.ruslin.ui.component.SuspendConfirmAlertDialog

@OptIn(
ExperimentalMaterial3Api::class,
Expand Down Expand Up @@ -221,68 +220,39 @@ fun NotesPage(
)

if (showRemoveMultipleItemsDialog) {
val deletingAnimation by rememberInfiniteTransition().animateFloat(
initialValue = 1f,
targetValue = 0f,
animationSpec = infiniteRepeatable(
animation = tween(300, easing = LinearEasing)
)
)
var isDeleting by remember { mutableStateOf(false) }

AlertDialog(
onDismissRequest = {
if (!isDeleting) {
showRemoveMultipleItemsDialog = false
}
},
confirmButton = {
TextButton(enabled = !isDeleting, onClick = {
isDeleting = true
scope.launch {
viewModel.deleteNotes(selectedItemIds)
.onFailure { e ->
Log.d(TAG, "$e")
}
isDeleting = false
firstSelectedItemId = null
showRemoveMultipleItemsDialog = false
}
}) {
Text(text = stringResource(id = R.string.confirm))
}
},
dismissButton = {
TextButton(enabled = !isDeleting, onClick = {
showRemoveMultipleItemsDialog = false
}) {
Text(text = stringResource(id = R.string.cancel))
}
SuspendConfirmAlertDialog(
onDismissRequest = { showRemoveMultipleItemsDialog = false },
inProgressIcon = {
Icon(
modifier = it,
imageVector = Icons.Default.DeleteSweep,
contentDescription = null,
tint = MaterialTheme.colorScheme.tertiary
)
},
icon = {
if (isDeleting) {
Icon(
modifier = Modifier.alpha(deletingAnimation),
imageVector = Icons.Default.DeleteSweep,
contentDescription = null,
tint = MaterialTheme.colorScheme.tertiary
)
} else {
Icon(
imageVector = Icons.Default.Warning,
contentDescription = null,
tint = MaterialTheme.colorScheme.error
)
}

Icon(
imageVector = Icons.Default.Warning,
contentDescription = null,
tint = MaterialTheme.colorScheme.error
)
},
title = {
Text(
text = stringResource(
id = R.string.ask_delete_selected_notes
)
)
})
},
onConfirm = {
viewModel.deleteNotes(selectedItemIds)
.onFailure { e ->
Log.d(TAG, "$e")
}
}) {
firstSelectedItemId = null
showRemoveMultipleItemsDialog = false
}
}

LaunchedEffect(Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,9 @@ class NotesViewModel @Inject constructor(
}
}

fun deleteFolder(folder: FfiFolder) {
viewModelScope.launch {
notesRepository.deleteFolder(folder.id)
loadFoldersFromRepo()
}
suspend fun deleteFolder(folder: FfiFolder) {
notesRepository.deleteFolder(folder.id)
loadFoldersFromRepo()
}

fun createFolder(title: String) {
Expand Down