Skip to content

Commit 2520146

Browse files
committed
feat: Add functionality to create and edit shortcuts with improved dialog handling
1 parent 1ba023e commit 2520146

File tree

3 files changed

+110
-42
lines changed

3 files changed

+110
-42
lines changed

app/src/main/java/com/yogeshpaliyal/deepr/ui/components/CreateShortcutDialog.kt

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,49 @@ import androidx.compose.runtime.getValue
99
import androidx.compose.runtime.mutableStateOf
1010
import androidx.compose.runtime.remember
1111
import androidx.compose.runtime.setValue
12+
import androidx.compose.ui.platform.LocalContext
1213
import com.yogeshpaliyal.deepr.Deepr
14+
import com.yogeshpaliyal.deepr.util.createShortcut
15+
import com.yogeshpaliyal.deepr.util.getShortcut
16+
import com.yogeshpaliyal.deepr.util.isShortcutSupported
1317

1418
@Composable
1519
fun CreateShortcutDialog(deepr: Deepr, onDismiss: () -> Unit, onCreate: (Deepr, String) -> Unit) {
16-
var shortcutName by remember { mutableStateOf("") }
17-
AlertDialog(
18-
onDismissRequest = onDismiss,
19-
title = { Text("Create Shortcut") },
20-
text = {
21-
TextField(
22-
value = shortcutName,
23-
onValueChange = { shortcutName = it },
24-
label = { Text("Shortcut Name") },
25-
placeholder = { Text(text = deepr.link) }
20+
val context = LocalContext.current
21+
val existingShortcut = getShortcut(context, deepr.id)
22+
if (isShortcutSupported(context)) {
23+
var shortcutName by remember {
24+
mutableStateOf(
25+
existingShortcut?.shortLabel?.toString() ?: ""
2626
)
27-
},
28-
confirmButton = {
29-
Button(
30-
onClick = { onCreate(deepr, shortcutName) },
31-
enabled = shortcutName.isNotBlank()
32-
) {
33-
Text("Create")
34-
}
35-
},
36-
dismissButton = {
37-
Button(onClick = onDismiss) {
38-
Text("Cancel")
39-
}
4027
}
41-
)
28+
AlertDialog(
29+
onDismissRequest = onDismiss,
30+
title = { Text("${if (existingShortcut == null) "Create" else "Edit"} Shortcut") },
31+
text = {
32+
TextField(
33+
value = shortcutName,
34+
onValueChange = { shortcutName = it },
35+
label = { Text("Shortcut Name") },
36+
placeholder = { Text(text = deepr.link) }
37+
)
38+
},
39+
confirmButton = {
40+
Button(
41+
onClick = {
42+
onCreate(deepr, shortcutName)
43+
createShortcut(context, deepr, shortcutName, existingShortcut != null)
44+
},
45+
enabled = shortcutName.isNotBlank()
46+
) {
47+
Text(if (existingShortcut == null) "Create" else "Edit")
48+
}
49+
},
50+
dismissButton = {
51+
Button(onClick = onDismiss) {
52+
Text("Cancel")
53+
}
54+
}
55+
)
56+
}
4257
}

app/src/main/java/com/yogeshpaliyal/deepr/ui/screens/Home.kt

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ import androidx.compose.ui.unit.dp
5353
import com.yogeshpaliyal.deepr.Deepr
5454
import com.yogeshpaliyal.deepr.ui.components.CreateShortcutDialog
5555
import com.yogeshpaliyal.deepr.ui.components.EditDeeplinkDialog
56-
import com.yogeshpaliyal.deepr.util.createShortcut
56+
import com.yogeshpaliyal.deepr.util.hasShortcut
57+
import com.yogeshpaliyal.deepr.util.isShortcutSupported
5758
import com.yogeshpaliyal.deepr.util.isValidDeeplink
5859
import com.yogeshpaliyal.deepr.util.openDeeplink
5960
import compose.icons.TablerIcons
@@ -200,7 +201,6 @@ fun Content(viewModel: AccountViewModel) {
200201
deepr = deepr,
201202
onDismiss = { showShortcutDialog = null },
202203
onCreate = { d, name ->
203-
createShortcut(context, d, name)
204204
showShortcutDialog = null
205205
}
206206
)
@@ -382,6 +382,28 @@ fun DeeprList(
382382
}
383383
}
384384

385+
386+
@Composable
387+
fun ShortcutMenuItem(account: Deepr, onShortcutClick: (Deepr) -> Unit) {
388+
val context = LocalContext.current
389+
val shortcutExists = remember(account.id) { hasShortcut(context, account.id) }
390+
391+
if (isShortcutSupported(LocalContext.current)) {
392+
DropdownMenuItem(
393+
text = { Text(if (shortcutExists) "Edit shortcut" else "Add shortcut") },
394+
onClick = {
395+
onShortcutClick(account)
396+
},
397+
leadingIcon = {
398+
Icon(
399+
TablerIcons.Plus,
400+
contentDescription = if (shortcutExists) "Edit shortcut" else "Add shortcut"
401+
)
402+
}
403+
)
404+
}
405+
}
406+
385407
@OptIn(androidx.compose.foundation.ExperimentalFoundationApi::class)
386408
@Composable
387409
fun DeeprItem(
@@ -463,19 +485,10 @@ fun DeeprItem(
463485
)
464486
}
465487
)
466-
DropdownMenuItem(
467-
text = { Text("Add shortcut") },
468-
onClick = {
469-
onShortcutClick(account)
470-
expanded = false
471-
},
472-
leadingIcon = {
473-
Icon(
474-
TablerIcons.Plus,
475-
contentDescription = "Add shortcut"
476-
)
477-
}
478-
)
488+
ShortcutMenuItem(account) {
489+
onShortcutClick(it)
490+
expanded = false
491+
}
479492
DropdownMenuItem(
480493
text = { Text("Edit") },
481494
onClick = {

app/src/main/java/com/yogeshpaliyal/deepr/util/ShortcutUtils.kt

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package com.yogeshpaliyal.deepr.util
22

33
import android.content.Context
44
import android.content.Intent
5+
import android.content.pm.ShortcutInfo
6+
import android.os.Build
57
import androidx.core.content.pm.ShortcutInfoCompat
68
import androidx.core.content.pm.ShortcutManagerCompat
79
import androidx.core.graphics.drawable.IconCompat
810
import androidx.core.net.toUri
911
import com.yogeshpaliyal.deepr.Deepr
1012
import com.yogeshpaliyal.deepr.R
1113

12-
fun createShortcut(context: Context, deepr: Deepr, shortcutName: String) {
13-
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
14+
fun createShortcut(context: Context, deepr: Deepr, shortcutName: String, alreadyExists: Boolean) {
15+
if (isShortcutSupported(context)) {
1416
val shortcutInfo = ShortcutInfoCompat.Builder(context, "deepr_${deepr.id}")
1517
.setShortLabel(shortcutName)
1618
.setLongLabel(shortcutName)
@@ -19,7 +21,45 @@ fun createShortcut(context: Context, deepr: Deepr, shortcutName: String) {
1921

2022
})
2123
.build()
24+
if (alreadyExists) {
25+
// If the shortcut already exists, we update it
26+
ShortcutManagerCompat.updateShortcuts(context, listOf(shortcutInfo))
27+
} else {
28+
// Otherwise, we request to pin the new shortcut
29+
ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null)
30+
}
31+
}
32+
}
33+
34+
fun isShortcutSupported(context: Context): Boolean {
35+
// Check if the device supports pinned shortcuts
36+
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
37+
ShortcutManagerCompat.isRequestPinShortcutSupported(context)
38+
}
39+
40+
/**
41+
* Checks if a shortcut already exists for the given deeplink
42+
*/
43+
fun getShortcut(context: Context, deeprId: Long): ShortcutInfo? {
44+
// On Android 10+ (API 29+), we can check for pinned shortcuts
45+
if (isShortcutSupported(context)) {
46+
val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as android.content.pm.ShortcutManager?
2247

23-
ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null)
48+
// Get all pinned shortcuts if available
49+
val shortcuts = shortcutManager?.pinnedShortcuts
50+
51+
// Check if our shortcut exists
52+
return shortcuts?.find { it.id == "deepr_$deeprId" }
2453
}
54+
55+
// For older Android versions, we can't reliably check if a shortcut exists
56+
// So we'll return false by default
57+
return null
58+
}
59+
60+
/**
61+
* Checks if a shortcut already exists for the given deeplink
62+
*/
63+
fun hasShortcut(context: Context, deeprId: Long): Boolean {
64+
return getShortcut(context, deeprId) != null
2565
}

0 commit comments

Comments
 (0)