Skip to content

Commit

Permalink
Merge pull request #219 from joreilly/datastore
Browse files Browse the repository at this point in the history
migrate to using datastore directly for all platforms
  • Loading branch information
joreilly authored Apr 18, 2024
2 parents 7a8116c + 4b75b43 commit 3d73617
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ class FantasyPremierLeagueViewModel(
}

fun updateLeagues(leagues: List<String>) {
repository.updateLeagues(leagues)
viewModelScope.launch {
repository.updateLeagues(leagues)
}
}

}
2 changes: 1 addition & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ kotlin {
implementation(libs.realm)
implementation(libs.androidx.lifecycle.viewmodel)

api(libs.bundles.multiplatformSettings)
implementation(libs.datastore.preferences)
api(libs.kermit)

implementation(compose.ui)
Expand Down
14 changes: 7 additions & 7 deletions common/src/androidMain/kotlin/dev/johnoreilly/common/actual.kt
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 common/src/commonMain/kotlin/dev/johnoreilly/common/AppSettings.kt
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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class FantasyPremierLeagueRepository : KoinComponent {
return fantasyPremierLeagueApi.fetchEventStatus()
}

fun updateLeagues(leagues: List<String>) {
suspend fun updateLeagues(leagues: List<String>) {
appSettings.updatesLeaguesSetting(leagues)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

Expand All @@ -23,7 +24,9 @@ open class LeaguesViewModel : ViewModel(), KoinComponent {
private val _isRefreshing = MutableStateFlow(false)

fun updateLeagues(leagues: List<String>) {
repository.updateLeagues(leagues)
viewModelScope.launch {
repository.updateLeagues(leagues)
}
}

suspend fun getEventStatus(): List<EventStatusDto> {
Expand Down
24 changes: 19 additions & 5 deletions common/src/iOSMain/kotlin/dev/johnoreilly/common/actual.kt
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"
}
)
10 changes: 6 additions & 4 deletions common/src/jvmMain/kotlin/dev/johnoreilly/common/actual.kt
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" }
)
17 changes: 8 additions & 9 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ ktor = "2.3.9"
slf4j = "2.0.12"
realm = "1.13.0"
skie = "0.6.2"
datastore = "1.1.0"

androidxActivity = "1.9.0-beta01"
androidxActivity = "1.9.0-rc01"
androidxComposeCompiler = "1.5.11-dev-k1.9.23-96ef9dc6af1"
jbComposeCompiler = "1.5.8"
androidxComposeBom = "2024.02.02"
androidxNavigationCompose = "2.8.0-alpha05"
androidxLifecycle = "2.8.0-alpha03"
androidxComposeBom = "2024.04.01"
androidxNavigationCompose = "2.8.0-alpha07"
androidxLifecycle = "2.8.0-beta01"

composeMultiplatform = "1.6.1"
accompanist = "0.32.0"
Expand All @@ -28,7 +29,6 @@ image-loader = "1.7.8"

junit = "4.13.2"

multiplatformSettings = "1.1.1"
kermit = "2.0.3"

minSdk = "24"
Expand Down Expand Up @@ -61,6 +61,8 @@ androidx-navigation-compose = { module = "androidx.navigation:navigation-compose
androidx-lifecycle-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidxLifecycle" }
androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel", version.ref = "androidxLifecycle" }

datastore-preferences = { module = "androidx.datastore:datastore-preferences-core", version.ref = "datastore" }

coilCompose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coilCompose" }
accompanist-placeholder = { group = "com.google.accompanist", name = "accompanist-placeholder-material3", version.ref = "accompanist" }
image-loader = { module = "io.github.qdsfdhvh:image-loader", version.ref = "image-loader" }
Expand All @@ -85,16 +87,13 @@ slf4j = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j" }

realm = { group = "io.realm.kotlin", name = "library-base", version.ref = "realm" }

multiplatform-settings = { module = "com.russhwolf:multiplatform-settings", version.ref = "multiplatformSettings" }
multiplatform-settings-coroutines = { module = "com.russhwolf:multiplatform-settings-coroutines", version.ref = "multiplatformSettings" }
kermit = { module = "co.touchlab:kermit", version.ref = "kermit" }
junit = { module = "junit:junit", version.ref = "junit" }
androidx-datastore-preferences-core = { group = "androidx.datastore", name = "datastore-preferences-core", version.ref = "datastore" }

[bundles]
multiplatformSettings = ["multiplatform-settings", "multiplatform-settings-coroutines"]
ktor-common = ["ktor-client-core", "ktor-client-json", "ktor-client-logging", "ktor-client-serialization", "ktor-client-content-negotiation", "ktor-serialization-kotlinx-json"]


[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
Expand Down

0 comments on commit 3d73617

Please sign in to comment.