-
Notifications
You must be signed in to change notification settings - Fork 13
/
AppShortcutManager.kt
48 lines (44 loc) · 1.74 KB
/
AppShortcutManager.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.zhangke.notionlight.shorcut
import android.content.Intent
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import com.zhangke.architect.coroutines.ApplicationScope
import com.zhangke.framework.utils.appContext
import com.zhangke.notionlight.R
import com.zhangke.notionlight.addblock.AddBlockActivity
import com.zhangke.notionlight.config.NotionPageConfig
import com.zhangke.notionlight.config.NotionPageConfigRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
object AppShortcutManager {
init {
ApplicationScope.launch {
ShortcutManagerCompat.removeAllDynamicShortcuts(appContext)
val pageList = withContext(Dispatchers.IO) {
NotionPageConfigRepo.getPageConfigList()
.firstOrNull()
?.take(5)
}
pageList?.map { it.buildShortcut() }
?.forEach {
ShortcutManagerCompat.pushDynamicShortcut(appContext, it)
}
}
}
private fun NotionPageConfig.buildShortcut(): ShortcutInfoCompat {
val intent = Intent().apply {
action = AddBlockActivity.ACTION_ADD_BLOCK
setClass(appContext, AddBlockActivity::class.java)
putExtra(AddBlockActivity.INTENT_ARG_PAGE, id)
}
return ShortcutInfoCompat.Builder(appContext, id)
.setShortLabel(title)
.setLongLabel(title)
.setIntent(intent)
.setIcon(IconCompat.createWithResource(appContext, R.mipmap.ic_launcher))
.build()
}
}