Skip to content

Commit

Permalink
Additions and improvements
Browse files Browse the repository at this point in the history
1. Added stringPreference
2. removed create prefix from shorthands.
3. Updated dependencies.
  • Loading branch information
07jasjeet committed Mar 8, 2024
1 parent f57d3c0 commit 6acf402
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ class MyAutoTypedPreferences(context: Context): AutoTypedDataStore(context.dataS
}

val listPref: ComplexPreference<List<String>>
get() = createListPreference(key)
get() = listPreference(key)

// or

val mapPref: ComplexPreference<Map<String, List<String>>>
get() = createMapPreference(key)
get() = mapPreference(key)

// ... and many other pre-defined preferences
}
Expand All @@ -81,7 +81,7 @@ You can also add custom preferences without any boilerplate as follows:
val key = stringPreferencesKey("key")

val customPref: ComplexPreference<SomeClass>
get() = createCustomPreference(key, defaultValue)
get() = customPreference(key, defaultValue)
```

To use these preferences, simply do as follows:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.jasjeet.typesafe_datastore_app.DataStoreSerializers.stringListSerializer
import com.jasjeet.typesafe_datastore.TypeSafeDataStore
import com.jasjeet.typesafe_datastore.preferences.ComplexPreference
import com.jasjeet.typesafe_datastore_app.DataStoreSerializers.stringListSerializer
import com.jasjeet.typesafe_datastore_gson.AutoTypedDataStore

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore("settings")
Expand All @@ -28,6 +28,6 @@ class MyAutoTypedPreferences(context: Context): AutoTypedDataStore(context.dataS
}

val listPreference: ComplexPreference<List<String>>
get() = createListPreference(key)
get() = listPreference(key)

}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath("com.android.tools.build:gradle:8.2.1")
classpath("com.android.tools.build:gradle:8.2.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${BuildVersions.kotlinVersion}")
}
}
Expand Down
9 changes: 8 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
mavenCentralUsername=jasje
mavenCentralPassword=kLiq6zQCc&$22$X

signing.keyId=0DC7F540
#CEF16760B38C1FB2A72B558C41A81EC6F99E2B1D
signing.password=Valo@2020
signing.secretKeyRingFile=/secring.gpg
2 changes: 1 addition & 1 deletion typesafe-datastore-gson/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ publishing {
create<MavenPublication>("release") {
groupId = "com.github.07jasjeet"
artifactId = "typesafe-datastore-gson"
version = "1.0.0"
version = "1.0.2"

afterEvaluate {
from(components["release"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,80 +22,86 @@ import com.jasjeet.typesafe_datastore.preferences.PrimitivePreference
* val key = stringPreferencesKey("my-key")
*
* val preference: ComplexPreference<List<String>>
* get() = createListPreference(key)
* get() = listPreference(key)
* ```
* To create custom preferences, use [createCustomPreference] and if you need more customisability, see [TypeSafeDataStore].
* To create custom preferences, use [customPreference] and if you need more customisability, see [TypeSafeDataStore].
* @see TypeSafeDataStore
*/
abstract class AutoTypedDataStore(dataStore: DataStore<Preferences>): TypeSafeDataStore(dataStore){
/** Short-hand option to create [Boolean] preference.*/
protected fun createBooleanPreference(
protected fun booleanPreference(
key: Preferences.Key<Boolean>,
default: Boolean = false
): PrimitivePreference<Boolean> = createPrimitivePreference(key, default)

/** Short-hand option to create [Int] preference.*/
protected fun createIntPreference(
protected fun intPreference(
key: Preferences.Key<Int>,
default: Int = 0
): PrimitivePreference<Int> = createPrimitivePreference(key, default)

/** Short-hand option to create [Boolean] preference.*/
protected fun createLongPreference(
protected fun longPreference(
key: Preferences.Key<Long>,
default: Long = 0
): PrimitivePreference<Long> = createPrimitivePreference(key, default)

/** Short-hand option to create [Float] preference.*/
protected fun createFloatPreference(
protected fun floatPreference(
key: Preferences.Key<Float>,
default: Float = 0.0f
): PrimitivePreference<Float> = createPrimitivePreference(key, default)

/** Short-hand option to create [Double] preference.*/
protected fun createDoublePreference(
protected fun doublePreference(
key: Preferences.Key<Double>,
default: Double = 0.0
): PrimitivePreference<Double> = createPrimitivePreference(key, default)

/** Short-hand option to create [String] preference.*/
protected fun stringPreference(
key: Preferences.Key<String>,
default: String = ""
): PrimitivePreference<String> = createPrimitivePreference(key, default)

/** Short-hand option to create `Set<String>` preference.*/
protected fun createStringSetPreference(
protected fun stringSetPreference(
key: Preferences.Key<Set<String>>,
defaultValue: Set<String>
): PrimitivePreference<Set<String>> = createPrimitivePreference(key, defaultValue)

/** Gson-backed short-hand option to create [List] preferences.*/
protected fun <T> createListPreference(
protected fun <T> listPreference(
key: Preferences.Key<String>,
defaultValue: List<T> = emptyList()
): ComplexPreference<List<T>> = createComplexPreference(key, listSerializer(defaultValue))

/** Gson-backed short-hand option to create [Map] preferences.*/
protected fun <K, V> createMapPreference(
protected fun <K, V> mapPreference(
key: Preferences.Key<String>,
defaultValue: Map<K, V> = emptyMap()
): ComplexPreference<Map<K, V>> = createComplexPreference(key, mapSerializer(defaultValue))

/** Gson-backed short-hand option to create [Array] preferences.*/
protected inline fun <reified T> createArrayPreference(
protected inline fun <reified T> arrayPreference(
key: Preferences.Key<String>,
defaultValue: Array<T> = emptyArray()
): ComplexPreference<Array<T>> = createComplexPreference(key, arraySerializer(defaultValue))

/** Gson-backed short-hand option to create [ArrayList] preferences.*/
protected fun <T> createArrayListPreference(
protected fun <T> arrayListPreference(
key: Preferences.Key<String>,
defaultValue: ArrayList<T> = arrayListOf()
): ComplexPreference<ArrayList<T>> = createComplexPreference(key, arrayListSerializer(defaultValue))

/** Gson-backed short-hand option to create [Set] preferences.*/
protected fun <T> createSetPreference(
protected fun <T> setPreference(
key: Preferences.Key<String>,
defaultValue: Set<T> = setOf()
): ComplexPreference<Set<T>> = createComplexPreference(key, setSerializer(defaultValue))

/** Gson-backed short-hand option to create [Class] preferences.*/
protected fun <T> createCustomPreference(
protected fun <T> customPreference(
key: Preferences.Key<String>,
defaultValue: T
): ComplexPreference<T> = createComplexPreference(key, gsonSerializer(defaultValue))
Expand Down
4 changes: 2 additions & 2 deletions typesafe-datastore-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ android {

dependencies {
implementation(project(":typesafe-datastore"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
}

publishing {
publications {
create<MavenPublication>("release") {
groupId = "com.github.07jasjeet"
artifactId = "typesafe-datastore-test"
version = "1.0.0"
version = "1.0.2"

afterEvaluate {
from(components["release"])
Expand Down
2 changes: 1 addition & 1 deletion typesafe-datastore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ publishing {
create<MavenPublication>("release") {
groupId = "com.github.07jasjeet"
artifactId = "typesafe-datastore"
version = "1.0.0"
version = "1.0.2"

afterEvaluate {
from(components["release"])
Expand Down

0 comments on commit 6acf402

Please sign in to comment.