Skip to content

Commit

Permalink
Compose Helper
Browse files Browse the repository at this point in the history
* Add compose helper library.
* Add setBlocking to Preference.
* Add defaultValue() to Preference.
  • Loading branch information
rumboalla committed Mar 24, 2024
1 parent f53fa12 commit f8d5a75
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 6 deletions.
37 changes: 34 additions & 3 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.1")
implementation("com.github.rumboalla.kryptostore:core:0.1.2")
```
Use preferences
```kotlin
Expand Down Expand Up @@ -60,7 +60,7 @@ suspend fun doSomething(context: Context) {
## Advanced Usage
Import the gson library for serialization
```kotlin
implementation("com.github.rumboalla.kryptostore:gson:0.1.1")
implementation("com.github.rumboalla.kryptostore:gson:0.1.2")
```
Use serialized preferences
```kotlin
Expand Down Expand Up @@ -90,7 +90,7 @@ suspend fun doSomething(context: Context) {
## Encryption
Import the library for encryption
```kotlin
implementation("com.github.rumboalla.kryptostore:keystore:0.1.1")
implementation("com.github.rumboalla.kryptostore:keystore:0.1.2")
```
Use encrypted preferences
```kotlin
Expand All @@ -117,6 +117,37 @@ suspend fun doSomething(context: Context) {
}
```

## Compose
Extensions for compose. Import the library
```kotlin
implementation("com.github.rumboalla.kryptostore:compose:0.1.2")
```
Use it
```kotlin
import android.content.Context
import androidx.compose.runtime.Composable
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.github.rumboalla.kryptostore.preference.booleanPref

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

class Prefs(context: Context) {
val boolean = booleanPref(context.store, "boolean", true)
}

@Composable
fun Component(prefs: Prefs) {
val state = prefs.boolean.collectAsStateWithLifecycle()
if (state.value) {
Text("Pref is true.")
} else {
Text("Pref is false.")
}
}
```

## Roadmap
* More serialization options: Moshi, kotlinx.serialization.
* More encryption options.
Expand Down
1 change: 1 addition & 0 deletions compose/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
60 changes: 60 additions & 0 deletions compose/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")
id("maven-publish")
}

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

defaultConfig {
minSdk = 21
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 = 34
}
}

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

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 = "compose"
version = "0.1.2"
from(components["release"])
}
}
}
}
Empty file added compose/proguard-rules.pro
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.rumboalla.kryptostore.compose

import android.content.Context
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 kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.runner.RunWith


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

@RunWith(AndroidJUnit4::class)
class KryptoStoreGsonInstrumentedTest {

private val context = InstrumentationRegistry.getInstrumentation().targetContext

init {
// Clears the datastore
runBlocking { context.store.edit { it.clear() } }
}

@Test
fun testCompose() = runBlocking {

}

}
2 changes: 2 additions & 0 deletions compose/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.rumboalla.kryptostore.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.github.rumboalla.kryptostore.preference.Preference


@Composable
fun <T> Preference<T>.collectAsStateWithLifecycle(): State<T> {
return this.flow().collectAsStateWithLifecycle(defaultValue())
}
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ afterEvaluate {
create<MavenPublication>("maven") {
groupId = "com.github.rumboalla.kryptostore"
artifactId = "core"
version = "0.1.1"
version = "0.1.2"
from(components["release"])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ open class GenericPreference<T>(
override suspend fun get() = flow.first()
override suspend fun set(v: T) { store.edit { it[key] = transform.transform(v) } }
override fun flow() = flow
override fun defaultValue(): T = defValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,19 @@ interface Preference<T> {
*/
@DangerousApi
fun getBlocking() = runBlocking { get() }

/**
* Sets the value of a preference. This call blocks until the preference is set.
*
* @param v {@T} Value {@T} to set in the preference.
*/
@DangerousApi
fun setBlocking(v: T) = runBlocking { set(v) }

/**
* Gets the default value of a preference.
*
* @return {@T} Value of the preference.
*/
fun defaultValue(): T
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ open class PrimitivePreference<T>(
override suspend fun get() = flow.first()
override suspend fun set(v: T) { store.edit { it[key] = v } }
override fun flow() = flow
override fun defaultValue(): T = defValue
}
2 changes: 1 addition & 1 deletion gson/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ afterEvaluate {
create<MavenPublication>("maven") {
groupId = "com.github.rumboalla.kryptostore"
artifactId = "gson"
version = "0.1.1"
version = "0.1.2"
from(components["release"])
}
}
Expand Down
2 changes: 1 addition & 1 deletion keystore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ afterEvaluate {
create<MavenPublication>("maven") {
groupId = "com.github.rumboalla.kryptostore"
artifactId = "keystore"
version = "0.1.1"
version = "0.1.2"
from(components["release"])
}
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ rootProject.name = "KryptoStore"
include(":core")
include(":gson")
include(":keystore")
include(":compose")

0 comments on commit f8d5a75

Please sign in to comment.