Skip to content

Commit f8d5a75

Browse files
committed
Compose Helper
* Add compose helper library. * Add setBlocking to Preference. * Add defaultValue() to Preference.
1 parent f53fa12 commit f8d5a75

File tree

14 files changed

+162
-6
lines changed

14 files changed

+162
-6
lines changed

README.md

+34-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ maven { url = uri("https://www.jitpack.io" ) }
1414
```
1515
Import the library
1616
```kotlin
17-
implementation("com.github.rumboalla.kryptostore:core:0.1.1")
17+
implementation("com.github.rumboalla.kryptostore:core:0.1.2")
1818
```
1919
Use preferences
2020
```kotlin
@@ -60,7 +60,7 @@ suspend fun doSomething(context: Context) {
6060
## Advanced Usage
6161
Import the gson library for serialization
6262
```kotlin
63-
implementation("com.github.rumboalla.kryptostore:gson:0.1.1")
63+
implementation("com.github.rumboalla.kryptostore:gson:0.1.2")
6464
```
6565
Use serialized preferences
6666
```kotlin
@@ -90,7 +90,7 @@ suspend fun doSomething(context: Context) {
9090
## Encryption
9191
Import the library for encryption
9292
```kotlin
93-
implementation("com.github.rumboalla.kryptostore:keystore:0.1.1")
93+
implementation("com.github.rumboalla.kryptostore:keystore:0.1.2")
9494
```
9595
Use encrypted preferences
9696
```kotlin
@@ -117,6 +117,37 @@ suspend fun doSomething(context: Context) {
117117
}
118118
```
119119

120+
## Compose
121+
Extensions for compose. Import the library
122+
```kotlin
123+
implementation("com.github.rumboalla.kryptostore:compose:0.1.2")
124+
```
125+
Use it
126+
```kotlin
127+
import android.content.Context
128+
import androidx.compose.runtime.Composable
129+
import androidx.datastore.core.DataStore
130+
import androidx.datastore.preferences.core.Preferences
131+
import androidx.datastore.preferences.preferencesDataStore
132+
import com.github.rumboalla.kryptostore.preference.booleanPref
133+
134+
private val Context.store: DataStore<Preferences> by preferencesDataStore(name = "prefs")
135+
136+
class Prefs(context: Context) {
137+
val boolean = booleanPref(context.store, "boolean", true)
138+
}
139+
140+
@Composable
141+
fun Component(prefs: Prefs) {
142+
val state = prefs.boolean.collectAsStateWithLifecycle()
143+
if (state.value) {
144+
Text("Pref is true.")
145+
} else {
146+
Text("Pref is false.")
147+
}
148+
}
149+
```
150+
120151
## Roadmap
121152
* More serialization options: Moshi, kotlinx.serialization.
122153
* More encryption options.

compose/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

compose/build.gradle.kts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
plugins {
2+
id("com.android.library")
3+
id("org.jetbrains.kotlin.android")
4+
id("maven-publish")
5+
}
6+
7+
android {
8+
namespace = "com.github.rumboalla.kryptostore.compose"
9+
compileSdk = 34
10+
11+
defaultConfig {
12+
minSdk = 21
13+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
14+
}
15+
16+
buildTypes {
17+
release {
18+
isMinifyEnabled = false
19+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
20+
}
21+
}
22+
23+
compileOptions {
24+
sourceCompatibility = JavaVersion.VERSION_1_8
25+
targetCompatibility = JavaVersion.VERSION_1_8
26+
}
27+
28+
kotlinOptions {
29+
jvmTarget = "1.8"
30+
}
31+
32+
testOptions {
33+
targetSdk = 34
34+
}
35+
}
36+
37+
dependencies {
38+
api(project(":core"))
39+
api("androidx.compose.ui:ui:1.6.4")
40+
api("androidx.lifecycle:lifecycle-runtime-compose:2.7.0")
41+
42+
testImplementation("junit:junit:4.13.2")
43+
44+
androidTestImplementation(project(":core"))
45+
androidTestImplementation("androidx.test:runner:1.5.2")
46+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
47+
}
48+
49+
afterEvaluate {
50+
publishing {
51+
publications {
52+
create<MavenPublication>("maven") {
53+
groupId = "com.github.rumboalla.kryptostore"
54+
artifactId = "compose"
55+
version = "0.1.2"
56+
from(components["release"])
57+
}
58+
}
59+
}
60+
}

compose/proguard-rules.pro

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.rumboalla.kryptostore.compose
2+
3+
import android.content.Context
4+
import androidx.datastore.core.DataStore
5+
import androidx.datastore.preferences.core.Preferences
6+
import androidx.datastore.preferences.core.edit
7+
import androidx.datastore.preferences.preferencesDataStore
8+
import androidx.test.ext.junit.runners.AndroidJUnit4
9+
import androidx.test.platform.app.InstrumentationRegistry
10+
import kotlinx.coroutines.runBlocking
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
14+
15+
private val Context.store: DataStore<Preferences> by preferencesDataStore(name = "test")
16+
17+
@RunWith(AndroidJUnit4::class)
18+
class KryptoStoreGsonInstrumentedTest {
19+
20+
private val context = InstrumentationRegistry.getInstrumentation().targetContext
21+
22+
init {
23+
// Clears the datastore
24+
runBlocking { context.store.edit { it.clear() } }
25+
}
26+
27+
@Test
28+
fun testCompose() = runBlocking {
29+
30+
}
31+
32+
}

compose/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.rumboalla.kryptostore.compose
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.State
5+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
6+
import com.github.rumboalla.kryptostore.preference.Preference
7+
8+
9+
@Composable
10+
fun <T> Preference<T>.collectAsStateWithLifecycle(): State<T> {
11+
return this.flow().collectAsStateWithLifecycle(defaultValue())
12+
}

core/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ afterEvaluate {
4949
create<MavenPublication>("maven") {
5050
groupId = "com.github.rumboalla.kryptostore"
5151
artifactId = "core"
52-
version = "0.1.1"
52+
version = "0.1.2"
5353
from(components["release"])
5454
}
5555
}

core/src/main/kotlin/com/github/rumboalla/kryptostore/preference/GenericPreference.kt

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ open class GenericPreference<T>(
2323
override suspend fun get() = flow.first()
2424
override suspend fun set(v: T) { store.edit { it[key] = transform.transform(v) } }
2525
override fun flow() = flow
26+
override fun defaultValue(): T = defValue
2627
}

core/src/main/kotlin/com/github/rumboalla/kryptostore/preference/Preference.kt

+15
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,19 @@ interface Preference<T> {
3535
*/
3636
@DangerousApi
3737
fun getBlocking() = runBlocking { get() }
38+
39+
/**
40+
* Sets the value of a preference. This call blocks until the preference is set.
41+
*
42+
* @param v {@T} Value {@T} to set in the preference.
43+
*/
44+
@DangerousApi
45+
fun setBlocking(v: T) = runBlocking { set(v) }
46+
47+
/**
48+
* Gets the default value of a preference.
49+
*
50+
* @return {@T} Value of the preference.
51+
*/
52+
fun defaultValue(): T
3853
}

core/src/main/kotlin/com/github/rumboalla/kryptostore/preference/PrimitivePreference.kt

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ open class PrimitivePreference<T>(
1717
override suspend fun get() = flow.first()
1818
override suspend fun set(v: T) { store.edit { it[key] = v } }
1919
override fun flow() = flow
20+
override fun defaultValue(): T = defValue
2021
}

gson/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ afterEvaluate {
5151
create<MavenPublication>("maven") {
5252
groupId = "com.github.rumboalla.kryptostore"
5353
artifactId = "gson"
54-
version = "0.1.1"
54+
version = "0.1.2"
5555
from(components["release"])
5656
}
5757
}

keystore/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ afterEvaluate {
5050
create<MavenPublication>("maven") {
5151
groupId = "com.github.rumboalla.kryptostore"
5252
artifactId = "keystore"
53-
version = "0.1.1"
53+
version = "0.1.2"
5454
from(components["release"])
5555
}
5656
}

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ rootProject.name = "KryptoStore"
1919
include(":core")
2020
include(":gson")
2121
include(":keystore")
22+
include(":compose")

0 commit comments

Comments
 (0)