-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include new module for preferences (Shared and DataStore)
- Loading branch information
Showing
15 changed files
with
304 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
plugins { | ||
id 'com.android.library' | ||
id 'kotlin-android' | ||
id 'maven-publish' | ||
id 'org.jetbrains.dokka' | ||
} | ||
|
||
android { | ||
|
||
compileSdkVersion 33 | ||
buildToolsVersion "30.0.3" | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 33 | ||
consumerProguardFiles "consumer-rules.pro" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
kotlinOptions.jvmTarget = JavaVersion.VERSION_17.toString() | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_17 | ||
targetCompatibility JavaVersion.VERSION_17 | ||
} | ||
|
||
dokkaHtml.configure { | ||
|
||
outputDirectory.set(file("../docs")) | ||
moduleName.set("$rootProject.name") | ||
suppressInheritedMembers.set(true) | ||
pluginsMapConfiguration.set( | ||
[ | ||
"org.jetbrains.dokka.base.DokkaBase": """{ | ||
"footerMessage": "Made with ❤️ at <b><a href=\\"https://about.jeluchu.com\\">Jéluchu</a></b> © 2022", | ||
"customStyleSheets": ["${file("../logo-style.css")}"] | ||
}""" | ||
] | ||
) | ||
} | ||
|
||
namespace 'com.jeluchu.jchucomponents.qr' | ||
} | ||
|
||
dependencies { | ||
implementation libs.bundles.preferences.androidx | ||
implementation libs.bundles.preferences.google | ||
implementation libs.bundles.preferences.compose | ||
} | ||
|
||
afterEvaluate { | ||
publishing { | ||
publications { | ||
release(MavenPublication) { | ||
from components.release | ||
groupId = "com.github.jeluchu" | ||
artifactId = "jchucomponents-preferences" | ||
version = "1.7.0" | ||
} | ||
} | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest> | ||
|
||
</manifest> |
181 changes: 181 additions & 0 deletions
181
jchucomponents-prefs/src/main/kotlin/com/jeluchu/prefs/datastore/DataStoreHelpers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package com.jeluchu.prefs.datastore | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import com.jeluchu.prefs.extensions.empty | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
|
||
class DataStoreHelpers { | ||
fun <T> savePreference(key: Preferences.Key<T>, value: T) { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
dataStore?.edit { preferences -> | ||
preferences[key] = value | ||
} | ||
} | ||
} | ||
|
||
fun savePreference(key: Preferences.Key<Int>, value: Int) { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
dataStore?.edit { preferences -> | ||
preferences[key] = value | ||
} | ||
} | ||
} | ||
|
||
fun savePreference(key: Preferences.Key<Long>, value: Long) { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
dataStore?.edit { preferences -> | ||
preferences[key] = value | ||
} | ||
} | ||
} | ||
|
||
fun savePreference(key: Preferences.Key<Float>, value: Float) { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
dataStore?.edit { preferences -> | ||
preferences[key] = value | ||
} | ||
} | ||
} | ||
|
||
fun savePreference(key: Preferences.Key<String>, value: String) { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
dataStore?.edit { preferences -> | ||
preferences[key] = value | ||
} | ||
} | ||
} | ||
|
||
fun savePreference(key: Preferences.Key<Boolean>, value: Boolean) { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
dataStore?.edit { preferences -> | ||
preferences[key] = value | ||
} | ||
} | ||
} | ||
|
||
fun <T> getPreference( | ||
key: Preferences.Key<T> | ||
) = runBlocking { | ||
dataStore?.data?.first()?.toPreferences()?.get(key) | ||
} | ||
|
||
fun getPreference( | ||
key: Preferences.Key<Int>, | ||
default: Int = Int.empty() | ||
) = runBlocking { | ||
dataStore?.data?.first()?.toPreferences()?.get(key) ?: default | ||
} | ||
|
||
fun getPreference( | ||
key: Preferences.Key<Long>, | ||
default: Long = Long.empty() | ||
) = runBlocking { | ||
dataStore?.data?.first()?.toPreferences()?.get(key) ?: default | ||
} | ||
|
||
fun getPreference( | ||
key: Preferences.Key<Float>, | ||
default: Float = Float.empty() | ||
) = runBlocking { | ||
dataStore?.data?.first()?.toPreferences()?.get(key) ?: default | ||
} | ||
|
||
fun getPreference( | ||
key: Preferences.Key<String>, | ||
default: String = String.empty() | ||
) = runBlocking { | ||
dataStore?.data?.first()?.toPreferences()?.get(key) ?: default | ||
} | ||
|
||
fun getPreference( | ||
key: Preferences.Key<Boolean>, | ||
default: Boolean = false | ||
) = runBlocking { | ||
dataStore?.data?.first()?.toPreferences()?.get(key) ?: default | ||
} | ||
|
||
companion object { | ||
var dataStore: DataStore<Preferences>? = null | ||
val Context.defaultPreferencesDataStore by preferencesDataStore(name = "default") | ||
|
||
fun initDataStore(context: Context) { | ||
dataStore = context.defaultPreferencesDataStore | ||
} | ||
|
||
fun <T> DataStore<Preferences>.getValueSync( | ||
key: Preferences.Key<T> | ||
) = runBlocking { data.first() }[key] | ||
|
||
fun DataStore<Preferences>.getValueSync( | ||
key: Preferences.Key<Int>, | ||
default: Int = Int.empty() | ||
) = runBlocking { data.first() }[key] ?: default | ||
|
||
fun DataStore<Preferences>.getValueSync( | ||
key: Preferences.Key<Long>, | ||
default: Long = Long.empty() | ||
) = runBlocking { data.first() }[key] ?: default | ||
|
||
fun DataStore<Preferences>.getValueSync( | ||
key: Preferences.Key<Float>, | ||
default: Float = Float.empty() | ||
) = runBlocking { data.first() }[key] ?: default | ||
|
||
fun DataStore<Preferences>.getValueSync( | ||
key: Preferences.Key<String>, | ||
default: String = String.empty() | ||
) = runBlocking { data.first() }[key] ?: default | ||
|
||
fun DataStore<Preferences>.getValueSync( | ||
key: Preferences.Key<Boolean>, | ||
default: Boolean = false | ||
) = runBlocking { data.first() }[key] ?: default | ||
|
||
@Composable | ||
fun <T> rememberPreference( | ||
key: Preferences.Key<T>, | ||
defaultValue: T, | ||
): MutableState<T> { | ||
val coroutineScope = rememberCoroutineScope() | ||
val context = LocalContext.current | ||
val state = remember { | ||
context.defaultPreferencesDataStore.data | ||
.map { | ||
it[key] ?: defaultValue | ||
} | ||
}.collectAsStateWithLifecycle(initialValue = defaultValue) | ||
|
||
return remember { | ||
object : MutableState<T> { | ||
override var value: T | ||
get() = state.value | ||
set(value) { | ||
coroutineScope.launch { | ||
context.defaultPreferencesDataStore.edit { | ||
it[key] = value | ||
} | ||
} | ||
} | ||
|
||
override fun component1() = value | ||
override fun component2(): (T) -> Unit = { value = it } | ||
} | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
jchucomponents-prefs/src/main/kotlin/com/jeluchu/prefs/extensions/CommonsExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.jeluchu.prefs.extensions | ||
|
||
fun Int.Companion.empty() = 0 | ||
fun Long.Companion.empty() = 0L | ||
fun Float.Companion.empty() = 0f | ||
fun String.Companion.empty() = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.