Skip to content

Commit

Permalink
Add SuspendConfirmAlertDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
DianQK committed Jan 29, 2023
1 parent f2aeb54 commit c655a30
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 104 deletions.
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
Expand Up @@ -18,7 +18,6 @@ import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
Expand All @@ -28,11 +27,11 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.launch
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 @@ -59,7 +58,6 @@ fun NotesDrawerSheet(
var openDeleteFolderAlertDialog: Folder? by remember {
mutableStateOf(null)
}
val scope = rememberCoroutineScope()

openEditFolderDialog?.let { editFolder ->
FolderDialog(
Expand All @@ -80,54 +78,22 @@ fun NotesDrawerSheet(
}

openDeleteFolderAlertDialog?.let { deleteFolder ->
var isDeleting by remember { mutableStateOf(false) }
val deletingAnimation by rememberInfiniteTransition().animateFloat(
initialValue = 1f,
targetValue = 0f,
animationSpec = infiniteRepeatable(
animation = tween(300, easing = LinearEasing)
)
)
AlertDialog(
onDismissRequest = {
if (!isDeleting) {
openDeleteFolderAlertDialog = null
}
},
confirmButton = {
TextButton(enabled = !isDeleting, onClick = {
isDeleting = true
scope.launch {
onDeleteFolder(deleteFolder.ffiFolder)
isDeleting = false
openDeleteFolderAlertDialog = null
}
}) {
Text(text = stringResource(id = R.string.confirm))
}
},
dismissButton = {
TextButton(enabled = !isDeleting, 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 = {
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(
Expand All @@ -139,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
79 changes: 25 additions & 54 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,67 +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

0 comments on commit c655a30

Please sign in to comment.