Skip to content

Commit 8118314

Browse files
committed
Add Datastore with Protobuf
1 parent de8bb03 commit 8118314

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

app/build.gradle.kts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
plugins {
33
alias(libs.plugins.com.android.application)
44
alias(libs.plugins.org.jetbrains.kotlin.android)
5+
alias(libs.plugins.com.google.protobuf)
56
}
67

78
android {
@@ -60,11 +61,32 @@ dependencies {
6061
implementation(libs.ui.graphics)
6162
implementation(libs.ui.tooling.preview)
6263
implementation(libs.material3)
64+
implementation(libs.bundles.datastore)
6365
testImplementation(libs.junit)
6466
androidTestImplementation(libs.androidx.test.ext.junit)
6567
androidTestImplementation(libs.espresso.core)
6668
androidTestImplementation(platform(libs.compose.bom))
6769
androidTestImplementation(libs.ui.test.junit4)
6870
debugImplementation(libs.ui.tooling)
6971
debugImplementation(libs.ui.test.manifest)
72+
}
73+
74+
protobuf {
75+
protoc {
76+
artifact = libs.protoc.get().toString()
77+
}
78+
// https://github.com/google/protobuf-gradle-plugin/issues/518#issuecomment-1273099797
79+
// https://github.com/zhaobozhen/LibChecker/blob/c0c3bc7c661fe45cc44d5c6ab0202764652e0b7e/app/build.gradle.kts#L197
80+
generateProtoTasks {
81+
all().forEach {
82+
it.builtins {
83+
create("java") {
84+
option("lite")
85+
}
86+
create("kotlin") {
87+
option("lite")
88+
}
89+
}
90+
}
91+
}
7092
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.janmalch.composeprotosettings.preferences
2+
3+
import androidx.datastore.core.CorruptionException
4+
import androidx.datastore.core.Serializer
5+
import com.google.protobuf.InvalidProtocolBufferException
6+
import java.io.InputStream
7+
import java.io.OutputStream
8+
9+
object UserPreferencesSerializer : Serializer<UserPreferences> {
10+
override val defaultValue: UserPreferences = UserPreferences.getDefaultInstance()
11+
override suspend fun readFrom(input: InputStream): UserPreferences {
12+
try {
13+
return UserPreferences.parseFrom(input)
14+
} catch (exception: InvalidProtocolBufferException) {
15+
throw CorruptionException("Cannot read proto.", exception)
16+
}
17+
}
18+
19+
override suspend fun writeTo(t: UserPreferences, output: OutputStream) = t.writeTo(output)
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto3";
2+
3+
option java_package = "io.github.janmalch.composeprotosettings.preferences";
4+
option java_multiple_files = true;
5+
6+
message UserPreferences {
7+
// filter for showing / hiding completed tasks
8+
bool show_completed = 1;
9+
}

gradle/libs.versions.toml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
[versions]
22
agp = "8.1.0-rc01"
33
org-jetbrains-kotlin-android = "1.8.10"
4-
core-ktx = "1.9.0"
4+
core-ktx = "1.10.1"
55
junit = "4.13.2"
66
androidx-test-ext-junit = "1.1.5"
77
espresso-core = "3.5.1"
88
lifecycle-runtime-ktx = "2.6.1"
99
activity-compose = "1.7.2"
10-
compose-bom = "2023.03.00"
10+
compose-bom = "2023.06.01"
11+
datastore = "1.0.0"
12+
protobuf = "3.23.2" # "3.23.3" is broken
13+
protobuf-plugin = "0.9.3"
1114

1215
[libraries]
1316
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
@@ -25,9 +28,16 @@ ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
2528
ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
2629
material3 = { group = "androidx.compose.material3", name = "material3" }
2730

31+
datatore = { group = "androidx.datastore", name = "datastore", version.ref = "datastore" }
32+
protobuf-java = { group = "com.google.protobuf", name = "protobuf-javalite", version.ref = "protobuf" }
33+
protobuf-kotlin = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }
34+
protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" }
35+
36+
2837
[plugins]
2938
com-android-application = { id = "com.android.application", version.ref = "agp" }
3039
org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "org-jetbrains-kotlin-android" }
40+
com-google-protobuf = { id = "com.google.protobuf", version.ref = "protobuf-plugin"}
3141

3242
[bundles]
33-
43+
datastore = ["datatore", "protobuf-java", "protobuf-kotlin"]

0 commit comments

Comments
 (0)