Skip to content

Commit

Permalink
fix: allow multiple datastores
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4rl3x committed Apr 15, 2024
1 parent 785f0dd commit f40e1aa
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
5 changes: 0 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ plugins {
alias(libs.plugins.nexusPublish)
}

subprojects {
group = "de.charlex.settings"
version = "2.0.0-alpha01"
}

nexusPublishing {
repositories {
sonatype {
Expand Down
8 changes: 8 additions & 0 deletions sample-shared/src/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ fun Screen() {
}) {
Text("Set Name to UnknownName")
}

Button({
coroutineScope.launch {
dataStore.clear()
}
}) {
Text("Clear DataStore")
}
}
}
}
2 changes: 1 addition & 1 deletion settings-datastore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ plugins {
}

group = "de.charlex.settings"
version = "2.0.0-alpha01"
version = "2.0.0-alpha02"
description = "Kotlin Multiplatform Settings Datastore"
2 changes: 1 addition & 1 deletion settings-datastore/src/SettingsDataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface SettingsDataStore {

companion object {

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

/**
* When using with robolectric, please use
Expand Down
6 changes: 2 additions & 4 deletions settings-datastore/src@android/SettingsDataStore+Ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package de.charlex.settings.datastore

import android.content.Context
import androidx.datastore.core.DataMigration
import androidx.datastore.core.MultiProcessDataStoreFactory
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.CoroutineScope
Expand All @@ -17,8 +16,8 @@ fun SettingsDataStore.Companion.create(
scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
// security: Security = AESSecurity
): SettingsDataStore {
if (settingsDataStore == null) {
settingsDataStore = SettingsDataStoreImpl(
return settingsDataStoreMap.getOrPut(name) {
SettingsDataStoreImpl(
dataStore = createDataStore(
migrations = migrations,
corruptionHandler = corruptionHandler,
Expand All @@ -29,6 +28,5 @@ fun SettingsDataStore.Companion.create(
// security = security
)
}
return settingsDataStore!!
}

7 changes: 3 additions & 4 deletions settings-datastore/src@ios/SettingsDataStore+Ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ fun SettingsDataStore.Companion.create(
scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
// security: Security = AESSecurity
): SettingsDataStore {
if (settingsDataStore == null) {
settingsDataStore = SettingsDataStoreImpl(
return settingsDataStoreMap.getOrPut(name) {
SettingsDataStoreImpl(
dataStore = createDataStore(
migrations = migrations,
corruptionHandler = corruptionHandler,
Expand All @@ -34,12 +34,11 @@ fun SettingsDataStore.Companion.create(
appropriateForURL = null,
create = false,
error = null,
)
)
requireNotNull(documentDirectory).path + "/$name"
}
// security = security
)
}
return settingsDataStore!!
}

0 comments on commit f40e1aa

Please sign in to comment.