Skip to content

Commit

Permalink
Add loading for delete folder alert
Browse files Browse the repository at this point in the history
  • Loading branch information
DianQK committed Jan 29, 2023
1 parent 9fa5170 commit f2aeb54
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
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 @@ -16,6 +18,7 @@ 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 @@ -25,6 +28,7 @@ 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
Expand All @@ -44,7 +48,7 @@ fun NotesDrawerSheet(
openCreateFolderDialog: Boolean,
onCreateFolder: (String) -> Unit,
onRenameFolder: (FfiFolder) -> Unit,
onDeleteFolder: (FfiFolder) -> Unit,
onDeleteFolder: suspend (FfiFolder) -> Unit,
onChangeOpenCreateFolderDialogVisible: (Boolean) -> Unit,
onShowSettingsPage: () -> Unit
) {
Expand All @@ -55,6 +59,7 @@ fun NotesDrawerSheet(
var openDeleteFolderAlertDialog: Folder? by remember {
mutableStateOf(null)
}
val scope = rememberCoroutineScope()

openEditFolderDialog?.let { editFolder ->
FolderDialog(
Expand All @@ -75,27 +80,54 @@ 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 = {
openDeleteFolderAlertDialog = null
if (!isDeleting) {
openDeleteFolderAlertDialog = null
}
},
confirmButton = {
TextButton(onClick = {
onDeleteFolder(deleteFolder.ffiFolder)
openDeleteFolderAlertDialog = null
TextButton(enabled = !isDeleting, onClick = {
isDeleting = true
scope.launch {
onDeleteFolder(deleteFolder.ffiFolder)
isDeleting = false
openDeleteFolderAlertDialog = null
}
}) {
Text(text = stringResource(id = R.string.confirm))
}
},
dismissButton = {
TextButton(onClick = {
TextButton(enabled = !isDeleting, onClick = {
openDeleteFolderAlertDialog = null
}) {
Text(text = stringResource(id = R.string.cancel))
}
},
icon = {
Icon(imageVector = Icons.Default.Warning, contentDescription = null)
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
)
}
},
title = {
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ fun NotesPage(
tint = MaterialTheme.colorScheme.error
)
}

},
title = {
Text(
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

0 comments on commit f2aeb54

Please sign in to comment.