fix ANR caused by direct message badge (#4182)

Saw an ANR (app not responding) error being reported in the Play console
and then found this. Sorry but `runBlocking` in production code is an
absolute no go.
This commit is contained in:
Konrad Pozniak 2023-12-21 19:02:28 +01:00 committed by GitHub
parent d8c436c268
commit 9781e43441
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View File

@ -141,6 +141,7 @@ import dagger.android.DispatchingAndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import de.c1710.filemojicompat_ui.helpers.EMOJI_PREFERENCE import de.c1710.filemojicompat_ui.helpers.EMOJI_PREFERENCE
import io.reactivex.rxjava3.schedulers.Schedulers import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
@ -329,7 +330,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
updateAnnouncementsBadge() updateAnnouncementsBadge()
} }
is NewNotificationsEvent -> { is NewNotificationsEvent -> {
directMessageTab?.let { tab -> directMessageTab?.let {
if (event.accountId == activeAccount.accountId) { if (event.accountId == activeAccount.accountId) {
val hasDirectMessageNotification = val hasDirectMessageNotification =
event.notifications.any { it.type == Notification.Type.MENTION && it.status?.visibility == Status.Visibility.DIRECT } event.notifications.any { it.type == Notification.Type.MENTION && it.status?.visibility == Status.Visibility.DIRECT }
@ -400,10 +401,12 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
tab.badge?.isVisible = showBadge tab.badge?.isVisible = showBadge
// TODO a bit cumbersome (also for resetting) // TODO a bit cumbersome (also for resetting)
accountManager.activeAccount?.let { lifecycleScope.launch(Dispatchers.IO) {
if (it.hasDirectMessageBadge != showBadge) { accountManager.activeAccount?.let {
it.hasDirectMessageBadge = showBadge if (it.hasDirectMessageBadge != showBadge) {
accountManager.saveAccount(it) it.hasDirectMessageBadge = showBadge
accountManager.saveAccount(it)
}
} }
} }
} }

View File

@ -66,7 +66,6 @@ import com.mikepenz.iconics.utils.sizeDp
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import javax.inject.Inject import javax.inject.Inject
import kotlin.time.DurationUnit import kotlin.time.DurationUnit
import kotlin.time.toDuration import kotlin.time.toDuration
@ -132,7 +131,7 @@ class ConversationsFragment :
binding.progressBar.hide() binding.progressBar.hide()
if (loadState.isAnyLoading()) { if (loadState.isAnyLoading()) {
runBlocking { lifecycleScope.launch {
eventHub.dispatch(ConversationsLoadingEvent(accountManager.activeAccount?.accountId ?: "")) eventHub.dispatch(ConversationsLoadingEvent(accountManager.activeAccount?.accountId ?: ""))
} }
} }