Skip to content

Commit b40096e

Browse files
Fixing the progress list not loading for the devices not supporting app shortcuts
Signed-off-by: Keval Patel <kevalpatel2106@gmail.com>
1 parent b9a92df commit b40096e

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

app/src/main/java/com/kevalpatel2106/yip/utils/AppShortcutHelper.kt

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,33 @@ class AppShortcutHelper @Inject internal constructor(
3434

3535
internal fun updateDynamicShortcuts(progresses: List<Progress>): Boolean {
3636
ShortcutManagerCompat.removeAllDynamicShortcuts(application)
37-
if (progresses.isEmpty()) return true
37+
if (progresses.isEmpty() || isDeviceSupportAppShortcuts(application)) return true
3838

3939
val icon = IconCompat.createWithResource(application, R.drawable.progress_app_shortcut)
40-
val shortcuts = progresses.subList(
41-
0,
42-
ShortcutManagerCompat.getMaxShortcutCountPerActivity(application) - NUMBER_OF_STATIC_APP_SHORTCUT
43-
).map { progress ->
44-
ShortcutInfoCompat.Builder(
45-
application,
46-
application.getString(R.string.progress_app_shortcut_id, progress.id)
47-
).setIcon(icon)
48-
.setShortLabel(progress.title)
49-
.setIntent(AppLaunchHelper.launchWithProgressDetail(application, progress.id))
50-
.build()
51-
}
40+
val shortcuts = getProgressesFoAppShortcuts(progresses)
41+
.map { progress ->
42+
ShortcutInfoCompat.Builder(
43+
application,
44+
application.getString(R.string.progress_app_shortcut_id, progress.id)
45+
).setIcon(icon)
46+
.setShortLabel(progress.title)
47+
.setIntent(AppLaunchHelper.launchWithProgressDetail(application, progress.id))
48+
.build()
49+
}
5250
return ShortcutManagerCompat.addDynamicShortcuts(application, shortcuts)
5351
}
5452

53+
private fun getProgressesFoAppShortcuts(
54+
progresses: List<Progress>,
55+
maxShortcutCount: Int = ShortcutManagerCompat.getMaxShortcutCountPerActivity(application)
56+
): List<Progress> {
57+
val lastIndex = Math.min(progresses.size, maxShortcutCount - NUMBER_OF_STATIC_APP_SHORTCUT)
58+
return progresses.subList(0, lastIndex)
59+
}
60+
61+
private fun isDeviceSupportAppShortcuts(application: Application) =
62+
ShortcutManagerCompat.getMaxShortcutCountPerActivity(application) == 0
63+
5564
companion object {
5665
private const val NUMBER_OF_STATIC_APP_SHORTCUT = 1
5766
}

0 commit comments

Comments
 (0)