From 365502ac9669fcd9fa687d10810fa434ef066e89 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 5 Feb 2024 12:29:36 +0100 Subject: [PATCH] Avoid computing default value if it's not necessary. --- .../impl/store/DefaultSessionPreferencesStore.kt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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 b6283d2c69..770ca699fe 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 @@ -66,21 +66,20 @@ class DefaultSessionPreferencesStore( override fun isSharePresenceEnabled(): Flow { // Migration, if sendPublicReadReceiptsKey was false, consider that sharing presence is false. - val defaultValue = runBlocking { isSendPublicReadReceiptsEnabled().first() } - return get(sharePresenceKey, defaultValue) + return get(sharePresenceKey) { runBlocking { isSendPublicReadReceiptsEnabled().first() } } } override suspend fun setSendPublicReadReceipts(enabled: Boolean) = update(sendPublicReadReceiptsKey, enabled) - override fun isSendPublicReadReceiptsEnabled(): Flow = get(sendPublicReadReceiptsKey, true) + override fun isSendPublicReadReceiptsEnabled(): Flow = get(sendPublicReadReceiptsKey) { true } override suspend fun setRenderReadReceipts(enabled: Boolean) = update(renderReadReceiptsKey, enabled) - override fun isRenderReadReceiptsEnabled(): Flow = get(renderReadReceiptsKey, true) + override fun isRenderReadReceiptsEnabled(): Flow = get(renderReadReceiptsKey) { true } override suspend fun setSendTypingNotifications(enabled: Boolean) = update(sendTypingNotificationsKey, enabled) - override fun isSendTypingNotificationsEnabled(): Flow = get(sendTypingNotificationsKey, true) + override fun isSendTypingNotificationsEnabled(): Flow = get(sendTypingNotificationsKey) { true } override suspend fun setRenderTypingNotifications(enabled: Boolean) = update(renderTypingNotificationsKey, enabled) - override fun isRenderTypingNotificationsEnabled(): Flow = get(renderTypingNotificationsKey, true) + override fun isRenderTypingNotificationsEnabled(): Flow = get(renderTypingNotificationsKey) { true } override suspend fun clear() { dataStoreFile.safeDelete() @@ -90,7 +89,7 @@ class DefaultSessionPreferencesStore( store.edit { prefs -> prefs[key] = value } } - private fun get(key: Preferences.Key, default: T): Flow { - return store.data.map { prefs -> prefs[key] ?: default } + private fun get(key: Preferences.Key, default: () -> T): Flow { + return store.data.map { prefs -> prefs[key] ?: default() } } }