Skip to content

Commit 8f1bafc

Browse files
Initial commit
0 parents  commit 8f1bafc

File tree

58 files changed

+1341
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1341
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

build.gradle

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
buildscript {
2+
ext.buildConfig = [
3+
'versionCode' : 1,
4+
'versionName' : "1.0.0",
5+
'compileSdkVersion': 30,
6+
'minSdkVersion' : 16,
7+
'targetSdkVersion' : 30
8+
]
9+
ext {
10+
constraintLayoutVersion = '2.1.1'
11+
espressoVersion = '3.4.0'
12+
datastoreVersion = '1.0.0'
13+
kotlinCoroutinesTestVersion = '1.6.0'
14+
kotlinVersion = "1.5.31"
15+
junitExtVersion = '1.1.3'
16+
junitVersion = '4.13.2'
17+
startupVersion = '1.1.0'
18+
}
19+
repositories {
20+
google()
21+
mavenCentral()
22+
}
23+
dependencies {
24+
classpath "com.android.tools.build:gradle:4.2.2"
25+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
26+
}
27+
}
28+
29+
allprojects {
30+
repositories {
31+
google()
32+
mavenCentral()
33+
jcenter() // Warning: this repository is going to shut down soon
34+
}
35+
}
36+
37+
task clean(type: Delete) {
38+
delete rootProject.buildDir
39+
}

datastore-ktx/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

datastore-ktx/build.gradle

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
plugins {
2+
id 'com.android.library'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
android {
7+
compileSdkVersion buildConfig.compileSdkVersion
8+
9+
defaultConfig {
10+
minSdkVersion buildConfig.minSdkVersion
11+
12+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
13+
consumerProguardFiles "consumer-rules.pro"
14+
}
15+
16+
buildTypes {
17+
release {
18+
minifyEnabled 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+
freeCompilerArgs += ['-module-name', "datastore-flow"]
31+
}
32+
}
33+
34+
dependencies {
35+
api "androidx.datastore:datastore-preferences:$datastoreVersion"
36+
implementation "androidx.startup:startup-runtime:$startupVersion"
37+
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1"
38+
testImplementation "org.jetbrains.kotlin:kotlin-coroutines-test:$kotlinCoroutinesTestVersion"
39+
androidTestImplementation "androidx.test.ext:junit:$junitExtVersion"
40+
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
41+
}

datastore-ktx/consumer-rules.pro

Whitespace-only changes.

datastore-ktx/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2022. Dylan Cai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.dylanc.datastore.flow
19+
20+
import androidx.test.ext.junit.runners.AndroidJUnit4
21+
import kotlinx.coroutines.GlobalScope
22+
import kotlinx.coroutines.flow.collect
23+
import kotlinx.coroutines.launch
24+
import kotlinx.coroutines.runBlocking
25+
import org.junit.Assert
26+
import org.junit.Test
27+
import org.junit.runner.RunWith
28+
29+
@RunWith(AndroidJUnit4::class)
30+
class DataStoreTest : DataStoreOwner {
31+
private val i1 by intPreferences()
32+
33+
@Test
34+
fun testInt1() {
35+
// =
36+
// i1.getValue().collect {
37+
// Assert.assertEquals(0, it)
38+
// }
39+
// i1.setValue(6).collect()
40+
// i1.getValue().collect {
41+
// Assert.assertEquals(6, it)
42+
// }
43+
}
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.dylanc.datastore.flow">
5+
6+
<application>
7+
<provider
8+
android:name="androidx.startup.InitializationProvider"
9+
android:authorities="${applicationId}.androidx-startup"
10+
android:exported="false"
11+
tools:node="merge">
12+
<meta-data
13+
android:name="com.dylanc.datastore.flow.DataStoreInitializer"
14+
android:value="androidx.startup" />
15+
</provider>
16+
</application>
17+
</manifest>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.dylanc.datastore.flow
2+
3+
import androidx.datastore.preferences.core.Preferences
4+
import androidx.lifecycle.LifecycleOwner
5+
import androidx.lifecycle.asLiveData
6+
import kotlinx.coroutines.flow.Flow
7+
8+
interface DataStoreFlow<T> {
9+
fun setValue(block: suspend (T) -> T): Flow<Preferences>
10+
11+
fun getValue(): Flow<T>
12+
13+
fun setValue(value: T): Flow<Preferences> = setValue { value }
14+
15+
fun setValue(owner: LifecycleOwner, block: suspend (T) -> T) =
16+
setValue(block).asLiveData().observe(owner) {}
17+
18+
fun setValue(owner: LifecycleOwner, value: T) = setValue(owner) { value }
19+
20+
fun observeValue(owner: LifecycleOwner, block: (T) -> Unit) =
21+
getValue().asLiveData().observe(owner) { block(it) }
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2022. Dylan Cai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:Suppress("unused")
18+
19+
package com.dylanc.datastore.flow
20+
21+
import android.app.Application
22+
import android.content.Context
23+
import androidx.datastore.core.DataStore
24+
import androidx.datastore.preferences.core.Preferences
25+
import androidx.datastore.preferences.preferencesDataStore
26+
import androidx.startup.Initializer
27+
28+
/**
29+
* @author Dylan Cai
30+
*/
31+
class DataStoreInitializer : Initializer<Unit> {
32+
33+
override fun create(context: Context) {
34+
application = context as Application
35+
if (DataStoreOwner.default == null) {
36+
DataStoreOwner.default = context.dataStore
37+
}
38+
}
39+
40+
override fun dependencies() = emptyList<Class<Initializer<*>>>()
41+
42+
private val Context.dataStore by preferencesDataStore("default")
43+
44+
companion object {
45+
internal lateinit var application: Application
46+
}
47+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
@file:Suppress("unused")
2+
3+
package com.dylanc.datastore.flow
4+
5+
import android.content.Context
6+
import androidx.datastore.core.DataStore
7+
import androidx.datastore.preferences.core.*
8+
import kotlinx.coroutines.flow.Flow
9+
import kotlinx.coroutines.flow.flow
10+
import kotlinx.coroutines.flow.map
11+
import kotlinx.coroutines.flow.single
12+
import kotlin.properties.ReadOnlyProperty
13+
import kotlin.reflect.KProperty
14+
15+
interface DataStoreOwner {
16+
val dataStore: DataStore<Preferences>
17+
get() = default ?: throw IllegalStateException("DataStoreOwner must be initialized before use")
18+
19+
val context: Context get() = DataStoreInitializer.application
20+
21+
companion object {
22+
var default: DataStore<Preferences>? = null
23+
}
24+
}
25+
26+
fun DataStoreOwner.intPreferences(default: Int = 0) =
27+
DataStoreProperty(default) { intPreferencesKey(it) }
28+
29+
fun DataStoreOwner.doublePreferences(default: Double = 0.0) =
30+
DataStoreProperty(default) { doublePreferencesKey(it) }
31+
32+
fun DataStoreOwner.booleanPreferences(default: Boolean = false) =
33+
DataStoreProperty(default) { booleanPreferencesKey(it) }
34+
35+
fun DataStoreOwner.floatPreferences(default: Float = 0f) =
36+
DataStoreProperty(default) { floatPreferencesKey(it) }
37+
38+
fun DataStoreOwner.longPreferences(default: Long = 0) =
39+
DataStoreProperty(default) { longPreferencesKey(it) }
40+
41+
fun DataStoreOwner.stringPreferences() =
42+
DataStoreNullableProperty { stringPreferencesKey(it) }
43+
44+
fun DataStoreOwner.stringPreferences(default: String) =
45+
DataStoreProperty(default) { stringPreferencesKey(it) }
46+
47+
fun DataStoreOwner.stringSetPreferences() =
48+
DataStoreNullableProperty { stringSetPreferencesKey(it) }
49+
50+
fun DataStoreOwner.stringSetPreferences(default: Set<String>) =
51+
DataStoreProperty(default) { stringSetPreferencesKey(it) }
52+
53+
class DataStoreProperty<V>(
54+
private val default: V,
55+
private val key: (String) -> Preferences.Key<V>
56+
) : ReadOnlyProperty<DataStoreOwner, DataStoreFlow<V>> {
57+
private var cache: V? = null
58+
59+
override fun getValue(thisRef: DataStoreOwner, property: KProperty<*>) = object : DataStoreFlow<V> {
60+
61+
override fun setValue(block: suspend (V) -> V): Flow<Preferences> =
62+
flow {
63+
emit(thisRef.dataStore.edit { preferences ->
64+
val value = block(cache ?: preferences[key(property.name)] ?: default)
65+
preferences[key(property.name)] = value
66+
cache = value
67+
})
68+
}
69+
70+
override fun getValue(): Flow<V> =
71+
thisRef.dataStore.data.map { preferences ->
72+
cache ?: (preferences[key(property.name)] ?: default).also { cache = it }
73+
}
74+
}
75+
}
76+
77+
class DataStoreNullableProperty<V>(
78+
private val key: (String) -> Preferences.Key<V>
79+
) : ReadOnlyProperty<DataStoreOwner, DataStoreFlow<V?>> {
80+
private var cache: V? = null
81+
82+
override fun getValue(thisRef: DataStoreOwner, property: KProperty<*>) = object : DataStoreFlow<V?> {
83+
84+
override fun setValue(block: suspend (V?) -> V?): Flow<Preferences> =
85+
flow {
86+
emit(thisRef.dataStore.edit { preferences ->
87+
val value = block(cache ?: preferences[key(property.name)])
88+
if (value != null) {
89+
preferences[key(property.name)] = value
90+
} else {
91+
preferences.remove(key(property.name))
92+
}
93+
cache = value
94+
})
95+
}
96+
97+
override fun getValue(): Flow<V?> =
98+
thisRef.dataStore.data.map { preferences ->
99+
cache ?: preferences[key(property.name)].also { cache = it }
100+
}
101+
}
102+
}

datastore-rxjava2/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

0 commit comments

Comments
 (0)