Now that FCM token can be rotated, let the FirebaseTokenTest observe the value.
This commit is contained in:
committed by
Benoit Marty
parent
a6fdb90838
commit
a54eee042b
@@ -11,6 +11,10 @@ import android.content.SharedPreferences
|
||||
import androidx.core.content.edit
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.onCompletion
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
@@ -18,6 +22,7 @@ import javax.inject.Inject
|
||||
*/
|
||||
interface FirebaseStore {
|
||||
fun getFcmToken(): String?
|
||||
fun fcmTokenFlow(): Flow<String?>
|
||||
fun storeFcmToken(token: String?)
|
||||
}
|
||||
|
||||
@@ -29,6 +34,22 @@ class SharedPreferencesFirebaseStore @Inject constructor(
|
||||
return sharedPreferences.getString(PREFS_KEY_FCM_TOKEN, null)
|
||||
}
|
||||
|
||||
override fun fcmTokenFlow(): Flow<String?> {
|
||||
val flow = MutableStateFlow(getFcmToken())
|
||||
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, k ->
|
||||
if (k == PREFS_KEY_FCM_TOKEN) {
|
||||
try {
|
||||
flow.value = getFcmToken()
|
||||
} catch (e: Exception) {
|
||||
flow.value = null
|
||||
}
|
||||
}
|
||||
}
|
||||
return flow
|
||||
.onStart { sharedPreferences.registerOnSharedPreferenceChangeListener(listener) }
|
||||
.onCompletion { sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener) }
|
||||
}
|
||||
|
||||
override fun storeFcmToken(token: String?) {
|
||||
sharedPreferences.edit {
|
||||
putString(PREFS_KEY_FCM_TOKEN, token)
|
||||
|
||||
@@ -19,7 +19,10 @@ import io.element.android.libraries.troubleshoot.api.test.NotificationTroublesho
|
||||
import io.element.android.libraries.troubleshoot.api.test.TestFilterData
|
||||
import io.element.android.services.toolbox.api.strings.StringProvider
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesMultibinding(AppScope::class)
|
||||
@@ -41,23 +44,28 @@ class FirebaseTokenTest @Inject constructor(
|
||||
return data.currentPushProviderName == FirebaseConfig.NAME
|
||||
}
|
||||
|
||||
private var currentJob: Job? = null
|
||||
override suspend fun run(coroutineScope: CoroutineScope) {
|
||||
currentJob?.cancel()
|
||||
delegate.start()
|
||||
val token = firebaseStore.getFcmToken()
|
||||
if (token != null) {
|
||||
delegate.updateState(
|
||||
description = stringProvider.getString(
|
||||
R.string.troubleshoot_notifications_test_firebase_token_success,
|
||||
"*****${token.takeLast(8)}"
|
||||
),
|
||||
status = NotificationTroubleshootTestState.Status.Success
|
||||
)
|
||||
} else {
|
||||
delegate.updateState(
|
||||
description = stringProvider.getString(R.string.troubleshoot_notifications_test_firebase_token_failure),
|
||||
status = NotificationTroubleshootTestState.Status.Failure(true)
|
||||
)
|
||||
}
|
||||
currentJob = firebaseStore.fcmTokenFlow()
|
||||
.onEach { token ->
|
||||
if (token != null) {
|
||||
delegate.updateState(
|
||||
description = stringProvider.getString(
|
||||
R.string.troubleshoot_notifications_test_firebase_token_success,
|
||||
"*****${token.takeLast(8)}"
|
||||
),
|
||||
status = NotificationTroubleshootTestState.Status.Success
|
||||
)
|
||||
} else {
|
||||
delegate.updateState(
|
||||
description = stringProvider.getString(R.string.troubleshoot_notifications_test_firebase_token_failure),
|
||||
status = NotificationTroubleshootTestState.Status.Failure(true)
|
||||
)
|
||||
}
|
||||
}
|
||||
.launchIn(coroutineScope)
|
||||
}
|
||||
|
||||
override suspend fun reset() = delegate.reset()
|
||||
|
||||
@@ -7,11 +7,16 @@
|
||||
|
||||
package io.element.android.libraries.pushproviders.firebase
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
|
||||
class InMemoryFirebaseStore(
|
||||
private var token: String? = null
|
||||
) : FirebaseStore {
|
||||
override fun getFcmToken(): String? = token
|
||||
|
||||
override fun fcmTokenFlow(): Flow<String?> = flowOf(token)
|
||||
|
||||
override fun storeFcmToken(token: String?) {
|
||||
this.token = token
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user