Extract AddRecipeInput proto file to a module

This commit is contained in:
Kirill Kamakin
2022-08-04 20:53:26 +02:00
parent a53495b1f9
commit 8784912cdb
11 changed files with 109 additions and 45 deletions

1
datastore/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,60 @@
import com.google.protobuf.gradle.builtins
import com.google.protobuf.gradle.generateProtoTasks
import com.google.protobuf.gradle.protobuf
import com.google.protobuf.gradle.protoc
plugins {
id("gq.kirmanak.mealient.library")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
alias(libs.plugins.protobuf)
}
android {
namespace = "gq.kirmanak.mealient.datastore"
}
dependencies {
implementation(libs.androidx.datastore.preferences)
implementation(libs.androidx.datastore.datastore)
implementation(libs.google.protobuf.javalite)
implementation(libs.androidx.security.crypto)
implementation(libs.google.dagger.hiltAndroid)
kapt(libs.google.dagger.hiltCompiler)
kaptTest(libs.google.dagger.hiltAndroidCompiler)
testImplementation(libs.google.dagger.hiltAndroidTesting)
implementation(libs.jetbrains.kotlinx.datetime)
implementation(libs.jetbrains.kotlinx.coroutinesAndroid)
testImplementation(libs.jetbrains.kotlinx.coroutinesTest)
testImplementation(libs.androidx.test.junit)
testImplementation(libs.google.truth)
testImplementation(libs.io.mockk)
}
protobuf {
protoc {
artifact = libs.google.protobuf.protoc.get().toString()
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
val java by registering {
option("lite")
}
}
}
}
}
kapt {
correctErrorTypes = true
}

View File

@@ -0,0 +1,29 @@
package gq.kirmanak.mealient.datastore
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.dataStoreFile
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import gq.kirmanak.mealient.datastore.recipe.AddRecipeInput
import gq.kirmanak.mealient.datastore.recipe.AddRecipeInputSerializer
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
interface DataStoreModule {
companion object {
@Provides
@Singleton
fun provideAddRecipeInputStore(
@ApplicationContext context: Context
): DataStore<AddRecipeInput> = DataStoreFactory.create(AddRecipeInputSerializer) {
context.dataStoreFile("add_recipe_input")
}
}
}

View File

@@ -0,0 +1,19 @@
package gq.kirmanak.mealient.datastore.recipe
import androidx.datastore.core.CorruptionException
import androidx.datastore.core.Serializer
import com.google.protobuf.InvalidProtocolBufferException
import java.io.InputStream
import java.io.OutputStream
object AddRecipeInputSerializer : Serializer<AddRecipeInput> {
override val defaultValue: AddRecipeInput = AddRecipeInput.getDefaultInstance()
override suspend fun readFrom(input: InputStream): AddRecipeInput = try {
AddRecipeInput.parseFrom(input)
} catch (e: InvalidProtocolBufferException) {
throw CorruptionException("Can't read proto file", e)
}
override suspend fun writeTo(t: AddRecipeInput, output: OutputStream) = t.writeTo(output)
}

View File

@@ -0,0 +1,14 @@
syntax = "proto3";
option java_package = "gq.kirmanak.mealient.datastore.recipe";
option java_multiple_files = true;
message AddRecipeInput {
string recipeName = 1;
string recipeDescription = 2;
string recipeYield = 3;
repeated string recipeInstructions = 4;
repeated string recipeIngredients = 5;
bool isRecipePublic = 6;
bool areCommentsDisabled = 7;
}