Skip to content

Commit

Permalink
Add kotlinx.serialization support
Browse files Browse the repository at this point in the history
  • Loading branch information
rumboalla committed Oct 29, 2024
1 parent f8d5a75 commit 3848693
Show file tree
Hide file tree
Showing 17 changed files with 303 additions and 38 deletions.
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ maven { url = uri("https://www.jitpack.io" ) }
```
Import the library
```kotlin
implementation("com.github.rumboalla.kryptostore:core:0.1.2")
implementation("com.github.rumboalla.kryptostore:core:0.1.3")
```
Use preferences
```kotlin
Expand Down Expand Up @@ -57,10 +57,10 @@ suspend fun doSomething(context: Context) {
}
```

## Advanced Usage
## Serialization (Gson)
Import the gson library for serialization
```kotlin
implementation("com.github.rumboalla.kryptostore:gson:0.1.2")
implementation("com.github.rumboalla.kryptostore:gson:0.1.3")
```
Use serialized preferences
```kotlin
Expand All @@ -87,10 +87,40 @@ suspend fun doSomething(context: Context) {
}
```

## Serialization (kotlinx.serialization)
Import the kotlinx.serialization library for serialization
```kotlin
implementation("com.github.rumboalla.kryptostore:kxs:0.1.3")
```
Use serialized preferences
```kotlin
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.github.rumboalla.kryptostore.kxsPref
import kotlinx.serialization.Serializable

private val Context.store: DataStore<Preferences> by preferencesDataStore(name = "prefs")

@Serializable
data class Data(val key: String = "", val value: Double = 0.0)

class Prefs(context: Context) {
val data = kxsPref(context.store, "data", Data(), gson)
}

suspend fun doSomething(context: Context) {
val prefs = Prefs(context)
val data = prefs.data.get()
prefs.data.set(data.copy(key = "key", value = 42.0))
}
```

## Encryption
Import the library for encryption
```kotlin
implementation("com.github.rumboalla.kryptostore:keystore:0.1.2")
implementation("com.github.rumboalla.kryptostore:keystore:0.1.3")
```
Use encrypted preferences
```kotlin
Expand Down Expand Up @@ -120,7 +150,7 @@ suspend fun doSomething(context: Context) {
## Compose
Extensions for compose. Import the library
```kotlin
implementation("com.github.rumboalla.kryptostore:compose:0.1.2")
implementation("com.github.rumboalla.kryptostore:compose:0.1.3")
```
Use it
```kotlin
Expand Down Expand Up @@ -149,7 +179,7 @@ fun Component(prefs: Prefs) {
```

## Roadmap
* More serialization options: Moshi, kotlinx.serialization.
* More serialization options: Moshi.
* More encryption options.

## License
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
plugins {
id("com.android.application") version "8.3.0" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
id("com.android.application") version "8.6.1" apply false
id("org.jetbrains.kotlin.android") version "2.0.20" apply false
}
19 changes: 11 additions & 8 deletions compose/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("org.jetbrains.kotlin.plugin.compose") version "2.0.20"
id("maven-publish")
}

android {
namespace = "com.github.rumboalla.kryptostore.compose"
compileSdk = 34
compileSdk = 35

defaultConfig {
minSdk = 21
Expand All @@ -30,20 +31,22 @@ android {
}

testOptions {
targetSdk = 34
targetSdk = 35
}
}

dependencies {
api(project(":core"))
api("androidx.compose.ui:ui:1.6.4")
api("androidx.lifecycle:lifecycle-runtime-compose:2.7.0")
api("androidx.compose.ui:ui:1.7.4")
api("androidx.lifecycle:lifecycle-runtime-compose:2.8.6")

testImplementation("junit:junit:4.13.2")

androidTestImplementation(project(":core"))
androidTestImplementation("androidx.test:runner:1.5.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test:runner:1.6.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
androidTestImplementation("androidx.compose.ui:ui-test-junit4-android:1.7.4")
androidTestImplementation("androidx.compose.ui:ui-test-manifest:1.7.4")
}

afterEvaluate {
Expand All @@ -52,9 +55,9 @@ afterEvaluate {
create<MavenPublication>("maven") {
groupId = "com.github.rumboalla.kryptostore"
artifactId = "compose"
version = "0.1.2"
version = "0.1.3"
from(components["release"])
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.github.rumboalla.kryptostore.compose

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.github.rumboalla.kryptostore.preference.Preference
import com.github.rumboalla.kryptostore.preference.stringPref
import kotlinx.coroutines.runBlocking
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith


private val Context.store: DataStore<Preferences> by preferencesDataStore(name = "test")

@RunWith(AndroidJUnit4::class)
class KryptoStoreGsonInstrumentedTest {
class KryptoStoreComposeInstrumentedTest {

@get:Rule
val composeTestRule = createComposeRule()

private val context = InstrumentationRegistry.getInstrumentation().targetContext

Expand All @@ -26,7 +34,17 @@ class KryptoStoreGsonInstrumentedTest {

@Test
fun testCompose() = runBlocking {
val pref = stringPref(context.store, "testString", "42")
composeTestRule.setContent {
TestComposable(pref)
}
}

@Composable
fun TestComposable(pref: Preference<String>) {
pref.collectAsStateWithLifecycle().value.let {
println("Value: $it")
}
}

}
14 changes: 7 additions & 7 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ plugins {

android {
namespace = "com.github.rumboalla.kryptostore"
compileSdk = 34
compileSdk = 35

defaultConfig {
minSdk = 16
minSdk = 19
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

Expand All @@ -30,17 +30,17 @@ android {
}

testOptions {
targetSdk = 34
targetSdk = 35
}
}

dependencies {
api("androidx.datastore:datastore-preferences:1.0.0")
api("androidx.datastore:datastore-preferences:1.1.1")

testImplementation("junit:junit:4.13.2")

androidTestImplementation("androidx.test:runner:1.5.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test:runner:1.6.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
}

afterEvaluate {
Expand All @@ -49,7 +49,7 @@ afterEvaluate {
create<MavenPublication>("maven") {
groupId = "com.github.rumboalla.kryptostore"
artifactId = "core"
version = "0.1.2"
version = "0.1.3"
from(components["release"])
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
14 changes: 7 additions & 7 deletions gson/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ plugins {

android {
namespace = "com.github.rumboalla.kryptostore.gson"
compileSdk = 34
compileSdk = 35

defaultConfig {
minSdk = 16
minSdk = 19
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

Expand All @@ -30,19 +30,19 @@ android {
}

testOptions {
targetSdk = 34
targetSdk = 35
}
}

dependencies {
api("com.google.code.gson:gson:2.10.1")
api("com.google.code.gson:gson:2.11.0")
api(project(":core"))

testImplementation("junit:junit:4.13.2")

androidTestImplementation(project(":core"))
androidTestImplementation("androidx.test:runner:1.5.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test:runner:1.6.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
}

afterEvaluate {
Expand All @@ -51,7 +51,7 @@ afterEvaluate {
create<MavenPublication>("maven") {
groupId = "com.github.rumboalla.kryptostore"
artifactId = "gson"
version = "0.1.2"
version = "0.1.3"
from(components["release"])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.github.rumboalla.kryptostore.annotation.DangerousApi
import com.github.rumboalla.kryptostore.createGsonTransform
import com.github.rumboalla.kryptostore.gsonPref
import com.google.gson.Gson
import junit.framework.TestCase.assertEquals
Expand Down
8 changes: 4 additions & 4 deletions keystore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

android {
namespace = "com.github.rumboalla.kryptostore.keystore"
compileSdk = 34
compileSdk = 35

defaultConfig {
minSdk = 23
Expand All @@ -30,7 +30,7 @@ android {
}

testOptions {
targetSdk = 34
targetSdk = 35
}
}

Expand All @@ -40,8 +40,8 @@ dependencies {
testImplementation("junit:junit:4.13.2")

androidTestImplementation(project(":gson"))
androidTestImplementation("androidx.test:runner:1.5.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test:runner:1.6.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
}

afterEvaluate {
Expand Down
1 change: 1 addition & 0 deletions kxs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
60 changes: 60 additions & 0 deletions kxs/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
kotlin("plugin.serialization") version "2.0.20"
id("maven-publish")
}

android {
namespace = "com.github.rumboalla.kryptostore.kxs"
compileSdk = 35

defaultConfig {
minSdk = 19
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}

testOptions {
targetSdk = 35
}
}

dependencies {
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
api(project(":core"))

testImplementation("junit:junit:4.13.2")

androidTestImplementation(project(":core"))
androidTestImplementation("androidx.test:runner:1.5.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
}

afterEvaluate {
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "com.github.rumboalla.kryptostore"
artifactId = "kxs"
version = "0.1.3"
from(components["release"])
}
}
}
}
Empty file added kxs/proguard-rules.pro
Empty file.
Loading

0 comments on commit 3848693

Please sign in to comment.