observing both the email pushers and email pids so that displayed email pushers are always in sync

This commit is contained in:
Adam Brown 2021-10-12 12:42:35 +01:00
parent bd51eae741
commit 786dec5dc0
2 changed files with 31 additions and 10 deletions

View file

@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.extensions
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.Observer
inline fun <T> LiveData<T>.observeK(owner: LifecycleOwner, crossinline observer: (T?) -> Unit) {
@ -27,3 +28,25 @@ inline fun <T> LiveData<T>.observeK(owner: LifecycleOwner, crossinline observer:
inline fun <T> LiveData<T>.observeNotNull(owner: LifecycleOwner, crossinline observer: (T) -> Unit) {
this.observe(owner, Observer { it?.run(observer) })
}
fun <T1, T2, R> combineLatest(source1: LiveData<T1>, source2: LiveData<T2>, mapper: (T1, T2) -> R): LiveData<R> {
val combined = MediatorLiveData<R>()
var source1Result: T1? = null
var source2Result: T2? = null
fun notify() {
if (source1Result != null && source2Result != null) {
combined.value = mapper(source1Result!!, source2Result!!)
}
}
combined.addSource(source1) {
source1Result = it
notify()
}
combined.addSource(source2) {
source2Result = it
notify()
}
return combined
}

View file

@ -54,6 +54,7 @@ import org.matrix.android.sdk.api.pushrules.RuleKind
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.identity.ThreePid
import org.matrix.android.sdk.api.session.pushers.Pusher
import org.matrix.android.sdk.internal.extensions.combineLatest
import javax.inject.Inject
// Referenced in vector_settings_preferences_root.xml
@ -339,11 +340,11 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
VectorPreferences.SETTINGS_ENABLE_ALL_NOTIF_PREFERENCE_KEY -> {
VectorPreferences.SETTINGS_ENABLE_ALL_NOTIF_PREFERENCE_KEY -> {
updateEnabledForAccount(preference)
true
}
else -> {
else -> {
return super.onPreferenceTreeClick(preference)
}
}
@ -406,12 +407,9 @@ private fun Session.getEmailsWithPushInformation(): List<Pair<ThreePid.Email, Bo
}
private fun Session.getEmailsWithPushInformationLive(): LiveData<List<Pair<ThreePid.Email, Boolean>>> {
return getThreePidsLive(refreshData = true)
.distinctUntilChanged()
.map { threePids ->
val emailPushers = getPushers().filter { it.kind == Pusher.KIND_EMAIL }
threePids
.filterIsInstance<ThreePid.Email>()
.map { it to emailPushers.any { pusher -> pusher.pushKey == it.email } }
}
val emailThreePids = getThreePidsLive(refreshData = true).map { it.filterIsInstance<ThreePid.Email>() }
val emailPushers = getPushersLive().map { it.filter { pusher -> pusher.kind == Pusher.KIND_EMAIL } }
return combineLatest(emailThreePids, emailPushers) { emailThreePidsResult, emailPushersResult ->
emailThreePidsResult.map { it to emailPushersResult.any { pusher -> pusher.pushKey == it.email } }
}.distinctUntilChanged()
}