diff --git a/libraries/preferences/api/src/main/kotlin/io/element/android/features/preferences/api/store/SessionPreferencesStore.kt b/libraries/preferences/api/src/main/kotlin/io/element/android/features/preferences/api/store/SessionPreferencesStore.kt index 0174d8d1eb..948e9acb75 100644 --- a/libraries/preferences/api/src/main/kotlin/io/element/android/features/preferences/api/store/SessionPreferencesStore.kt +++ b/libraries/preferences/api/src/main/kotlin/io/element/android/features/preferences/api/store/SessionPreferencesStore.kt @@ -19,8 +19,20 @@ package io.element.android.features.preferences.api.store import kotlinx.coroutines.flow.Flow interface SessionPreferencesStore { + suspend fun setSharePresence(enabled: Boolean) + fun isSharePresenceEnabled(): Flow + suspend fun setSendPublicReadReceipts(enabled: Boolean) fun isSendPublicReadReceiptsEnabled(): Flow + suspend fun setRenderReadReceipts(enabled: Boolean) + fun isRenderReadReceiptsEnabled(): Flow + + suspend fun setSendTypingNotifications(enabled: Boolean) + fun isSendTypingNotificationsEnabled(): Flow + + suspend fun setRenderTypingNotifications(enabled: Boolean) + fun isRenderTypingNotificationsEnabled(): Flow + suspend fun clear() } diff --git a/libraries/preferences/impl/src/main/kotlin/io/element/android/libraries/preferences/impl/store/DefaultSessionPreferencesStore.kt b/libraries/preferences/impl/src/main/kotlin/io/element/android/libraries/preferences/impl/store/DefaultSessionPreferencesStore.kt index eb2fffb045..b6283d2c69 100644 --- a/libraries/preferences/impl/src/main/kotlin/io/element/android/libraries/preferences/impl/store/DefaultSessionPreferencesStore.kt +++ b/libraries/preferences/impl/src/main/kotlin/io/element/android/libraries/preferences/impl/store/DefaultSessionPreferencesStore.kt @@ -29,7 +29,9 @@ import io.element.android.libraries.di.annotations.SessionCoroutineScope import io.element.android.libraries.matrix.api.core.SessionId import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map +import kotlinx.coroutines.runBlocking import java.io.File class DefaultSessionPreferencesStore( @@ -43,14 +45,43 @@ class DefaultSessionPreferencesStore( return context.preferencesDataStoreFile("session_${hashedUserId}_preferences") } } + + private val sharePresenceKey = booleanPreferencesKey("sharePresence") private val sendPublicReadReceiptsKey = booleanPreferencesKey("sendPublicReadReceipts") + private val renderReadReceiptsKey = booleanPreferencesKey("renderReadReceipts") + private val sendTypingNotificationsKey = booleanPreferencesKey("sendTypingNotifications") + private val renderTypingNotificationsKey = booleanPreferencesKey("renderTypingNotifications") private val dataStoreFile = storeFile(context, sessionId) private val store = PreferenceDataStoreFactory.create(scope = sessionCoroutineScope) { dataStoreFile } + override suspend fun setSharePresence(enabled: Boolean) { + update(sharePresenceKey, enabled) + // Also update all the other settings + setSendPublicReadReceipts(enabled) + setRenderReadReceipts(enabled) + setSendTypingNotifications(enabled) + setRenderTypingNotifications(enabled) + } + + override fun isSharePresenceEnabled(): Flow { + // Migration, if sendPublicReadReceiptsKey was false, consider that sharing presence is false. + val defaultValue = runBlocking { isSendPublicReadReceiptsEnabled().first() } + return get(sharePresenceKey, defaultValue) + } + override suspend fun setSendPublicReadReceipts(enabled: Boolean) = update(sendPublicReadReceiptsKey, enabled) override fun isSendPublicReadReceiptsEnabled(): Flow = get(sendPublicReadReceiptsKey, true) + override suspend fun setRenderReadReceipts(enabled: Boolean) = update(renderReadReceiptsKey, enabled) + override fun isRenderReadReceiptsEnabled(): Flow = get(renderReadReceiptsKey, true) + + override suspend fun setSendTypingNotifications(enabled: Boolean) = update(sendTypingNotificationsKey, enabled) + override fun isSendTypingNotificationsEnabled(): Flow = get(sendTypingNotificationsKey, true) + + override suspend fun setRenderTypingNotifications(enabled: Boolean) = update(renderTypingNotificationsKey, enabled) + override fun isRenderTypingNotificationsEnabled(): Flow = get(renderTypingNotificationsKey, true) + override suspend fun clear() { dataStoreFile.safeDelete() } diff --git a/libraries/preferences/test/src/main/kotlin/io/element/android/libraries/featureflag/test/InMemorySessionPreferencesStore.kt b/libraries/preferences/test/src/main/kotlin/io/element/android/libraries/featureflag/test/InMemorySessionPreferencesStore.kt index 1f6f7a6724..9e9fc7ba2a 100644 --- a/libraries/preferences/test/src/main/kotlin/io/element/android/libraries/featureflag/test/InMemorySessionPreferencesStore.kt +++ b/libraries/preferences/test/src/main/kotlin/io/element/android/libraries/featureflag/test/InMemorySessionPreferencesStore.kt @@ -21,19 +21,50 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow class InMemorySessionPreferencesStore( + isSharePresenceEnabled: Boolean = true, isSendPublicReadReceiptsEnabled: Boolean = true, + isRenderReadReceiptsEnabled: Boolean = true, + isSendTypingNotificationsEnabled: Boolean = true, + isRenderTypingNotificationsEnabled: Boolean = true, ) : SessionPreferencesStore { + private val isSharePresenceEnabled = MutableStateFlow(isSharePresenceEnabled) private val isSendPublicReadReceiptsEnabled = MutableStateFlow(isSendPublicReadReceiptsEnabled) + private val isRenderReadReceiptsEnabled = MutableStateFlow(isRenderReadReceiptsEnabled) + private val isSendTypingNotificationsEnabled = MutableStateFlow(isSendTypingNotificationsEnabled) + private val isRenderTypingNotificationsEnabled = MutableStateFlow(isRenderTypingNotificationsEnabled) var clearCallCount = 0 private set + override suspend fun setSharePresence(enabled: Boolean) { + isSharePresenceEnabled.tryEmit(enabled) + } + + override fun isSharePresenceEnabled(): Flow = isSharePresenceEnabled + override suspend fun setSendPublicReadReceipts(enabled: Boolean) { isSendPublicReadReceiptsEnabled.tryEmit(enabled) } - override fun isSendPublicReadReceiptsEnabled(): Flow { - return isSendPublicReadReceiptsEnabled + + override fun isSendPublicReadReceiptsEnabled(): Flow = isSendPublicReadReceiptsEnabled + + override suspend fun setRenderReadReceipts(enabled: Boolean) { + isRenderReadReceiptsEnabled.tryEmit(enabled) + } + + override fun isRenderReadReceiptsEnabled(): Flow = isRenderReadReceiptsEnabled + + override suspend fun setSendTypingNotifications(enabled: Boolean) { + isSendTypingNotificationsEnabled.tryEmit(enabled) + } + + override fun isSendTypingNotificationsEnabled(): Flow = isSendTypingNotificationsEnabled + + override suspend fun setRenderTypingNotifications(enabled: Boolean) { + isRenderTypingNotificationsEnabled.tryEmit(enabled) } + override fun isRenderTypingNotificationsEnabled(): Flow = isRenderTypingNotificationsEnabled + override suspend fun clear() { clearCallCount++ isSendPublicReadReceiptsEnabled.tryEmit(true)