-
Notifications
You must be signed in to change notification settings - Fork 128
Create Android Friendly Meals #97
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da71519
Project structure
marinacoelho bd05665
Add first screen, navigation, injection and app icons
marinacoelho 9491884
Add recipe and image generation features to the main screen
marinacoelho 8cb7776
Add Markdown support, add loading indicator, change cuisine field for…
marinacoelho 748b7b1
Update vertex-ai-friendly-meals/android/app/src/main/java/com/google/…
marinacoelho a23608d
Update logic to show loading icon
marinacoelho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
24 changes: 24 additions & 0 deletions
24
...src/androidTest/java/com/google/firebase/example/friendlymeals/ExampleInstrumentedTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
vertex-ai-friendly-meals/android/app/src/main/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
61 changes: 61 additions & 0 deletions
61
...meals/android/app/src/main/java/com/google/firebase/example/friendlymeals/MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...eals/android/app/src/main/java/com/google/firebase/example/friendlymeals/MainViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} |
28 changes: 28 additions & 0 deletions
28
...main/java/com/google/firebase/example/friendlymeals/data/datasource/AIRemoteDataSource.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../main/java/com/google/firebase/example/friendlymeals/data/injection/FirebaseHiltModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ain/java/com/google/firebase/example/friendlymeals/data/injection/FriendlyMealsHiltApp.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
11 changes: 11 additions & 0 deletions
11
.../android/app/src/main/java/com/google/firebase/example/friendlymeals/data/model/Recipe.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
17 changes: 17 additions & 0 deletions
17
...p/src/main/java/com/google/firebase/example/friendlymeals/data/repository/AIRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.