From fa0860ac0f44a45c137e6d1039267c0c2170a5b6 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 18 Jul 2024 17:52:53 +0200 Subject: [PATCH] Authenticated media : listen db. --- .../sdk/internal/session/SessionModule.kt | 5 +++ .../DefaultIsAuthenticatedMediaSupported.kt | 34 ++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt index 54834f4263..d6e1d36b54 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt @@ -83,6 +83,7 @@ import org.matrix.android.sdk.internal.session.events.DefaultEventService import org.matrix.android.sdk.internal.session.homeserver.DefaultHomeServerCapabilitiesService import org.matrix.android.sdk.internal.session.identity.DefaultIdentityService import org.matrix.android.sdk.internal.session.integrationmanager.IntegrationManager +import org.matrix.android.sdk.internal.session.media.DefaultIsAuthenticatedMediaSupported import org.matrix.android.sdk.internal.session.openid.DefaultOpenIdService import org.matrix.android.sdk.internal.session.permalinks.DefaultPermalinkService import org.matrix.android.sdk.internal.session.room.EventRelationsAggregationProcessor @@ -365,6 +366,10 @@ internal abstract class SessionModule { @IntoSet abstract fun bindEventInsertObserver(observer: EventInsertLiveObserver): SessionLifecycleObserver + @Binds + @IntoSet + abstract fun bindIsMediaAuthenticated(observer: DefaultIsAuthenticatedMediaSupported): SessionLifecycleObserver + @Binds @IntoSet abstract fun bindIntegrationManager(manager: IntegrationManager): SessionLifecycleObserver diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/media/DefaultIsAuthenticatedMediaSupported.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/media/DefaultIsAuthenticatedMediaSupported.kt index f2884c5cf7..e6f09c9353 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/media/DefaultIsAuthenticatedMediaSupported.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/media/DefaultIsAuthenticatedMediaSupported.kt @@ -18,28 +18,44 @@ package org.matrix.android.sdk.internal.session.media import com.zhuinden.monarchy.Monarchy import io.realm.Realm +import io.realm.RealmResults +import org.matrix.android.sdk.internal.database.RealmLiveEntityObserver import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntity -import org.matrix.android.sdk.internal.database.query.get import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.session.SessionScope +import timber.log.Timber import javax.inject.Inject @SessionScope -class DefaultIsAuthenticatedMediaSupported @Inject constructor( +internal class DefaultIsAuthenticatedMediaSupported @Inject constructor( @SessionDatabase private val monarchy: Monarchy, -) : IsAuthenticatedMediaSupported { - - private val canUseAuthenticatedMedia: Boolean by lazy { - canUseAuthenticatedMedia() - } +) : + IsAuthenticatedMediaSupported, + RealmLiveEntityObserver(monarchy.realmConfiguration) { override fun invoke(): Boolean { return canUseAuthenticatedMedia } - private fun canUseAuthenticatedMedia(): Boolean { + override val query = Monarchy.Query { + it.where(HomeServerCapabilitiesEntity::class.java) + } + + override fun onChange(results: RealmResults) { + Timber.d("HomeServerCapabilitiesEntity updated.") + canUseAuthenticatedMedia = results.canUseAuthenticatedMedia() + Timber.d("canUseAuthenticatedMedia: $canUseAuthenticatedMedia") + } + + private var canUseAuthenticatedMedia = getInitialValue() + + private fun getInitialValue(): Boolean { return Realm.getInstance(monarchy.realmConfiguration).use { realm -> - HomeServerCapabilitiesEntity.get(realm)?.canUseAuthenticatedMedia ?: false + query.createQuery(realm).findAll().canUseAuthenticatedMedia() } } + + private fun RealmResults.canUseAuthenticatedMedia(): Boolean { + return firstOrNull()?.canUseAuthenticatedMedia ?: false + } }