Add protobuf datastore
This commit is contained in:
parent
82b38dacab
commit
5b8179ade1
5 changed files with 76 additions and 1 deletions
|
@ -1,8 +1,30 @@
|
|||
import com.google.protobuf.gradle.*
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.android.application)
|
||||
alias(libs.plugins.jetbrains.kotlin.android)
|
||||
alias(libs.plugins.compose.compiler)
|
||||
alias(libs.plugins.serialization)
|
||||
alias(libs.plugins.protobuf)
|
||||
}
|
||||
|
||||
protobuf {
|
||||
protoc {
|
||||
artifact = "com.google.protobuf:protoc:3.24.1"
|
||||
}
|
||||
|
||||
generateProtoTasks {
|
||||
all().forEach { task ->
|
||||
task.builtins {
|
||||
id("java") {
|
||||
option("lite")
|
||||
}
|
||||
id("kotlin") {
|
||||
option("lite")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
android {
|
||||
|
@ -76,6 +98,8 @@ dependencies {
|
|||
implementation(platform(libs.androidx.compose.bom))
|
||||
implementation(libs.okhttp)
|
||||
implementation(libs.okhttp.logging.interceptor)
|
||||
implementation(libs.protobuf.javalite)
|
||||
implementation(libs.protobuf.kotlinlite)
|
||||
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.androidx.junit)
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package ing.bikeshedengineer.debtpirate
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.CorruptionException
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.core.Serializer
|
||||
import androidx.datastore.dataStore
|
||||
import com.google.protobuf.InvalidProtocolBufferException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
object StoreSerializer : Serializer<Store> {
|
||||
override val defaultValue: Store
|
||||
get() = Store.getDefaultInstance()
|
||||
|
||||
override suspend fun readFrom(input: InputStream): Store {
|
||||
try {
|
||||
return Store.parseFrom(input)
|
||||
} catch (exception: InvalidProtocolBufferException) {
|
||||
throw CorruptionException("Cannot read proto file.", exception)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun writeTo(t: Store, output: OutputStream) = t.writeTo(output)
|
||||
}
|
||||
|
||||
val Context.dataStore: DataStore<Store> by dataStore(
|
||||
fileName = "store.proto",
|
||||
serializer = StoreSerializer
|
||||
)
|
15
app/app/src/main/proto/store.proto
Normal file
15
app/app/src/main/proto/store.proto
Normal file
|
@ -0,0 +1,15 @@
|
|||
syntax = "proto3";
|
||||
|
||||
option java_package = "ing.bikeshedengineer.debtpirate";
|
||||
option java_multiple_files = true;
|
||||
|
||||
message Token {
|
||||
string token = 1;
|
||||
int64 expires_at = 2;
|
||||
}
|
||||
|
||||
message Store {
|
||||
int32 user_id = 1;
|
||||
Token auth_token = 2;
|
||||
Token session_token = 3;
|
||||
}
|
|
@ -4,4 +4,5 @@ plugins {
|
|||
alias(libs.plugins.jetbrains.kotlin.android) apply false
|
||||
alias(libs.plugins.compose.compiler) apply false
|
||||
alias(libs.plugins.serialization) apply false
|
||||
alias(libs.plugins.protobuf) apply false
|
||||
}
|
||||
|
|
|
@ -18,8 +18,10 @@ lifecycleViewmodelCompose = "2.8.6"
|
|||
material = "1.12.0"
|
||||
material3 = "1.3.0"
|
||||
navigation = "2.8.2"
|
||||
retrofit = "2.9.0"
|
||||
protobuf = "0.9.4"
|
||||
protoLite = "3.21.11"
|
||||
okhttp = "4.10.0"
|
||||
retrofit = "2.9.0"
|
||||
|
||||
[libraries]
|
||||
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
|
||||
|
@ -50,9 +52,12 @@ retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref =
|
|||
retrofit-gson = { group = "com.squareup.retrofit2", name = "converter-gson", version.ref = "retrofit" }
|
||||
okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttp" }
|
||||
okhttp-logging-interceptor = { group = "com.squareup.okhttp3", name = "logging-interceptor", version.ref = "okhttp" }
|
||||
protobuf-javalite = { group = "com.google.protobuf", name = "protobuf-javalite", version.ref = "protoLite" }
|
||||
protobuf-kotlinlite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protoLite" }
|
||||
|
||||
[plugins]
|
||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
|
||||
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
|
||||
serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
|
||||
protobuf = { id = "com.google.protobuf", version.ref = "protobuf" }
|
Loading…
Add table
Reference in a new issue