refactor: Implement updateShortcut as a suspending function (#491)

Previously a regular function that created and subscribed to an rxJava
callable, now it's a suspending function that enforces Dispatchers.IO as
the context, launched in its own coroutine.
This commit is contained in:
Nik Clayton 2024-03-03 17:21:04 +01:00 committed by GitHub
parent 900b1728bf
commit da6e026cc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 56 deletions

View File

@ -979,8 +979,10 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
} }
updateProfiles() updateProfiles()
externalScope.launch {
updateShortcut(applicationContext, accountManager.activeAccount!!) updateShortcut(applicationContext, accountManager.activeAccount!!)
} }
}
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
private fun loadDrawerAvatar(avatarUrl: String, showPlaceholder: Boolean) { private fun loadDrawerAvatar(avatarUrl: String, showPlaceholder: Boolean) {

View File

@ -29,11 +29,10 @@ import app.pachli.core.database.model.AccountEntity
import app.pachli.core.designsystem.R as DR import app.pachli.core.designsystem.R as DR
import app.pachli.core.navigation.MainActivityIntent import app.pachli.core.navigation.MainActivityIntent
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import io.reactivex.rxjava3.core.Single import kotlinx.coroutines.Dispatchers
import io.reactivex.rxjava3.schedulers.Schedulers import kotlinx.coroutines.withContext
fun updateShortcut(context: Context, account: AccountEntity) { suspend fun updateShortcut(context: Context, account: AccountEntity) = withContext(Dispatchers.IO) {
Single.fromCallable {
val innerSize = context.resources.getDimensionPixelSize(DR.dimen.adaptive_bitmap_inner_size) val innerSize = context.resources.getDimensionPixelSize(DR.dimen.adaptive_bitmap_inner_size)
val outerSize = context.resources.getDimensionPixelSize(DR.dimen.adaptive_bitmap_outer_size) val outerSize = context.resources.getDimensionPixelSize(DR.dimen.adaptive_bitmap_outer_size)
@ -83,10 +82,6 @@ fun updateShortcut(context: Context, account: AccountEntity) {
.build() .build()
ShortcutManagerCompat.addDynamicShortcuts(context, listOf(shortcutInfo)) ShortcutManagerCompat.addDynamicShortcuts(context, listOf(shortcutInfo))
}
.subscribeOn(Schedulers.io())
.onErrorReturnItem(false)
.subscribe()
} }
fun removeShortcut(context: Context, account: AccountEntity) { fun removeShortcut(context: Context, account: AccountEntity) {