|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.quickstart.vertexai.feature.image |
| 18 | + |
| 19 | +import androidx.compose.foundation.Image |
| 20 | +import androidx.compose.foundation.layout.Box |
| 21 | +import androidx.compose.foundation.layout.Column |
| 22 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 23 | +import androidx.compose.foundation.layout.padding |
| 24 | +import androidx.compose.foundation.rememberScrollState |
| 25 | +import androidx.compose.foundation.verticalScroll |
| 26 | +import androidx.compose.material3.Card |
| 27 | +import androidx.compose.material3.CardDefaults |
| 28 | +import androidx.compose.material3.CircularProgressIndicator |
| 29 | +import androidx.compose.material3.ElevatedCard |
| 30 | +import androidx.compose.material3.MaterialTheme |
| 31 | +import androidx.compose.material3.OutlinedTextField |
| 32 | +import androidx.compose.material3.Text |
| 33 | +import androidx.compose.material3.TextButton |
| 34 | +import androidx.compose.runtime.Composable |
| 35 | +import androidx.compose.runtime.collectAsState |
| 36 | +import androidx.compose.runtime.getValue |
| 37 | +import androidx.compose.runtime.mutableStateOf |
| 38 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 39 | +import androidx.compose.runtime.setValue |
| 40 | +import androidx.compose.ui.Alignment |
| 41 | +import androidx.compose.ui.Modifier |
| 42 | +import androidx.compose.ui.graphics.asImageBitmap |
| 43 | +import androidx.compose.ui.res.stringResource |
| 44 | +import androidx.compose.ui.tooling.preview.Preview |
| 45 | +import androidx.compose.ui.unit.dp |
| 46 | +import androidx.lifecycle.viewmodel.compose.viewModel |
| 47 | +import com.google.firebase.quickstart.vertexai.GenerativeViewModelFactory |
| 48 | +import com.google.firebase.quickstart.vertexai.R |
| 49 | +import com.google.firebase.quickstart.vertexai.ui.theme.GenerativeAISample |
| 50 | + |
| 51 | +@Composable |
| 52 | +internal fun ImagenRoute( |
| 53 | + imagenViewModel: ImagenViewModel = viewModel(factory = GenerativeViewModelFactory) |
| 54 | +) { |
| 55 | + val imagenUiState by imagenViewModel.uiState.collectAsState() |
| 56 | + |
| 57 | + ImagenScreen(imagenUiState, onImagenClicked = { inputText -> |
| 58 | + imagenViewModel.generateImage(inputText) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +@Composable |
| 63 | +fun ImagenScreen( |
| 64 | + uiState: ImagenUiState = ImagenUiState.Loading, |
| 65 | + onImagenClicked: (String) -> Unit = {} |
| 66 | +) { |
| 67 | + var imagenPrompt by rememberSaveable { mutableStateOf("") } |
| 68 | + |
| 69 | + Column( |
| 70 | + modifier = Modifier |
| 71 | + .verticalScroll(rememberScrollState()) |
| 72 | + ) { |
| 73 | + ElevatedCard( |
| 74 | + modifier = Modifier |
| 75 | + .padding(all = 16.dp) |
| 76 | + .fillMaxWidth(), |
| 77 | + shape = MaterialTheme.shapes.large |
| 78 | + ) { |
| 79 | + OutlinedTextField( |
| 80 | + value = imagenPrompt, |
| 81 | + label = { Text(stringResource(R.string.imagen_label)) }, |
| 82 | + placeholder = { Text(stringResource(R.string.imagen_hint)) }, |
| 83 | + onValueChange = { imagenPrompt = it }, |
| 84 | + modifier = Modifier |
| 85 | + .padding(16.dp) |
| 86 | + .fillMaxWidth() |
| 87 | + ) |
| 88 | + TextButton( |
| 89 | + onClick = { |
| 90 | + if (imagenPrompt.isNotBlank()) { |
| 91 | + onImagenClicked(imagenPrompt) |
| 92 | + } |
| 93 | + }, |
| 94 | + modifier = Modifier |
| 95 | + .padding(end = 16.dp, bottom = 16.dp) |
| 96 | + .align(Alignment.End) |
| 97 | + ) { |
| 98 | + Text(stringResource(R.string.action_go)) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + when (uiState) { |
| 103 | + ImagenUiState.Initial -> { |
| 104 | + // Nothing is shown |
| 105 | + } |
| 106 | + |
| 107 | + ImagenUiState.Loading -> { |
| 108 | + Box( |
| 109 | + contentAlignment = Alignment.Center, |
| 110 | + modifier = Modifier |
| 111 | + .padding(all = 8.dp) |
| 112 | + .align(Alignment.CenterHorizontally) |
| 113 | + ) { |
| 114 | + CircularProgressIndicator() |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + is ImagenUiState.Success -> { |
| 119 | + Card( |
| 120 | + modifier = Modifier |
| 121 | + .padding(start = 16.dp, end = 16.dp, bottom = 16.dp) |
| 122 | + .fillMaxWidth(), |
| 123 | + shape = MaterialTheme.shapes.large, |
| 124 | + colors = CardDefaults.cardColors( |
| 125 | + containerColor = MaterialTheme.colorScheme.onSecondaryContainer |
| 126 | + ) |
| 127 | + ) { |
| 128 | + Image(bitmap = uiState.image.asImageBitmap(), "") |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + is ImagenUiState.Error -> { |
| 133 | + Card( |
| 134 | + modifier = Modifier |
| 135 | + .padding(horizontal = 16.dp) |
| 136 | + .fillMaxWidth(), |
| 137 | + shape = MaterialTheme.shapes.large, |
| 138 | + colors = CardDefaults.cardColors( |
| 139 | + containerColor = MaterialTheme.colorScheme.errorContainer |
| 140 | + ) |
| 141 | + ) { |
| 142 | + Text( |
| 143 | + text = uiState.errorMessage, |
| 144 | + color = MaterialTheme.colorScheme.error, |
| 145 | + modifier = Modifier.padding(all = 16.dp) |
| 146 | + ) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +@Composable |
| 154 | +@Preview(showSystemUi = true) |
| 155 | +fun ImagenScreenPreview() { |
| 156 | + GenerativeAISample(darkTheme = true) { |
| 157 | + ImagenScreen() |
| 158 | + } |
| 159 | +} |
0 commit comments