Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ShortcutManagerCompat.setDynamicShortcuts(). #3211

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions app/src/main/java/com/github/libretube/helpers/ShortcutHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,25 @@ object ShortcutHelper {
AppShortcut("trends", R.string.trends, R.drawable.ic_trending),
AppShortcut("subscriptions", R.string.subscriptions, R.drawable.ic_subscriptions),
AppShortcut("library", R.string.library, R.drawable.ic_library)
).reversed()
)

private fun createShortcut(context: Context, action: String, label: String, icon: IconCompat) {
val shortcut = ShortcutInfoCompat.Builder(context, action)
private fun createShortcut(context: Context, appShortcut: AppShortcut): ShortcutInfoCompat {
val label = context.getString(appShortcut.label)
return ShortcutInfoCompat.Builder(context, appShortcut.action)
.setShortLabel(label)
.setLongLabel(label)
.setIcon(icon)
.setIcon(IconCompat.createWithResource(context, appShortcut.drawable))
.setIntent(
Intent(context, MainActivity::class.java).apply {
this.action = Intent.ACTION_VIEW
putExtra(IntentData.fragmentToOpen, action)
}
Intent(Intent.ACTION_VIEW, null, context, MainActivity::class.java)
.putExtra(IntentData.fragmentToOpen, appShortcut.action)
)
.build()

ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
}

fun createShortcuts(context: Context) {
ShortcutManagerCompat.getDynamicShortcuts(context).takeIf { it.isEmpty() } ?: return

shortcuts.forEach {
val icon = IconCompat.createWithResource(context, it.drawable)
createShortcut(context, it.action, context.getString(it.label), icon)
if (ShortcutManagerCompat.getDynamicShortcuts(context).isEmpty()) {
val dynamicShortcuts = shortcuts.map { createShortcut(context, it) }
ShortcutManagerCompat.setDynamicShortcuts(context, dynamicShortcuts)
}
}
}