Skip to content

Commit da4bf70

Browse files
Added the dynamic app shortcuts based on the app shorting order
Signed-off-by: Keval Patel <kevalpatel2106@gmail.com>
1 parent 001c854 commit da4bf70

19 files changed

Lines changed: 122 additions & 47 deletions

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
<action android:name="android.intent.action.BOOT_COMPLETED" />
8989
</intent-filter>
9090
</receiver>
91+
<receiver android:name=".utils.LocaleChangedReceiver">
92+
<intent-filter>
93+
<action android:name="android.intent.action.LOCALE_CHANGED" />
94+
</intent-filter>
95+
</receiver>
9196
<receiver android:name=".notifications.ProgressNotificationReceiver" />
9297
</application>
9398

app/src/main/java/com/kevalpatel2106/yip/dashboard/DashboardViewModel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.kevalpatel2106.yip.recyclerview.representable.YipItemRepresentable
2020
import com.kevalpatel2106.yip.repo.YipRepo
2121
import com.kevalpatel2106.yip.repo.billing.BillingRepo
2222
import com.kevalpatel2106.yip.repo.providers.SharedPrefsProvider
23+
import com.kevalpatel2106.yip.utils.AppShortcutHelper
2324
import io.reactivex.BackpressureStrategy
2425
import io.reactivex.Flowable
2526
import io.reactivex.functions.BiFunction
@@ -31,7 +32,8 @@ internal class DashboardViewModel @Inject constructor(
3132
private val application: Application,
3233
private val yipRepo: YipRepo,
3334
private val sharedPrefsProvider: SharedPrefsProvider,
34-
private val billingRepo: BillingRepo
35+
private val billingRepo: BillingRepo,
36+
private val appShortcutHelper: AppShortcutHelper
3537
) : BaseViewModel() {
3638
internal val progresses = MutableLiveData<ArrayList<YipItemRepresentable>>()
3739
internal val askForRating = MutableLiveData<Unit>()
@@ -56,6 +58,8 @@ internal class DashboardViewModel @Inject constructor(
5658
progresses.value?.clear()
5759
progresses.value?.add(LoadingRepresentable)
5860
progresses.recall()
61+
}.doOnNext { (progresses, _) ->
62+
appShortcutHelper.updateDynamicShortcuts(progresses)
5963
}.map { (progresses, isPro) ->
6064

6165
// Add Ads if the user is not pro.

app/src/main/java/com/kevalpatel2106/yip/detail/DetailFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal class DetailFragment : Fragment() {
3030
menuItem.itemId == R.id.menu_delete_progress -> {
3131
model.viewState.value?.progressTitleText?.let { title -> conformDelete(title) }
3232
}
33-
menuItem.itemId == R.id.menu_pin_shortcut -> model.pinShortcut()
33+
menuItem.itemId == R.id.menu_pin_shortcut -> model.requestPinShortcut()
3434
}
3535
true
3636
}

app/src/main/java/com/kevalpatel2106/yip/detail/DetailViewModel.kt

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package com.kevalpatel2106.yip.detail
22

33
import android.app.Application
44
import android.text.SpannableString
5-
import androidx.core.content.pm.ShortcutInfoCompat
6-
import androidx.core.content.pm.ShortcutManagerCompat
7-
import androidx.core.graphics.drawable.IconCompat
85
import androidx.lifecycle.MutableLiveData
96
import com.kevalpatel2106.yip.R
107
import com.kevalpatel2106.yip.core.BaseViewModel
@@ -14,13 +11,15 @@ import com.kevalpatel2106.yip.core.addTo
1411
import com.kevalpatel2106.yip.core.darkenColor
1512
import com.kevalpatel2106.yip.repo.YipRepo
1613
import com.kevalpatel2106.yip.repo.utils.DateFormatter
17-
import com.kevalpatel2106.yip.splash.AppLaunchHelper
14+
import com.kevalpatel2106.yip.utils.AppLaunchHelper
15+
import com.kevalpatel2106.yip.utils.AppShortcutHelper
1816
import timber.log.Timber
1917
import javax.inject.Inject
2018

2119
internal class DetailViewModel @Inject internal constructor(
2220
private val application: Application,
2321
private val yipRepo: YipRepo,
22+
private val appShortcutHelper: AppShortcutHelper,
2423
private val sdf: DateFormatter
2524
) : BaseViewModel() {
2625

@@ -91,25 +90,10 @@ internal class DetailViewModel @Inject internal constructor(
9190
.addTo(compositeDisposable)
9291
}
9392

94-
internal fun pinShortcut() {
95-
if (!ShortcutManagerCompat.isRequestPinShortcutSupported(application)) return
96-
97-
val shortcutInfo = ShortcutInfoCompat.Builder(
98-
application,
99-
application.getString(R.string.progress_pin_shortcut_id)
100-
).setIcon(
101-
IconCompat.createWithResource(application, R.drawable.progress_app_shortcut)
102-
).setShortLabel(
103-
viewState.value?.progressTitleText ?: application.getString(R.string.application_name)
104-
).setIntent(
105-
AppLaunchHelper.launchWithProgressDetail(application, progressId)
106-
).setAlwaysBadged()
107-
.build()
108-
109-
ShortcutManagerCompat.requestPinShortcut(
110-
application,
111-
shortcutInfo,
112-
null
113-
)
93+
internal fun requestPinShortcut() {
94+
val title = viewState.value?.progressTitleText
95+
?: application.getString(R.string.application_name)
96+
val launchIntent = AppLaunchHelper.launchWithProgressDetail(application, progressId)
97+
appShortcutHelper.requestPinShortcut(title, launchIntent)
11498
}
11599
}

app/src/main/java/com/kevalpatel2106/yip/di/AppComponent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.kevalpatel2106.yip.payment.PaymentActivity
1313
import com.kevalpatel2106.yip.settings.SettingsActivity
1414
import com.kevalpatel2106.yip.settings.SettingsFragment
1515
import com.kevalpatel2106.yip.splash.SplashActivity
16-
import com.kevalpatel2106.yip.utils.NotificationViewer
16+
import com.kevalpatel2106.yip.views.NotificationViewer
1717
import com.kevalpatel2106.yip.webviews.WebViewActivity
1818
import com.kevalpatel2106.yip.widget.ProgressListRemoteService
1919
import dagger.Component
@@ -50,4 +50,4 @@ internal fun Context.getAppComponent(): AppComponent {
5050
return DaggerAppComponent.builder()
5151
.coreComponent((applicationContext as? YIPApplication)?.coreComponent)
5252
.build()
53-
}
53+
}

app/src/main/java/com/kevalpatel2106/yip/edit/EditProgressActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import com.kevalpatel2106.yip.edit.EditProgressUseCases.conformBeforeExit
2121
import com.kevalpatel2106.yip.edit.EditProgressUseCases.getDatePicker
2222
import com.kevalpatel2106.yip.payment.PaymentActivity
2323
import com.kevalpatel2106.yip.repo.utils.DateFormatter
24-
import com.kevalpatel2106.yip.utils.ColorPicker
24+
import com.kevalpatel2106.yip.views.ColorPicker
2525
import kotlinx.android.synthetic.main.activity_edit_progress.edit_color
2626
import kotlinx.android.synthetic.main.activity_edit_progress.edit_end_time
2727
import kotlinx.android.synthetic.main.activity_edit_progress.edit_progress_title
@@ -177,4 +177,4 @@ internal class EditProgressActivity : AppCompatActivity() {
177177
})
178178
}
179179
}
180-
}
180+
}

app/src/main/java/com/kevalpatel2106/yip/notifications/ProgressNotification.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import androidx.annotation.VisibleForTesting
1111
import androidx.core.app.NotificationCompat
1212
import com.kevalpatel2106.yip.R
1313
import com.kevalpatel2106.yip.entity.Progress
14-
import com.kevalpatel2106.yip.splash.AppLaunchHelper
14+
import com.kevalpatel2106.yip.utils.AppLaunchHelper
1515

1616
/**
1717
* Helper class for showing and canceling progress

app/src/main/java/com/kevalpatel2106/yip/splash/SplashActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity
55
import androidx.lifecycle.ViewModelProvider
66
import com.kevalpatel2106.yip.core.di.provideViewModel
77
import com.kevalpatel2106.yip.di.getAppComponent
8+
import com.kevalpatel2106.yip.utils.AppLaunchHelper
89
import javax.inject.Inject
910

1011
internal class SplashActivity : AppCompatActivity() {

app/src/main/java/com/kevalpatel2106/yip/splash/AppLaunchHelper.kt renamed to app/src/main/java/com/kevalpatel2106/yip/utils/AppLaunchHelper.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package com.kevalpatel2106.yip.splash
1+
package com.kevalpatel2106.yip.utils
22

33
import android.content.Context
44
import android.content.Intent
55
import androidx.core.app.TaskStackBuilder
66
import com.kevalpatel2106.yip.core.prepareLaunchIntent
77
import com.kevalpatel2106.yip.dashboard.DashboardActivity
88
import com.kevalpatel2106.yip.edit.EditProgressActivity
9+
import com.kevalpatel2106.yip.splash.SplashActivity
910

1011
internal object AppLaunchHelper {
1112
private const val ACTION_CREATE_PROGRESS = "com.kevalpatel2106.yip.create_new"
@@ -33,16 +34,16 @@ internal object AppLaunchHelper {
3334

3435
internal fun launchFlow(splashActivity: SplashActivity, intent: Intent) {
3536
when {
36-
intent.action == AppLaunchHelper.ACTION_CREATE_PROGRESS -> {
37+
intent.action == ACTION_CREATE_PROGRESS -> {
3738
TaskStackBuilder.create(splashActivity)
3839
.addNextIntent(DashboardActivity.launchIntent(splashActivity))
3940
.addNextIntent(EditProgressActivity.createNewDeadlineIntent(splashActivity))
4041
.startActivities()
4142
}
42-
intent.action == AppLaunchHelper.ACTION_LAUNCH_WITH_PROGRESS -> {
43+
intent.action == ACTION_LAUNCH_WITH_PROGRESS -> {
4344
DashboardActivity.launch(
4445
splashActivity,
45-
intent.getLongExtra(AppLaunchHelper.ARG_PROGRESS_ID, -1)
46+
intent.getLongExtra(ARG_PROGRESS_ID, -1)
4647
)
4748
}
4849
else -> DashboardActivity.launch(splashActivity)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.kevalpatel2106.yip.utils
2+
3+
import android.app.Application
4+
import android.content.Intent
5+
import androidx.core.content.pm.ShortcutInfoCompat
6+
import androidx.core.content.pm.ShortcutManagerCompat
7+
import androidx.core.graphics.drawable.IconCompat
8+
import com.kevalpatel2106.yip.R
9+
import com.kevalpatel2106.yip.entity.Progress
10+
import javax.inject.Inject
11+
12+
class AppShortcutHelper @Inject internal constructor(
13+
private val application: Application
14+
) {
15+
16+
internal fun requestPinShortcut(title: String, launchIntent: Intent) {
17+
if (!ShortcutManagerCompat.isRequestPinShortcutSupported(application)) return
18+
19+
val shortcutInfo = ShortcutInfoCompat.Builder(
20+
application,
21+
application.getString(R.string.progress_pin_shortcut_id)
22+
).setIcon(IconCompat.createWithResource(application, R.drawable.progress_app_shortcut))
23+
.setShortLabel(title)
24+
.setIntent(launchIntent)
25+
.setAlwaysBadged()
26+
.build()
27+
28+
ShortcutManagerCompat.requestPinShortcut(
29+
application,
30+
shortcutInfo,
31+
null
32+
)
33+
}
34+
35+
internal fun updateDynamicShortcuts(progresses: List<Progress>): Boolean {
36+
ShortcutManagerCompat.removeAllDynamicShortcuts(application)
37+
if (progresses.isEmpty()) return true
38+
39+
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+
}
52+
return ShortcutManagerCompat.addDynamicShortcuts(application, shortcuts)
53+
}
54+
55+
companion object {
56+
private const val NUMBER_OF_STATIC_APP_SHORTCUT = 1
57+
}
58+
}

0 commit comments

Comments
 (0)