Use ShortcutManagerCompat.setDynamicShortcuts().

This commit is contained in:
Isira Seneviratne 2023-03-01 05:33:12 +05:30
parent f9ab38add3
commit d4f18799ea

View File

@ -16,30 +16,25 @@ object ShortcutHelper {
AppShortcut("trends", R.string.trends, R.drawable.ic_trending), AppShortcut("trends", R.string.trends, R.drawable.ic_trending),
AppShortcut("subscriptions", R.string.subscriptions, R.drawable.ic_subscriptions), AppShortcut("subscriptions", R.string.subscriptions, R.drawable.ic_subscriptions),
AppShortcut("library", R.string.library, R.drawable.ic_library) AppShortcut("library", R.string.library, R.drawable.ic_library)
).reversed() )
private fun createShortcut(context: Context, action: String, label: String, icon: IconCompat) { private fun createShortcut(context: Context, appShortcut: AppShortcut): ShortcutInfoCompat {
val shortcut = ShortcutInfoCompat.Builder(context, action) val label = context.getString(appShortcut.label)
return ShortcutInfoCompat.Builder(context, appShortcut.action)
.setShortLabel(label) .setShortLabel(label)
.setLongLabel(label) .setLongLabel(label)
.setIcon(icon) .setIcon(IconCompat.createWithResource(context, appShortcut.drawable))
.setIntent( .setIntent(
Intent(context, MainActivity::class.java).apply { Intent(Intent.ACTION_VIEW, null, context, MainActivity::class.java)
this.action = Intent.ACTION_VIEW .putExtra(IntentData.fragmentToOpen, appShortcut.action)
putExtra(IntentData.fragmentToOpen, action)
}
) )
.build() .build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
} }
fun createShortcuts(context: Context) { fun createShortcuts(context: Context) {
ShortcutManagerCompat.getDynamicShortcuts(context).takeIf { it.isEmpty() } ?: return if (ShortcutManagerCompat.getDynamicShortcuts(context).isEmpty()) {
val dynamicShortcuts = shortcuts.map { createShortcut(context, it) }
shortcuts.forEach { ShortcutManagerCompat.setDynamicShortcuts(context, dynamicShortcuts)
val icon = IconCompat.createWithResource(context, it.drawable)
createShortcut(context, it.action, context.getString(it.label), icon)
} }
} }
} }