Disable fcm preference while register request is in flight

This commit is contained in:
andrew 2023-08-16 13:27:28 +09:30
parent 0aa5dc7969
commit d308f381d9
2 changed files with 20 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.notifications
import android.content.Context import android.content.Context
import com.goterl.lazysodium.utils.KeyPair import com.goterl.lazysodium.utils.KeyPair
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -32,18 +33,20 @@ class PushRegistry @Inject constructor(
private var pushRegistrationJob: Job? = null private var pushRegistrationJob: Job? = null
fun refresh(force: Boolean) { fun refresh(force: Boolean): Job {
Log.d(TAG, "refresh() called with: force = $force") Log.d(TAG, "refresh() called with: force = $force")
pushRegistrationJob?.apply { pushRegistrationJob?.apply {
if (force) cancel() else if (isActive || !tokenManager.hasValidRegistration) return if (force) cancel() else if (isActive) return MainScope().launch {}
} }
pushRegistrationJob = MainScope().launch { return MainScope().launch(Dispatchers.IO) {
register(tokenFetcher.fetch()) fail { e -> try {
register(tokenFetcher.fetch()).get()
} catch (e: Exception) {
Log.e(TAG, "register failed", e) Log.e(TAG, "register failed", e)
} }
} }.also { pushRegistrationJob = it }
} }
fun register(token: String?): Promise<*, Exception> { fun register(token: String?): Promise<*, Exception> {

View File

@ -42,7 +42,18 @@ class NotificationsPreferenceFragment : ListSummaryPreferenceFragment() {
fcmPreference.isChecked = prefs.isPushEnabled() fcmPreference.isChecked = prefs.isPushEnabled()
fcmPreference.setOnPreferenceChangeListener { _: Preference, newValue: Any -> fcmPreference.setOnPreferenceChangeListener { _: Preference, newValue: Any ->
prefs.setPushEnabled(newValue as Boolean) 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 true
} }
if (NotificationChannels.supported()) { if (NotificationChannels.supported()) {