Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Android Friendly Meals #97

Merged
merged 6 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions vertex-ai-friendly-meals/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
*.iml
*.apk
*.aar
*.zip
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
.vscode
.project
.settings
.classpath
google-services.json
.idea/
.idea/*
local.properties
1 change: 1 addition & 0 deletions vertex-ai-friendly-meals/android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
76 changes: 76 additions & 0 deletions vertex-ai-friendly-meals/android/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
alias(libs.plugins.google.services)
alias(libs.plugins.google.ksp)
alias(libs.plugins.google.hilt)
alias(libs.plugins.kotlin.serialization)
}

android {
namespace = "com.google.firebase.example.friendlymeals"
compileSdk = 35

defaultConfig {
applicationId = "com.google.firebase.example.friendlymeals"
minSdk = 26
targetSdk = 35
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
buildFeatures {
compose = true
}
}

dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.hilt.navigation.compose)
implementation(libs.androidx.core.splashscreen)
implementation(libs.androidx.constraintlayout.compose)
implementation(libs.kotlinx.serialization.json)
implementation(libs.hilt.android)
ksp(libs.hilt.android.compiler)

implementation(platform(libs.firebase.bom))
implementation(libs.firebase.vertexai)

//Library to handle Markdown in Compose
implementation(libs.richtext.commonmark)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
21 changes: 21 additions & 0 deletions vertex-ai-friendly-meals/android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.google.firebase.example.friendlymeals

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.google.firebase.example.friendlymeals", appContext.packageName)
}
}
29 changes: 29 additions & 0 deletions vertex-ai-friendly-meals/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FriendlyMeals"
tools:targetApi="31"
android:name = ".data.injection.FriendlyMealsHiltApp"
android:windowSoftInputMode="adjustResize">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.FriendlyMeals">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.google.firebase.example.friendlymeals

import android.os.Bundle
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.google.firebase.example.friendlymeals.ui.home.HomeRoute
import com.google.firebase.example.friendlymeals.ui.home.HomeScreen
import com.google.firebase.example.friendlymeals.ui.theme.FriendlyMealsTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setSoftInputMode()

setContent {
val snackbarHostState = remember { SnackbarHostState() }
val navController = rememberNavController()

FriendlyMealsTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background,
) {
Scaffold(
modifier = Modifier.fillMaxSize(),
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }
) { innerPadding ->
NavHost(
navController = navController,
startDestination = HomeRoute,
modifier = Modifier.padding(innerPadding)
) {
composable<HomeRoute> { HomeScreen() }
}
}
}
}
}
}

private fun setSoftInputMode() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.google.firebase.example.friendlymeals

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

open class MainViewModel : ViewModel() {
fun launchCatching(block: suspend CoroutineScope.() -> Unit) =
viewModelScope.launch(
CoroutineExceptionHandler { _, throwable ->
Log.e("MainViewModel", throwable.message ?: "Unknown error")
},
block = block
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.google.firebase.example.friendlymeals.data.datasource

import android.graphics.Bitmap
import com.google.firebase.vertexai.GenerativeModel
import com.google.firebase.vertexai.ImagenModel
import com.google.firebase.vertexai.type.PublicPreviewAPI
import javax.inject.Inject

@OptIn(PublicPreviewAPI::class)
class AIRemoteDataSource @Inject constructor(
private val generativeModel: GenerativeModel,
private val imagenModel: ImagenModel,
) {
suspend fun generateRecipe(ingredients: String, notes: String): String {
var prompt = "Based on this ingredients list: $ingredients, please give me one recipe."
if (notes.isNotBlank()) {
prompt += "Please take in consideration these notes: $notes."
}
val response = generativeModel.generateContent(prompt)
return response.text.orEmpty()
}

@OptIn(PublicPreviewAPI::class)
suspend fun generateRecipeImage(recipe: String): Bitmap {
val imageResponse = imagenModel.generateImages(recipe)
return imageResponse.images.first().asBitmap()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.google.firebase.example.friendlymeals.data.injection

import com.google.firebase.Firebase
import com.google.firebase.vertexai.GenerativeModel
import com.google.firebase.vertexai.ImagenModel
import com.google.firebase.vertexai.type.ImagenAspectRatio
import com.google.firebase.vertexai.type.ImagenImageFormat
import com.google.firebase.vertexai.type.ImagenPersonFilterLevel
import com.google.firebase.vertexai.type.ImagenSafetyFilterLevel
import com.google.firebase.vertexai.type.ImagenSafetySettings
import com.google.firebase.vertexai.type.PublicPreviewAPI
import com.google.firebase.vertexai.type.imagenGenerationConfig
import com.google.firebase.vertexai.vertexAI
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(SingletonComponent::class)
object FirebaseHiltModule {
@Provides fun generativeModel(): GenerativeModel {
return Firebase.vertexAI.generativeModel(
modelName = "gemini-2.0-flash"
)
}

@OptIn(PublicPreviewAPI::class)
@Provides fun imagenModel(): ImagenModel {
val generationConfig = imagenGenerationConfig {
numberOfImages = 1
aspectRatio = ImagenAspectRatio.SQUARE_1x1
imageFormat = ImagenImageFormat.png()
}

val safetySettings = ImagenSafetySettings(
safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE,
personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL
)
return Firebase.vertexAI.imagenModel(
modelName = "imagen-3.0-generate-002",
generationConfig = generationConfig,
safetySettings = safetySettings
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.google.firebase.example.friendlymeals.data.injection

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class FriendlyMealsHiltApp: Application()
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.google.firebase.example.friendlymeals.data.model

import android.graphics.Bitmap

data class Recipe(
val title: String = "",
val description: String = "",
val ingredients: List<String> = listOf(),
val steps: List<String> = listOf(),
val image: Bitmap? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.google.firebase.example.friendlymeals.data.repository

import android.graphics.Bitmap
import com.google.firebase.example.friendlymeals.data.datasource.AIRemoteDataSource
import javax.inject.Inject

class AIRepository @Inject constructor(
private val aiRemoteDataSource: AIRemoteDataSource
) {
suspend fun generateRecipe(ingredients: String, notes: String): String {
return aiRemoteDataSource.generateRecipe(ingredients, notes)
}

suspend fun generateRecipeImage(recipe: String): Bitmap {
return aiRemoteDataSource.generateRecipeImage(recipe)
}
}
Loading