-
Notifications
You must be signed in to change notification settings - Fork 780
Imagen Editing Solved #110
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.android.ai.samples.imagenediting.ui | ||
| package com.android.ai.samples.imagenediting.sample.ui | ||
|
|
||
| import android.graphics.Bitmap | ||
| import android.graphics.Paint | ||
|
|
@@ -58,7 +58,7 @@ import androidx.compose.ui.layout.ContentScale | |
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.core.graphics.createBitmap | ||
| import com.android.ai.samples.imagenediting.R | ||
| import com.android.ai.samples.imagenediting.sample.R | ||
| import kotlin.math.min | ||
|
|
||
| @Composable | ||
|
|
@@ -80,12 +80,27 @@ fun ImagenEditingMaskEditor(sourceBitmap: Bitmap, onMaskFinalized: (Bitmap) -> U | |
| .fillMaxWidth() | ||
| .pointerInput(Unit) { | ||
| detectDragGestures( | ||
| // TODO #4 - Implement Drag logic | ||
| onDragStart = { startOffset -> | ||
| val transformedStart = Offset( | ||
| (startOffset.x - offsetX) / scale, | ||
| (startOffset.y - offsetY) / scale, | ||
| ) | ||
| currentPath = Path().apply { moveTo(transformedStart.x, transformedStart.y) } | ||
| }, | ||
| onDrag = { change, _ -> | ||
| currentPath?.let { | ||
| val transformedChange = Offset( | ||
| (change.position.x - offsetX) / scale, | ||
| (change.position.y - offsetY) / scale, | ||
| ) | ||
| it.lineTo(transformedChange.x, transformedChange.y) | ||
| currentPath = Path().apply { addPath(it) } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating a new For better performance, consider a different state management strategy. For example, you could collect the |
||
| } | ||
| change.consume() | ||
| }, | ||
| onDragEnd = { | ||
| currentPath?.let { paths.add(it) } | ||
| currentPath = null | ||
| }, | ||
| ) | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,13 +13,13 @@ | |
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.android.ai.samples.imagenediting.ui | ||
| package com.android.ai.samples.imagenediting.sample.ui | ||
|
|
||
| import android.graphics.Bitmap | ||
| import android.util.Log | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import com.android.ai.samples.imagenediting.data.ImagenEditingDataSource | ||
| import com.android.ai.samples.imagenediting.sample.data.ImagenEditingDataSource | ||
| import dagger.hilt.android.lifecycle.HiltViewModel | ||
| import javax.inject.Inject | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
|
|
@@ -54,8 +54,23 @@ class ImagenEditingViewModel @Inject constructor(private val imagenDataSource: I | |
| } | ||
|
|
||
| fun inpaintImage(sourceImage: Bitmap, maskImage: Bitmap, prompt: String, editSteps: Int = 50) { | ||
| // TODO #5 - Implement ViewModel Logic for inpainting | ||
| } | ||
| _uiState.value = ImagenEditingUIState.Loading | ||
| viewModelScope.launch { | ||
| try { | ||
| val inpaintedBitmap = imagenDataSource.inpaintImage( | ||
| sourceImage = sourceImage, | ||
| maskImage = maskImage, | ||
| prompt = prompt, | ||
| editSteps = editSteps, | ||
| ) | ||
| _uiState.value = ImagenEditingUIState.ImageGenerated( | ||
| bitmap = inpaintedBitmap, | ||
| contentDescription = "Inpainted image based on prompt: $prompt", | ||
| ) | ||
| } catch (e: Exception) { | ||
| _uiState.value = ImagenEditingUIState.Error(e.localizedMessage ?: "An unknown error occurred during inpainting") | ||
| } | ||
|
Comment on lines
+70
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception is caught and an error message is shown to the user, but the exception itself is not logged. This is often referred to as "swallowing an exception" and makes debugging failures very difficult because the stack trace and other details are lost. It's a best practice to always log the caught exception to aid in debugging. } catch (e: Exception) {
Log.e("ImagenEditingViewModel", "An error occurred during inpainting", e)
_uiState.value = ImagenEditingUIState.Error(e.localizedMessage ?: "An unknown error occurred during inpainting")
} |
||
| } } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| fun onImageMaskReady(originalBitmap: Bitmap, maskBitmap: Bitmap) { | ||
| val originalContentDescription = (_uiState.value as? ImagenEditingUIState.ImageGenerated)?.contentDescription ?: "Edited image" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
.first()is not safe as it will throw aNoSuchElementExceptionif theimageslist is empty, causing the app to crash. The API might return an empty list even on a successful response. UsingfirstOrNull()and then handling thenullcase by throwing a more descriptive exception is a more robust approach.