From 59c250f5058df1967ae3bc678f78d0a031d653ec Mon Sep 17 00:00:00 2001
From: MM20 <15646950+MM2-0@users.noreply.github.com>
Date: Fri, 5 Aug 2022 13:47:00 +0200
Subject: [PATCH] Show confirmation dialog before deleting a shortcut
---
i18n/src/main/res/values/strings.xml | 2 ++
.../launcher/search/shortcut/ShortcutItem.kt | 29 ++++++++++++++++---
.../search/shortcut/ShortcutItemVM.kt | 1 +
3 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/i18n/src/main/res/values/strings.xml b/i18n/src/main/res/values/strings.xml
index 30163897..5b46b753 100644
--- a/i18n/src/main/res/values/strings.xml
+++ b/i18n/src/main/res/values/strings.xml
@@ -149,6 +149,8 @@
The directory %1$s and all its content will be deleted permanently. Proceed\?
The file %1$s will be deleted permanently. Proceed\?
+
+ The shortcut %1$s will be removed permanently. Proceed\?
Couldn\'t open %1$s
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/shortcut/ShortcutItem.kt b/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/shortcut/ShortcutItem.kt
index 76bdd3dd..e7697636 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/shortcut/ShortcutItem.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/shortcut/ShortcutItem.kt
@@ -7,9 +7,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
-import androidx.compose.material3.MaterialTheme
-import androidx.compose.material3.SnackbarResult
-import androidx.compose.material3.Text
+import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
@@ -51,6 +49,7 @@ fun AppShortcutItem(
val lifecycleOwner = LocalLifecycleOwner.current
val snackbarHostState = LocalSnackbarHostState.current
+ var requestDelete by remember { mutableStateOf(false) }
var edit by remember { mutableStateOf(false) }
val transition = updateTransition(showDetails, label = "AppShortcutItem")
@@ -145,7 +144,7 @@ fun AppShortcutItem(
toolbarActions.add(DefaultToolbarAction(
label = stringResource(R.string.menu_delete),
icon = Icons.Rounded.Delete,
- action = { viewModel.deleteShortcut() }
+ action = { requestDelete = true }
))
}
@@ -195,6 +194,28 @@ fun AppShortcutItem(
}
}
+ if (requestDelete) {
+ AlertDialog(
+ onDismissRequest = { requestDelete = false },
+ text = { Text(stringResource(R.string.alert_delete_shortcut, shortcut.label)) },
+ confirmButton = {
+ TextButton(onClick = {
+ viewModel.deleteShortcut()
+ requestDelete = false
+ }) {
+ Text(stringResource(android.R.string.ok))
+ }
+ },
+ dismissButton = {
+ TextButton(onClick = {
+ requestDelete = false
+ }) {
+ Text(stringResource(android.R.string.cancel))
+ }
+ }
+ )
+ }
+
if (edit) {
CustomizeSearchableSheet(
searchable = shortcut,
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/shortcut/ShortcutItemVM.kt b/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/shortcut/ShortcutItemVM.kt
index d1fa2293..600c0261 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/shortcut/ShortcutItemVM.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/shortcut/ShortcutItemVM.kt
@@ -28,6 +28,7 @@ class ShortcutItemVM(private val shortcut: AppShortcut) : SearchableItemVM(short
}
fun deleteShortcut() {
+ if (!canDelete) return
shortcutRepository.removePinnedShortcut(shortcut)
favoritesRepository.unpinItem(shortcut)
}