mirror of
https://github.com/oxen-io/session-android.git
synced 2025-01-03 13:47:45 +00:00
Disable fcm preference while register request is in flight
This commit is contained in:
parent
0aa5dc7969
commit
d308f381d9
@ -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> {
|
||||
|
@ -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()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user