Skip to content

Commit f40e1aa

Browse files
committed
fix: allow multiple datastores
1 parent 785f0dd commit f40e1aa

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

build.gradle.kts

-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ plugins {
66
alias(libs.plugins.nexusPublish)
77
}
88

9-
subprojects {
10-
group = "de.charlex.settings"
11-
version = "2.0.0-alpha01"
12-
}
13-
149
nexusPublishing {
1510
repositories {
1611
sonatype {

sample-shared/src/Screen.kt

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ fun Screen() {
4242
}) {
4343
Text("Set Name to UnknownName")
4444
}
45+
46+
Button({
47+
coroutineScope.launch {
48+
dataStore.clear()
49+
}
50+
}) {
51+
Text("Clear DataStore")
52+
}
4553
}
4654
}
4755
}

settings-datastore/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ plugins {
33
}
44

55
group = "de.charlex.settings"
6-
version = "2.0.0-alpha01"
6+
version = "2.0.0-alpha02"
77
description = "Kotlin Multiplatform Settings Datastore"

settings-datastore/src/SettingsDataStore.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface SettingsDataStore {
2222

2323
companion object {
2424

25-
internal var settingsDataStore: SettingsDataStore? = null
25+
internal val settingsDataStoreMap: MutableMap<String, SettingsDataStore> = mutableMapOf()
2626

2727
/**
2828
* When using with robolectric, please use

settings-datastore/src@android/SettingsDataStore+Ext.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package de.charlex.settings.datastore
22

33
import android.content.Context
44
import androidx.datastore.core.DataMigration
5-
import androidx.datastore.core.MultiProcessDataStoreFactory
65
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
76
import androidx.datastore.preferences.core.Preferences
87
import kotlinx.coroutines.CoroutineScope
@@ -17,8 +16,8 @@ fun SettingsDataStore.Companion.create(
1716
scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
1817
// security: Security = AESSecurity
1918
): SettingsDataStore {
20-
if (settingsDataStore == null) {
21-
settingsDataStore = SettingsDataStoreImpl(
19+
return settingsDataStoreMap.getOrPut(name) {
20+
SettingsDataStoreImpl(
2221
dataStore = createDataStore(
2322
migrations = migrations,
2423
corruptionHandler = corruptionHandler,
@@ -29,6 +28,5 @@ fun SettingsDataStore.Companion.create(
2928
// security = security
3029
)
3130
}
32-
return settingsDataStore!!
3331
}
3432

settings-datastore/src@ios/SettingsDataStore+Ext.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ fun SettingsDataStore.Companion.create(
2121
scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
2222
// security: Security = AESSecurity
2323
): SettingsDataStore {
24-
if (settingsDataStore == null) {
25-
settingsDataStore = SettingsDataStoreImpl(
24+
return settingsDataStoreMap.getOrPut(name) {
25+
SettingsDataStoreImpl(
2626
dataStore = createDataStore(
2727
migrations = migrations,
2828
corruptionHandler = corruptionHandler,
@@ -34,12 +34,11 @@ fun SettingsDataStore.Companion.create(
3434
appropriateForURL = null,
3535
create = false,
3636
error = null,
37-
)
37+
)
3838
requireNotNull(documentDirectory).path + "/$name"
3939
}
4040
// security = security
4141
)
4242
}
43-
return settingsDataStore!!
4443
}
4544

0 commit comments

Comments
 (0)