Extract AddRecipeInput proto file to a module
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
14
datastore/src/main/proto/AddRecipeInput.proto
Normal file
14
datastore/src/main/proto/AddRecipeInput.proto
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user