never create more than the allowed number of shortcuts

This commit is contained in:
Conny Duck 2024-04-22 19:40:31 +02:00
parent 7960db6c78
commit 1099246cdc
No known key found for this signature in database
2 changed files with 58 additions and 50 deletions

View File

@ -1066,7 +1066,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
}
updateProfiles()
shareShortcutHelper.updateShortcut(accountManager.activeAccount!!)
shareShortcutHelper.updateShortcuts()
}
@SuppressLint("CheckResult")

View File

@ -30,21 +30,28 @@ import com.bumptech.glide.Glide
import com.keylesspalace.tusky.MainActivity
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.di.ApplicationScope
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class ShareShortcutHelper @Inject constructor(
private val context: Context,
private val accountManager: AccountManager,
@ApplicationScope private val externalScope: CoroutineScope
) {
fun updateShortcut(account: AccountEntity) {
externalScope.launch {
fun updateShortcuts() {
externalScope.launch(Dispatchers.IO) {
val innerSize = context.resources.getDimensionPixelSize(R.dimen.adaptive_bitmap_inner_size)
val outerSize = context.resources.getDimensionPixelSize(R.dimen.adaptive_bitmap_outer_size)
val maxNumberOfShortcuts = ShortcutManagerCompat.getMaxShortcutCountPerActivity(context)
val shortcuts = accountManager.accounts.take(maxNumberOfShortcuts).map { account ->
val bmp = if (TextUtils.isEmpty(account.profilePictureUrl)) {
Glide.with(context)
.asBitmap()
@ -84,7 +91,7 @@ class ShareShortcutHelper @Inject constructor(
putExtra(ShortcutManagerCompat.EXTRA_SHORTCUT_ID, account.id.toString())
}
val shortcutInfo = ShortcutInfoCompat.Builder(context, account.id.toString())
ShortcutInfoCompat.Builder(context, account.id.toString())
.setIntent(intent)
.setCategories(setOf("com.keylesspalace.tusky.Share"))
.setShortLabel(account.displayName)
@ -92,8 +99,9 @@ class ShareShortcutHelper @Inject constructor(
.setLongLived(true)
.setIcon(icon)
.build()
}
ShortcutManagerCompat.addDynamicShortcuts(context, listOf(shortcutInfo))
ShortcutManagerCompat.addDynamicShortcuts(context, shortcuts)
}
}