-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from joreilly/datastore
migrate to using datastore directly for all platforms
- Loading branch information
Showing
9 changed files
with
74 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions
14
common/src/androidMain/kotlin/dev/johnoreilly/common/actual.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
package dev.johnoreilly.common | ||
|
||
import android.content.Context | ||
import com.russhwolf.settings.ObservableSettings | ||
import com.russhwolf.settings.SharedPreferencesSettings | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import io.ktor.client.engine.android.* | ||
import org.koin.dsl.module | ||
|
||
actual fun platformModule() = module { | ||
single { Android.create() } | ||
single<ObservableSettings> { createObservableSettings(get()) } | ||
single { dataStore(get())} | ||
} | ||
|
||
|
||
private fun createObservableSettings(context: Context): ObservableSettings { | ||
return SharedPreferencesSettings(context.getSharedPreferences("AppSettings", Context.MODE_PRIVATE)) | ||
} | ||
fun dataStore(context: Context): DataStore<Preferences> = | ||
createDataStore( | ||
producePath = { context.filesDir.resolve("fpl.preferences_pb").absolutePath } | ||
) |
34 changes: 25 additions & 9 deletions
34
common/src/commonMain/kotlin/dev/johnoreilly/common/AppSettings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,43 @@ | ||
package dev.johnoreilly.common | ||
|
||
import com.russhwolf.settings.ExperimentalSettingsApi | ||
import com.russhwolf.settings.ObservableSettings | ||
import com.russhwolf.settings.coroutines.getStringOrNullFlow | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import okio.Path.Companion.toPath | ||
|
||
@OptIn(ExperimentalSettingsApi::class) | ||
class AppSettings(val settings: ObservableSettings) { | ||
|
||
fun createDataStore( | ||
producePath: () -> String, | ||
): DataStore<Preferences> = PreferenceDataStoreFactory.createWithPath( | ||
corruptionHandler = null, | ||
migrations = emptyList(), | ||
produceFile = { producePath().toPath() }, | ||
) | ||
|
||
class AppSettings(private val dataStore: DataStore<Preferences>) { | ||
|
||
val leagues: Flow<List<String>> = | ||
settings.getStringOrNullFlow(LEAGUES_SETTING).map { getLeaguesSettingFromString(it) } | ||
dataStore.data | ||
.map { preferences -> | ||
getLeaguesSettingFromString(preferences[LEAGUES_SETTING]) | ||
} | ||
|
||
|
||
fun updatesLeaguesSetting(leagues: List<String>) { | ||
settings.putString(LEAGUES_SETTING, leagues.joinToString(separator = ",")) | ||
suspend fun updatesLeaguesSetting(leagues: List<String>) { | ||
dataStore.edit { preferences -> | ||
preferences[LEAGUES_SETTING] = leagues.joinToString(separator = ",") | ||
} | ||
} | ||
|
||
|
||
private fun getLeaguesSettingFromString(settingsString: String?) = | ||
settingsString?.split(",") ?: emptyList() | ||
|
||
companion object { | ||
const val LEAGUES_SETTING = "leagues" | ||
val LEAGUES_SETTING = stringPreferencesKey("leagues") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 19 additions & 5 deletions
24
common/src/iOSMain/kotlin/dev/johnoreilly/common/actual.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
package dev.johnoreilly.common | ||
|
||
import com.russhwolf.settings.NSUserDefaultsSettings | ||
import com.russhwolf.settings.ObservableSettings | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import io.ktor.client.engine.darwin.* | ||
import org.koin.dsl.module | ||
import platform.Foundation.NSUserDefaults | ||
import platform.Foundation.NSDocumentDirectory | ||
import platform.Foundation.NSFileManager | ||
import platform.Foundation.NSURL | ||
import platform.Foundation.NSUserDomainMask | ||
|
||
|
||
actual fun platformModule() = module { | ||
single { Darwin.create() } | ||
single<ObservableSettings> { NSUserDefaultsSettings(NSUserDefaults.standardUserDefaults) } | ||
single { dataStore()} | ||
} | ||
|
||
|
||
|
||
fun dataStore(): DataStore<Preferences> = createDataStore( | ||
producePath = { | ||
val documentDirectory: NSURL? = NSFileManager.defaultManager.URLForDirectory( | ||
directory = NSDocumentDirectory, | ||
inDomain = NSUserDomainMask, | ||
appropriateForURL = null, | ||
create = false, | ||
error = null, | ||
) | ||
requireNotNull(documentDirectory).path + "/fpl.preferences_pb" | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
package dev.johnoreilly.common | ||
|
||
import com.russhwolf.settings.PreferencesSettings | ||
import com.russhwolf.settings.ObservableSettings | ||
import androidx.datastore.core.DataStore | ||
import io.ktor.client.engine.java.* | ||
import org.koin.dsl.module | ||
import java.util.prefs.Preferences | ||
|
||
|
||
actual fun platformModule() = module { | ||
single { Java.create() } | ||
single<ObservableSettings> { PreferencesSettings(Preferences.userRoot()) } | ||
single { dataStore()} | ||
} | ||
|
||
fun dataStore(): DataStore<androidx.datastore.preferences.core.Preferences> = | ||
createDataStore( | ||
producePath = { "fpl.preferences_pb" } | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters