diff --git a/composeApp/src/androidMain/kotlin/org/ooni/probe/AndroidApplication.kt b/composeApp/src/androidMain/kotlin/org/ooni/probe/AndroidApplication.kt index 1e5f9e0c..1690755c 100644 --- a/composeApp/src/androidMain/kotlin/org/ooni/probe/AndroidApplication.kt +++ b/composeApp/src/androidMain/kotlin/org/ooni/probe/AndroidApplication.kt @@ -47,8 +47,8 @@ class AndroidApplication : Application() { private fun readAssetFile(path: String) = assets.open(path).bufferedReader().use { it.readText() } private fun buildDataStore(): DataStore = - getDataStore( - producePath = { this.filesDir.resolve(DATA_STORE_FILE_NAME).absolutePath }, + Dependencies.getDataStore( + producePath = { this.filesDir.resolve(Dependencies.Companion.DATA_STORE_FILE_NAME).absolutePath }, migrations = listOf(SharedPreferencesMigration(this, "notifications_enabled")), ) } diff --git a/composeApp/src/androidUnitTest/kotlin/org/ooni/testing/CreatePreferenceDataStore.kt b/composeApp/src/androidUnitTest/kotlin/org/ooni/testing/CreatePreferenceDataStore.kt index 900b756b..1a28749e 100644 --- a/composeApp/src/androidUnitTest/kotlin/org/ooni/testing/CreatePreferenceDataStore.kt +++ b/composeApp/src/androidUnitTest/kotlin/org/ooni/testing/CreatePreferenceDataStore.kt @@ -4,12 +4,11 @@ import android.app.Application import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.test.core.app.ApplicationProvider -import org.ooni.probe.DATA_STORE_FILE_NAME -import org.ooni.probe.getDataStore +import org.ooni.probe.di.Dependencies internal actual fun createPreferenceDataStore(): DataStore { val app = ApplicationProvider.getApplicationContext() - return getDataStore( - producePath = { app.filesDir.resolve(DATA_STORE_FILE_NAME).absolutePath }, + return Dependencies.getDataStore( + producePath = { app.filesDir.resolve(Dependencies.Companion.DATA_STORE_FILE_NAME).absolutePath }, ) } diff --git a/composeApp/src/commonMain/kotlin/org/ooni/probe/CreateDataStore.kt b/composeApp/src/commonMain/kotlin/org/ooni/probe/CreateDataStore.kt deleted file mode 100644 index 898edf7b..00000000 --- a/composeApp/src/commonMain/kotlin/org/ooni/probe/CreateDataStore.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.ooni.probe - -import androidx.datastore.core.DataMigration -import androidx.datastore.core.DataStore -import androidx.datastore.preferences.core.PreferenceDataStoreFactory -import androidx.datastore.preferences.core.Preferences -import kotlinx.atomicfu.locks.SynchronizedObject -import kotlinx.atomicfu.locks.synchronized -import okio.Path.Companion.toPath - -private lateinit var dataStore: DataStore - -private val lock = SynchronizedObject() - -/** - * Gets the singleton DataStore instance, creating it if necessary. - */ -fun getDataStore( - producePath: () -> String, - migrations: List> = listOf(), -): DataStore = - synchronized(lock) { - if (::dataStore.isInitialized) { - dataStore - } else { - PreferenceDataStoreFactory.createWithPath( - produceFile = { producePath().toPath() }, - migrations = migrations, - ) - .also { dataStore = it } - } - } - -internal const val DATA_STORE_FILE_NAME = "probe.preferences_pb" diff --git a/composeApp/src/commonMain/kotlin/org/ooni/probe/di/Dependencies.kt b/composeApp/src/commonMain/kotlin/org/ooni/probe/di/Dependencies.kt index 95d71373..ae52779c 100644 --- a/composeApp/src/commonMain/kotlin/org/ooni/probe/di/Dependencies.kt +++ b/composeApp/src/commonMain/kotlin/org/ooni/probe/di/Dependencies.kt @@ -1,12 +1,15 @@ package org.ooni.probe.di import androidx.annotation.VisibleForTesting +import androidx.datastore.core.DataMigration import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.PreferenceDataStoreFactory import androidx.datastore.preferences.core.Preferences import app.cash.sqldelight.db.SqlDriver import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.IO import kotlinx.serialization.json.Json +import okio.Path.Companion.toPath import org.ooni.engine.Engine import org.ooni.engine.NetworkTypeFinder import org.ooni.engine.OonimkallBridge @@ -132,5 +135,26 @@ class Dependencies( @VisibleForTesting fun buildDatabase(driverFactory: () -> SqlDriver): Database = Database(driverFactory()) + + private lateinit var dataStore: DataStore + internal const val DATA_STORE_FILE_NAME = "probe.preferences_pb" + + /** + * Gets the singleton DataStore instance, creating it if necessary. + */ + fun getDataStore( + producePath: () -> String, + migrations: List> = listOf(), + ): DataStore = + if (::dataStore.isInitialized) { + dataStore + } else { + PreferenceDataStoreFactory.createWithPath( + produceFile = { producePath().toPath() }, + migrations = migrations, + ) + .also { dataStore = it } + } + } } diff --git a/composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt b/composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt index b0dfb065..30e8243c 100644 --- a/composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt +++ b/composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt @@ -87,7 +87,7 @@ private class BundleMarker : NSObject() { @OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) fun buildDataStore(): DataStore = - getDataStore( + Dependencies.getDataStore( producePath = { val documentDirectory: NSURL? = NSFileManager.defaultManager.URLForDirectory( @@ -97,6 +97,6 @@ fun buildDataStore(): DataStore = create = false, error = null, ) - requireNotNull(documentDirectory).path + "/$DATA_STORE_FILE_NAME" + requireNotNull(documentDirectory).path + "/${Dependencies.Companion.DATA_STORE_FILE_NAME}" }, ) diff --git a/composeApp/src/iosTest/kotlin/org/ooni/testing/CreatePreferenceDataStore.kt b/composeApp/src/iosTest/kotlin/org/ooni/testing/CreatePreferenceDataStore.kt index 5b17da7b..69e44bfb 100644 --- a/composeApp/src/iosTest/kotlin/org/ooni/testing/CreatePreferenceDataStore.kt +++ b/composeApp/src/iosTest/kotlin/org/ooni/testing/CreatePreferenceDataStore.kt @@ -2,8 +2,7 @@ package org.ooni.testing import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences -import org.ooni.probe.DATA_STORE_FILE_NAME -import org.ooni.probe.getDataStore +import org.ooni.probe.di.Dependencies import platform.Foundation.NSDocumentDirectory import platform.Foundation.NSFileManager import platform.Foundation.NSURL @@ -11,7 +10,7 @@ import platform.Foundation.NSUserDomainMask @OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) internal actual fun createPreferenceDataStore(): DataStore { - return getDataStore( + return Dependencies.getDataStore( producePath = { val documentDirectory: NSURL? = NSFileManager.defaultManager.URLForDirectory( @@ -21,7 +20,7 @@ internal actual fun createPreferenceDataStore(): DataStore { create = false, error = null, ) - requireNotNull(documentDirectory).path + "/$DATA_STORE_FILE_NAME" + requireNotNull(documentDirectory).path + "/${Dependencies.Companion.DATA_STORE_FILE_NAME}" }, ) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f6a25e27..54c31c13 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -66,7 +66,6 @@ ui = [ tooling = [ "kermit", "sqldelight-coroutines", - "kotlinx-atomicfu", "androidx-datastore-core-okio", "androidx-datastore-preferences-core", ]