From 46ccb807b69620ace191d1b04fe04404315619e6 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 21 Aug 2025 11:56:16 +0200 Subject: [PATCH 1/2] Ensure that only one DataStore active for the same file. --- .../impl/store/DefaultPreferencesDataStoreFactory.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libraries/preferences/impl/src/main/kotlin/io/element/android/libraries/preferences/impl/store/DefaultPreferencesDataStoreFactory.kt b/libraries/preferences/impl/src/main/kotlin/io/element/android/libraries/preferences/impl/store/DefaultPreferencesDataStoreFactory.kt index 648f82b9bdb..b2084921505 100644 --- a/libraries/preferences/impl/src/main/kotlin/io/element/android/libraries/preferences/impl/store/DefaultPreferencesDataStoreFactory.kt +++ b/libraries/preferences/impl/src/main/kotlin/io/element/android/libraries/preferences/impl/store/DefaultPreferencesDataStoreFactory.kt @@ -14,18 +14,27 @@ import androidx.datastore.preferences.preferencesDataStore import com.squareup.anvil.annotations.ContributesBinding import io.element.android.libraries.di.AppScope import io.element.android.libraries.di.ApplicationContext +import io.element.android.libraries.di.SingleIn import io.element.android.libraries.preferences.api.store.PreferenceDataStoreFactory +import java.util.concurrent.ConcurrentHashMap import javax.inject.Inject +@SingleIn(AppScope::class) @ContributesBinding(AppScope::class) class DefaultPreferencesDataStoreFactory @Inject constructor( @ApplicationContext private val context: Context, ) : PreferenceDataStoreFactory { + private val dataStoreHolders = ConcurrentHashMap() + private class DataStoreHolder(name: String) { val Context.dataStore: DataStore by preferencesDataStore(name = name) } + override fun create(name: String): DataStore { - return with(DataStoreHolder(name)) { + val holder = dataStoreHolders.getOrPut(name) { + DataStoreHolder(name) + } + return with(holder) { context.dataStore } } From a9c01a8b7e883224aa1c0c3270c5b61914dd3150 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 21 Aug 2025 11:57:56 +0200 Subject: [PATCH 2/2] Now that DefaultPreferencesDataStoreFactory is protected DataStore creation, FullScreenIntentPermissionsPresenter does not need to be a singleton. --- .../impl/FullScreenIntentPermissionsPresenter.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/libraries/fullscreenintent/impl/src/main/kotlin/io/element/android/libraries/fullscreenintent/impl/FullScreenIntentPermissionsPresenter.kt b/libraries/fullscreenintent/impl/src/main/kotlin/io/element/android/libraries/fullscreenintent/impl/FullScreenIntentPermissionsPresenter.kt index 14cc438f41f..ca9810c41dc 100644 --- a/libraries/fullscreenintent/impl/src/main/kotlin/io/element/android/libraries/fullscreenintent/impl/FullScreenIntentPermissionsPresenter.kt +++ b/libraries/fullscreenintent/impl/src/main/kotlin/io/element/android/libraries/fullscreenintent/impl/FullScreenIntentPermissionsPresenter.kt @@ -21,8 +21,6 @@ import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.edit import io.element.android.libraries.architecture.Presenter import io.element.android.libraries.core.meta.BuildMeta -import io.element.android.libraries.di.AppScope -import io.element.android.libraries.di.SingleIn import io.element.android.libraries.fullscreenintent.api.FullScreenIntentPermissionsEvents import io.element.android.libraries.fullscreenintent.api.FullScreenIntentPermissionsState import io.element.android.libraries.preferences.api.store.PreferenceDataStoreFactory @@ -32,7 +30,6 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import javax.inject.Inject -@SingleIn(AppScope::class) class FullScreenIntentPermissionsPresenter @Inject constructor( private val buildVersionSdkIntProvider: BuildVersionSdkIntProvider, private val externalIntentLauncher: ExternalIntentLauncher,