|
|
|
@ -11,6 +11,10 @@ import android.content.SharedPreferences
@@ -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
@@ -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(
@@ -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) |
|
|
|
|