Skip to content

Commit fc93511

Browse files
Create Android Friendly Meals (#97)
* Project structure * Add first screen, navigation, injection and app icons * Add recipe and image generation features to the main screen * Add Markdown support, add loading indicator, change cuisine field for notes field * Update vertex-ai-friendly-meals/android/app/src/main/java/com/google/firebase/example/friendlymeals/data/datasource/AIRemoteDataSource.kt Co-authored-by: Rosário P. Fernandes <[email protected]> * Update logic to show loading icon --------- Co-authored-by: Rosário P. Fernandes <[email protected]>
1 parent 32ca08e commit fc93511

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1416
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
*.iml
2+
*.apk
3+
*.aar
4+
*.zip
5+
.gradle
6+
/local.properties
7+
/.idea
8+
.DS_Store
9+
/build
10+
/captures
11+
.externalNativeBuild
12+
.cxx
13+
.vscode
14+
.project
15+
.settings
16+
.classpath
17+
google-services.json
18+
.idea/
19+
.idea/*
20+
local.properties
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.kotlin.compose)
5+
alias(libs.plugins.google.services)
6+
alias(libs.plugins.google.ksp)
7+
alias(libs.plugins.google.hilt)
8+
alias(libs.plugins.kotlin.serialization)
9+
}
10+
11+
android {
12+
namespace = "com.google.firebase.example.friendlymeals"
13+
compileSdk = 35
14+
15+
defaultConfig {
16+
applicationId = "com.google.firebase.example.friendlymeals"
17+
minSdk = 26
18+
targetSdk = 35
19+
versionCode = 1
20+
versionName = "1.0"
21+
22+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
23+
}
24+
25+
buildTypes {
26+
release {
27+
isMinifyEnabled = false
28+
proguardFiles(
29+
getDefaultProguardFile("proguard-android-optimize.txt"),
30+
"proguard-rules.pro"
31+
)
32+
}
33+
}
34+
compileOptions {
35+
sourceCompatibility = JavaVersion.VERSION_11
36+
targetCompatibility = JavaVersion.VERSION_11
37+
}
38+
kotlinOptions {
39+
jvmTarget = "11"
40+
}
41+
buildFeatures {
42+
compose = true
43+
}
44+
}
45+
46+
dependencies {
47+
implementation(libs.androidx.core.ktx)
48+
implementation(libs.androidx.lifecycle.runtime.ktx)
49+
implementation(libs.androidx.activity.compose)
50+
implementation(platform(libs.androidx.compose.bom))
51+
implementation(libs.androidx.ui)
52+
implementation(libs.androidx.ui.graphics)
53+
implementation(libs.androidx.ui.tooling.preview)
54+
implementation(libs.androidx.material3)
55+
implementation(libs.androidx.navigation.compose)
56+
implementation(libs.androidx.hilt.navigation.compose)
57+
implementation(libs.androidx.core.splashscreen)
58+
implementation(libs.androidx.constraintlayout.compose)
59+
implementation(libs.kotlinx.serialization.json)
60+
implementation(libs.hilt.android)
61+
ksp(libs.hilt.android.compiler)
62+
63+
implementation(platform(libs.firebase.bom))
64+
implementation(libs.firebase.vertexai)
65+
66+
//Library to handle Markdown in Compose
67+
implementation(libs.richtext.commonmark)
68+
69+
testImplementation(libs.junit)
70+
androidTestImplementation(libs.androidx.junit)
71+
androidTestImplementation(libs.androidx.espresso.core)
72+
androidTestImplementation(platform(libs.androidx.compose.bom))
73+
androidTestImplementation(libs.androidx.ui.test.junit4)
74+
debugImplementation(libs.androidx.ui.tooling)
75+
debugImplementation(libs.androidx.ui.test.manifest)
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.google.firebase.example.friendlymeals
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.google.firebase.example.friendlymeals", appContext.packageName)
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.FriendlyMeals"
14+
tools:targetApi="31"
15+
android:name = ".data.injection.FriendlyMealsHiltApp"
16+
android:windowSoftInputMode="adjustResize">
17+
<activity
18+
android:name=".MainActivity"
19+
android:exported="true"
20+
android:theme="@style/Theme.FriendlyMeals">
21+
<intent-filter>
22+
<action android:name="android.intent.action.MAIN" />
23+
24+
<category android:name="android.intent.category.LAUNCHER" />
25+
</intent-filter>
26+
</activity>
27+
</application>
28+
29+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.google.firebase.example.friendlymeals
2+
3+
import android.os.Bundle
4+
import android.view.WindowManager
5+
import androidx.activity.ComponentActivity
6+
import androidx.activity.compose.setContent
7+
import androidx.activity.enableEdgeToEdge
8+
import androidx.compose.foundation.layout.fillMaxSize
9+
import androidx.compose.foundation.layout.padding
10+
import androidx.compose.material3.MaterialTheme
11+
import androidx.compose.material3.Scaffold
12+
import androidx.compose.material3.SnackbarHost
13+
import androidx.compose.material3.SnackbarHostState
14+
import androidx.compose.material3.Surface
15+
import androidx.compose.runtime.remember
16+
import androidx.compose.ui.Modifier
17+
import androidx.navigation.compose.NavHost
18+
import androidx.navigation.compose.composable
19+
import androidx.navigation.compose.rememberNavController
20+
import com.google.firebase.example.friendlymeals.ui.home.HomeRoute
21+
import com.google.firebase.example.friendlymeals.ui.home.HomeScreen
22+
import com.google.firebase.example.friendlymeals.ui.theme.FriendlyMealsTheme
23+
import dagger.hilt.android.AndroidEntryPoint
24+
25+
@AndroidEntryPoint
26+
class MainActivity : ComponentActivity() {
27+
override fun onCreate(savedInstanceState: Bundle?) {
28+
super.onCreate(savedInstanceState)
29+
enableEdgeToEdge()
30+
setSoftInputMode()
31+
32+
setContent {
33+
val snackbarHostState = remember { SnackbarHostState() }
34+
val navController = rememberNavController()
35+
36+
FriendlyMealsTheme {
37+
Surface(
38+
modifier = Modifier.fillMaxSize(),
39+
color = MaterialTheme.colorScheme.background,
40+
) {
41+
Scaffold(
42+
modifier = Modifier.fillMaxSize(),
43+
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }
44+
) { innerPadding ->
45+
NavHost(
46+
navController = navController,
47+
startDestination = HomeRoute,
48+
modifier = Modifier.padding(innerPadding)
49+
) {
50+
composable<HomeRoute> { HomeScreen() }
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
58+
private fun setSoftInputMode() {
59+
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.google.firebase.example.friendlymeals
2+
3+
import android.util.Log
4+
import androidx.lifecycle.ViewModel
5+
import androidx.lifecycle.viewModelScope
6+
import kotlinx.coroutines.CoroutineExceptionHandler
7+
import kotlinx.coroutines.CoroutineScope
8+
import kotlinx.coroutines.launch
9+
10+
open class MainViewModel : ViewModel() {
11+
fun launchCatching(block: suspend CoroutineScope.() -> Unit) =
12+
viewModelScope.launch(
13+
CoroutineExceptionHandler { _, throwable ->
14+
Log.e("MainViewModel", throwable.message ?: "Unknown error")
15+
},
16+
block = block
17+
)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.google.firebase.example.friendlymeals.data.datasource
2+
3+
import android.graphics.Bitmap
4+
import com.google.firebase.vertexai.GenerativeModel
5+
import com.google.firebase.vertexai.ImagenModel
6+
import com.google.firebase.vertexai.type.PublicPreviewAPI
7+
import javax.inject.Inject
8+
9+
@OptIn(PublicPreviewAPI::class)
10+
class AIRemoteDataSource @Inject constructor(
11+
private val generativeModel: GenerativeModel,
12+
private val imagenModel: ImagenModel,
13+
) {
14+
suspend fun generateRecipe(ingredients: String, notes: String): String {
15+
var prompt = "Based on this ingredients list: $ingredients, please give me one recipe."
16+
if (notes.isNotBlank()) {
17+
prompt += "Please take in consideration these notes: $notes."
18+
}
19+
val response = generativeModel.generateContent(prompt)
20+
return response.text.orEmpty()
21+
}
22+
23+
@OptIn(PublicPreviewAPI::class)
24+
suspend fun generateRecipeImage(recipe: String): Bitmap {
25+
val imageResponse = imagenModel.generateImages(recipe)
26+
return imageResponse.images.first().asBitmap()
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.google.firebase.example.friendlymeals.data.injection
2+
3+
import com.google.firebase.Firebase
4+
import com.google.firebase.vertexai.GenerativeModel
5+
import com.google.firebase.vertexai.ImagenModel
6+
import com.google.firebase.vertexai.type.ImagenAspectRatio
7+
import com.google.firebase.vertexai.type.ImagenImageFormat
8+
import com.google.firebase.vertexai.type.ImagenPersonFilterLevel
9+
import com.google.firebase.vertexai.type.ImagenSafetyFilterLevel
10+
import com.google.firebase.vertexai.type.ImagenSafetySettings
11+
import com.google.firebase.vertexai.type.PublicPreviewAPI
12+
import com.google.firebase.vertexai.type.imagenGenerationConfig
13+
import com.google.firebase.vertexai.vertexAI
14+
import dagger.Module
15+
import dagger.Provides
16+
import dagger.hilt.InstallIn
17+
import dagger.hilt.components.SingletonComponent
18+
19+
@Module
20+
@InstallIn(SingletonComponent::class)
21+
object FirebaseHiltModule {
22+
@Provides fun generativeModel(): GenerativeModel {
23+
return Firebase.vertexAI.generativeModel(
24+
modelName = "gemini-2.0-flash"
25+
)
26+
}
27+
28+
@OptIn(PublicPreviewAPI::class)
29+
@Provides fun imagenModel(): ImagenModel {
30+
val generationConfig = imagenGenerationConfig {
31+
numberOfImages = 1
32+
aspectRatio = ImagenAspectRatio.SQUARE_1x1
33+
imageFormat = ImagenImageFormat.png()
34+
}
35+
36+
val safetySettings = ImagenSafetySettings(
37+
safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE,
38+
personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL
39+
)
40+
return Firebase.vertexAI.imagenModel(
41+
modelName = "imagen-3.0-generate-002",
42+
generationConfig = generationConfig,
43+
safetySettings = safetySettings
44+
)
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.google.firebase.example.friendlymeals.data.injection
2+
3+
import android.app.Application
4+
import dagger.hilt.android.HiltAndroidApp
5+
6+
@HiltAndroidApp
7+
class FriendlyMealsHiltApp: Application()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.google.firebase.example.friendlymeals.data.model
2+
3+
import android.graphics.Bitmap
4+
5+
data class Recipe(
6+
val title: String = "",
7+
val description: String = "",
8+
val ingredients: List<String> = listOf(),
9+
val steps: List<String> = listOf(),
10+
val image: Bitmap? = null
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.google.firebase.example.friendlymeals.data.repository
2+
3+
import android.graphics.Bitmap
4+
import com.google.firebase.example.friendlymeals.data.datasource.AIRemoteDataSource
5+
import javax.inject.Inject
6+
7+
class AIRepository @Inject constructor(
8+
private val aiRemoteDataSource: AIRemoteDataSource
9+
) {
10+
suspend fun generateRecipe(ingredients: String, notes: String): String {
11+
return aiRemoteDataSource.generateRecipe(ingredients, notes)
12+
}
13+
14+
suspend fun generateRecipeImage(recipe: String): Bitmap {
15+
return aiRemoteDataSource.generateRecipeImage(recipe)
16+
}
17+
}

0 commit comments

Comments
 (0)