forked from upstream-mirrors/quik
refactor(utils): Clean up shortcut code
This commit is contained in:
@@ -26,7 +26,7 @@ interface ShortcutManager {
|
|||||||
|
|
||||||
fun updateShortcuts()
|
fun updateShortcuts()
|
||||||
|
|
||||||
fun getShortcut(threadId: Long): ShortcutInfoCompat?
|
fun getOrCreateShortcut(threadId: Long): ShortcutInfoCompat?
|
||||||
|
|
||||||
fun reportShortcutUsed(threadId: Long)
|
fun reportShortcutUsed(threadId: Long)
|
||||||
|
|
||||||
|
|||||||
@@ -356,7 +356,7 @@ class NotificationManagerImpl @Inject constructor(
|
|||||||
|
|
||||||
context.startActivity(intent)
|
context.startActivity(intent)
|
||||||
}
|
}
|
||||||
val sc = shortcutManager.getShortcut(threadId)
|
val sc = shortcutManager.getOrCreateShortcut(threadId)
|
||||||
notification.setShortcutInfo(sc)
|
notification.setShortcutInfo(sc)
|
||||||
notificationManager.notify(threadId.toInt(), notification.build())
|
notificationManager.notify(threadId.toInt(), notification.build())
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ShortcutManager
|
import android.content.pm.ShortcutManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import androidx.core.app.Person
|
|
||||||
import androidx.core.content.pm.ShortcutInfoCompat
|
import androidx.core.content.pm.ShortcutInfoCompat
|
||||||
import androidx.core.content.pm.ShortcutManagerCompat
|
import androidx.core.content.pm.ShortcutManagerCompat
|
||||||
import dev.octoshrimpy.quik.common.util.extensions.getThemedIcon
|
import dev.octoshrimpy.quik.common.util.extensions.getThemedIcon
|
||||||
@@ -54,7 +53,10 @@ class ShortcutManagerImpl @Inject constructor(
|
|||||||
if (shortcutManager.isRateLimitingActive) return
|
if (shortcutManager.isRateLimitingActive) return
|
||||||
|
|
||||||
val shortcuts: List<ShortcutInfoCompat> = conversationRepo.getTopConversations()
|
val shortcuts: List<ShortcutInfoCompat> = conversationRepo.getTopConversations()
|
||||||
.take(shortcutManager.maxShortcutCountPerActivity - shortcutManager.manifestShortcuts.size)
|
.take(
|
||||||
|
shortcutManager.maxShortcutCountPerActivity -
|
||||||
|
shortcutManager.manifestShortcuts.size
|
||||||
|
)
|
||||||
.map { conversation -> createShortcutForConversation(conversation) }
|
.map { conversation -> createShortcutForConversation(conversation) }
|
||||||
|
|
||||||
ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts)
|
ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts)
|
||||||
@@ -64,17 +66,17 @@ class ShortcutManagerImpl @Inject constructor(
|
|||||||
/**
|
/**
|
||||||
* Get the shortcut for a threadId. Will create it if it doesn't exist.
|
* Get the shortcut for a threadId. Will create it if it doesn't exist.
|
||||||
*/
|
*/
|
||||||
override fun getShortcut(threadId: Long): ShortcutInfoCompat? {
|
override fun getOrCreateShortcut(threadId: Long): ShortcutInfoCompat? {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||||
var sc = getShortcuts().find { it.id == threadId.toString() }
|
var sc = getShortcuts().find { it.id == threadId.toString() }
|
||||||
if(sc != null)
|
if (sc != null)
|
||||||
sc = updateShortcut(sc)
|
sc = updateShortcut(sc)
|
||||||
if (sc == null) {
|
if (sc == null) {
|
||||||
val conv = conversationRepo.getConversation(threadId)
|
val conv = conversationRepo.getConversation(threadId)
|
||||||
if (conv == null)
|
if (conv == null)
|
||||||
return null
|
return null
|
||||||
else
|
else
|
||||||
sc = createShortcutForConversation(conv)
|
sc = createShortcutForConversation(conv)
|
||||||
}
|
}
|
||||||
return sc
|
return sc
|
||||||
} else {
|
} else {
|
||||||
@@ -88,10 +90,8 @@ class ShortcutManagerImpl @Inject constructor(
|
|||||||
override fun reportShortcutUsed(threadId: Long) {
|
override fun reportShortcutUsed(threadId: Long) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||||
val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
||||||
if(getShortcut(threadId ) == null) {
|
if (getOrCreateShortcut(threadId ) == null) {
|
||||||
val conversation = conversationRepo.getOrCreateConversation(threadId)
|
val conversation = conversationRepo.getOrCreateConversation(threadId) ?: return
|
||||||
if (conversation == null)
|
|
||||||
return
|
|
||||||
val shortcut = createShortcutForConversation(conversation)
|
val shortcut = createShortcutForConversation(conversation)
|
||||||
ShortcutManagerCompat.setDynamicShortcuts(context, listOf(shortcut))
|
ShortcutManagerCompat.setDynamicShortcuts(context, listOf(shortcut))
|
||||||
}
|
}
|
||||||
@@ -120,7 +120,7 @@ class ShortcutManagerImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val persons: Array<Person> = conversation.recipients.map { it -> it.toPerson(context, colors) }.toTypedArray();
|
val persons = conversation.recipients.map { it.toPerson(context, colors) }.toTypedArray()
|
||||||
|
|
||||||
val intent = Intent(context, ComposeActivity::class.java)
|
val intent = Intent(context, ComposeActivity::class.java)
|
||||||
.setAction(Intent.ACTION_VIEW)
|
.setAction(Intent.ACTION_VIEW)
|
||||||
@@ -152,10 +152,10 @@ class ShortcutManagerImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getShortcuts() : List<ShortcutInfoCompat> {
|
private fun getShortcuts() : List<ShortcutInfoCompat> {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||||
return ShortcutManagerCompat.getDynamicShortcuts(context)
|
ShortcutManagerCompat.getDynamicShortcuts(context)
|
||||||
} else {
|
} else {
|
||||||
return emptyList()
|
emptyList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user