avoid runBlocking

This commit is contained in:
Benoit Marty 2022-06-02 14:41:46 +02:00 committed by Benoit Marty
parent fb7df5bf46
commit a5378d6e94

View File

@ -19,6 +19,7 @@ package im.vector.app.core.pushers
import android.content.Context
import android.content.pm.PackageManager
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@ -28,7 +29,7 @@ import im.vector.app.features.VectorFeatures
import im.vector.app.features.settings.BackgroundSyncMode
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.push.fcm.FcmHelper
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.Matrix
import org.matrix.android.sdk.api.cache.CacheStrategy
import org.matrix.android.sdk.api.util.MatrixJsonParser
@ -76,11 +77,12 @@ class UnifiedPushHelper @Inject constructor(
pushersManager: PushersManager? = null,
onDoneRunnable: Runnable? = null
) {
activity.lifecycleScope.launch {
if (!vectorFeatures.allowExternalUnifiedPushDistributors()) {
up.saveDistributor(context, context.packageName)
up.registerApp(context)
onDoneRunnable?.run()
return
return@launch
}
if (force) {
// Un-register first
@ -89,7 +91,7 @@ class UnifiedPushHelper @Inject constructor(
if (up.getDistributor(context).isNotEmpty()) {
up.registerApp(context)
onDoneRunnable?.run()
return
return@launch
}
// By default, use internal solution (fcm/background sync)
@ -104,6 +106,7 @@ class UnifiedPushHelper @Inject constructor(
openDistributorDialogInternal(activity, pushersManager, onDoneRunnable, distributors, !force, !force)
}
}
}
fun openDistributorDialog(
activity: FragmentActivity,
@ -158,6 +161,7 @@ class UnifiedPushHelper @Inject constructor(
return@setItems
}
activity.lifecycleScope.launch {
if (unregisterFirst) {
// Un-register first
unregister(pushersManager)
@ -166,6 +170,7 @@ class UnifiedPushHelper @Inject constructor(
Timber.i("Saving distributor: $distributor")
up.registerApp(context)
}
}
.setCancelable(cancellable)
.setOnDismissListener {
onDoneRunnable?.run()
@ -173,16 +178,14 @@ class UnifiedPushHelper @Inject constructor(
.show()
}
fun unregister(pushersManager: PushersManager? = null) {
suspend fun unregister(pushersManager: PushersManager? = null) {
val mode = BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME
vectorPreferences.setFdroidSyncBackgroundMode(mode)
runBlocking {
try {
pushersManager?.unregisterPusher(unifiedPushStore.getEndpointOrToken().orEmpty())
} catch (e: Exception) {
Timber.d(e, "Probably unregistering a non existing pusher")
}
}
unifiedPushStore.storeUpEndpoint(null)
unifiedPushStore.storePushGateway(null)
up.unregisterApp(context)