Merge pull request #2349 from element-hq/feature/bma/disableTyping
"Share presence" setting
This commit is contained in:
commit
022d309eb6
35 changed files with 197 additions and 58 deletions
|
|
@ -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<Boolean>
|
||||
|
||||
suspend fun setSendPublicReadReceipts(enabled: Boolean)
|
||||
fun isSendPublicReadReceiptsEnabled(): Flow<Boolean>
|
||||
|
||||
suspend fun setRenderReadReceipts(enabled: Boolean)
|
||||
fun isRenderReadReceiptsEnabled(): Flow<Boolean>
|
||||
|
||||
suspend fun setSendTypingNotifications(enabled: Boolean)
|
||||
fun isSendTypingNotificationsEnabled(): Flow<Boolean>
|
||||
|
||||
suspend fun setRenderTypingNotifications(enabled: Boolean)
|
||||
fun isRenderTypingNotificationsEnabled(): Flow<Boolean>
|
||||
|
||||
suspend fun clear()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,13 +45,41 @@ 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<Boolean> {
|
||||
// Migration, if sendPublicReadReceiptsKey was false, consider that sharing presence is false.
|
||||
return get(sharePresenceKey) { runBlocking { isSendPublicReadReceiptsEnabled().first() } }
|
||||
}
|
||||
|
||||
override suspend fun setSendPublicReadReceipts(enabled: Boolean) = update(sendPublicReadReceiptsKey, enabled)
|
||||
override fun isSendPublicReadReceiptsEnabled(): Flow<Boolean> = get(sendPublicReadReceiptsKey, true)
|
||||
override fun isSendPublicReadReceiptsEnabled(): Flow<Boolean> = get(sendPublicReadReceiptsKey) { true }
|
||||
|
||||
override suspend fun setRenderReadReceipts(enabled: Boolean) = update(renderReadReceiptsKey, enabled)
|
||||
override fun isRenderReadReceiptsEnabled(): Flow<Boolean> = get(renderReadReceiptsKey) { true }
|
||||
|
||||
override suspend fun setSendTypingNotifications(enabled: Boolean) = update(sendTypingNotificationsKey, enabled)
|
||||
override fun isSendTypingNotificationsEnabled(): Flow<Boolean> = get(sendTypingNotificationsKey) { true }
|
||||
|
||||
override suspend fun setRenderTypingNotifications(enabled: Boolean) = update(renderTypingNotificationsKey, enabled)
|
||||
override fun isRenderTypingNotificationsEnabled(): Flow<Boolean> = get(renderTypingNotificationsKey) { true }
|
||||
|
||||
override suspend fun clear() {
|
||||
dataStoreFile.safeDelete()
|
||||
|
|
@ -59,7 +89,7 @@ class DefaultSessionPreferencesStore(
|
|||
store.edit { prefs -> prefs[key] = value }
|
||||
}
|
||||
|
||||
private fun <T> get(key: Preferences.Key<T>, default: T): Flow<T> {
|
||||
return store.data.map { prefs -> prefs[key] ?: default }
|
||||
private fun <T> get(key: Preferences.Key<T>, default: () -> T): Flow<T> {
|
||||
return store.data.map { prefs -> prefs[key] ?: default() }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Boolean> = isSharePresenceEnabled
|
||||
|
||||
override suspend fun setSendPublicReadReceipts(enabled: Boolean) {
|
||||
isSendPublicReadReceiptsEnabled.tryEmit(enabled)
|
||||
}
|
||||
override fun isSendPublicReadReceiptsEnabled(): Flow<Boolean> {
|
||||
return isSendPublicReadReceiptsEnabled
|
||||
|
||||
override fun isSendPublicReadReceiptsEnabled(): Flow<Boolean> = isSendPublicReadReceiptsEnabled
|
||||
|
||||
override suspend fun setRenderReadReceipts(enabled: Boolean) {
|
||||
isRenderReadReceiptsEnabled.tryEmit(enabled)
|
||||
}
|
||||
|
||||
override fun isRenderReadReceiptsEnabled(): Flow<Boolean> = isRenderReadReceiptsEnabled
|
||||
|
||||
override suspend fun setSendTypingNotifications(enabled: Boolean) {
|
||||
isSendTypingNotificationsEnabled.tryEmit(enabled)
|
||||
}
|
||||
|
||||
override fun isSendTypingNotificationsEnabled(): Flow<Boolean> = isSendTypingNotificationsEnabled
|
||||
|
||||
override suspend fun setRenderTypingNotifications(enabled: Boolean) {
|
||||
isRenderTypingNotificationsEnabled.tryEmit(enabled)
|
||||
}
|
||||
|
||||
override fun isRenderTypingNotificationsEnabled(): Flow<Boolean> = isRenderTypingNotificationsEnabled
|
||||
|
||||
override suspend fun clear() {
|
||||
clearCallCount++
|
||||
isSendPublicReadReceiptsEnabled.tryEmit(true)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue