From d308f381d9e9554b1cc9d6948c9483677e0bbdf9 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 16 Aug 2023 13:27:28 +0930 Subject: [PATCH] Disable fcm preference while register request is in flight --- .../securesms/notifications/PushRegistry.kt | 13 ++++++++----- .../preferences/NotificationsPreferenceFragment.kt | 13 ++++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/PushRegistry.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/PushRegistry.kt index 718b2b29ca..b0954f2327 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/PushRegistry.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/PushRegistry.kt @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.notifications import android.content.Context import com.goterl.lazysodium.utils.KeyPair import dagger.hilt.android.qualifiers.ApplicationContext +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch @@ -32,18 +33,20 @@ class PushRegistry @Inject constructor( private var pushRegistrationJob: Job? = null - fun refresh(force: Boolean) { + fun refresh(force: Boolean): Job { Log.d(TAG, "refresh() called with: force = $force") pushRegistrationJob?.apply { - if (force) cancel() else if (isActive || !tokenManager.hasValidRegistration) return + if (force) cancel() else if (isActive) return MainScope().launch {} } - pushRegistrationJob = MainScope().launch { - register(tokenFetcher.fetch()) fail { e -> + return MainScope().launch(Dispatchers.IO) { + try { + register(tokenFetcher.fetch()).get() + } catch (e: Exception) { Log.e(TAG, "register failed", e) } - } + }.also { pushRegistrationJob = it } } fun register(token: String?): Promise<*, Exception> { diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.kt index 933f13b9ec..fa6461acc4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.kt @@ -42,7 +42,18 @@ class NotificationsPreferenceFragment : ListSummaryPreferenceFragment() { fcmPreference.isChecked = prefs.isPushEnabled() fcmPreference.setOnPreferenceChangeListener { _: Preference, newValue: Any -> prefs.setPushEnabled(newValue as Boolean) - pushRegistry.refresh(true) + val job = pushRegistry.refresh(true) + + fcmPreference.isEnabled = false + + lifecycleScope.launch(Dispatchers.IO) { + job.join() + + withContext(Dispatchers.Main) { + fcmPreference.isEnabled = true + } + } + true } if (NotificationChannels.supported()) {