Skip to content

Commit

Permalink
Fixing the progress list not loading for the devices not supporting a…
Browse files Browse the repository at this point in the history
…pp shortcuts

Signed-off-by: Keval Patel <[email protected]>
  • Loading branch information
kevalpatel2106 committed Mar 13, 2019
1 parent b9a92df commit b40096e
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions app/src/main/java/com/kevalpatel2106/yip/utils/AppShortcutHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,33 @@ class AppShortcutHelper @Inject internal constructor(

internal fun updateDynamicShortcuts(progresses: List<Progress>): Boolean {
ShortcutManagerCompat.removeAllDynamicShortcuts(application)
if (progresses.isEmpty()) return true
if (progresses.isEmpty() || isDeviceSupportAppShortcuts(application)) return true

val icon = IconCompat.createWithResource(application, R.drawable.progress_app_shortcut)
val shortcuts = progresses.subList(
0,
ShortcutManagerCompat.getMaxShortcutCountPerActivity(application) - NUMBER_OF_STATIC_APP_SHORTCUT
).map { progress ->
ShortcutInfoCompat.Builder(
application,
application.getString(R.string.progress_app_shortcut_id, progress.id)
).setIcon(icon)
.setShortLabel(progress.title)
.setIntent(AppLaunchHelper.launchWithProgressDetail(application, progress.id))
.build()
}
val shortcuts = getProgressesFoAppShortcuts(progresses)
.map { progress ->
ShortcutInfoCompat.Builder(
application,
application.getString(R.string.progress_app_shortcut_id, progress.id)
).setIcon(icon)
.setShortLabel(progress.title)
.setIntent(AppLaunchHelper.launchWithProgressDetail(application, progress.id))
.build()
}
return ShortcutManagerCompat.addDynamicShortcuts(application, shortcuts)
}

private fun getProgressesFoAppShortcuts(
progresses: List<Progress>,
maxShortcutCount: Int = ShortcutManagerCompat.getMaxShortcutCountPerActivity(application)
): List<Progress> {
val lastIndex = Math.min(progresses.size, maxShortcutCount - NUMBER_OF_STATIC_APP_SHORTCUT)
return progresses.subList(0, lastIndex)
}

private fun isDeviceSupportAppShortcuts(application: Application) =
ShortcutManagerCompat.getMaxShortcutCountPerActivity(application) == 0

companion object {
private const val NUMBER_OF_STATIC_APP_SHORTCUT = 1
}
Expand Down

0 comments on commit b40096e

Please sign in to comment.