Skip to content

Commit bd5ad9a

Browse files
Add files via upload
1 parent 070de35 commit bd5ad9a

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/feature/multimodal/PhotoReasoningScreen.kt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import coil.request.SuccessResult
5151
import com.google.ai.sample.GenerativeViewModelFactory
5252
import coil.size.Precision
5353
import com.google.ai.sample.R
54-
import com.google.ai.sample.ScreenshotManager
54+
import com.google.ai.sample.ScreenshotBridge
5555
import com.google.ai.sample.util.UriSaver
5656
import kotlinx.coroutines.launch
5757
import android.content.Context
@@ -76,37 +76,39 @@ internal fun PhotoReasoningRoute(
7676
onReasonClicked = { inputText, selectedItems ->
7777
coroutineScope.launch {
7878
// Take screenshot when Go button is pressed
79-
val screenshotManager = ScreenshotManager.getInstance(context)
79+
val screenshotBridge = ScreenshotBridge.getInstance(context)
8080

8181
// Use a callback approach with non-blocking behavior
82-
screenshotManager.takeScreenshot { bitmap ->
83-
if (bitmap != null) {
84-
// Save screenshot to file
85-
val screenshotFile = screenshotManager.saveBitmapToFile(bitmap)
86-
if (screenshotFile != null) {
87-
// Add screenshot URI to selected items
88-
val updatedItems = selectedItems.toMutableList()
89-
updatedItems.add(screenshotFile.toUri())
90-
91-
// Process all images including screenshot within the coroutine scope
92-
coroutineScope.launch {
93-
processImagesAndReason(updatedItems, inputText, imageRequestBuilder, imageLoader, viewModel)
82+
screenshotBridge.takeScreenshot(object : ScreenshotBridge.ScreenshotCallback {
83+
override fun onScreenshotTaken(bitmap: Bitmap?) {
84+
if (bitmap != null) {
85+
// Save screenshot to file
86+
val screenshotFile = screenshotBridge.saveBitmapToFile(bitmap)
87+
if (screenshotFile != null) {
88+
// Add screenshot URI to selected items
89+
val updatedItems = selectedItems.toMutableList()
90+
updatedItems.add(screenshotFile.toUri())
91+
92+
// Process all images including screenshot within the coroutine scope
93+
coroutineScope.launch {
94+
processImagesAndReason(updatedItems, inputText, imageRequestBuilder, imageLoader, viewModel)
95+
}
96+
} else {
97+
// If screenshot saving failed, proceed with original images
98+
coroutineScope.launch {
99+
processImagesAndReason(selectedItems, inputText, imageRequestBuilder, imageLoader, viewModel)
100+
}
101+
Toast.makeText(context, "Failed to save screenshot", Toast.LENGTH_SHORT).show()
94102
}
95103
} else {
96-
// If screenshot saving failed, proceed with original images
104+
// If screenshot failed, proceed with original images
97105
coroutineScope.launch {
98106
processImagesAndReason(selectedItems, inputText, imageRequestBuilder, imageLoader, viewModel)
99107
}
100-
Toast.makeText(context, "Failed to save screenshot", Toast.LENGTH_SHORT).show()
108+
Toast.makeText(context, "Failed to take screenshot", Toast.LENGTH_SHORT).show()
101109
}
102-
} else {
103-
// If screenshot failed, proceed with original images
104-
coroutineScope.launch {
105-
processImagesAndReason(selectedItems, inputText, imageRequestBuilder, imageLoader, viewModel)
106-
}
107-
Toast.makeText(context, "Failed to take screenshot", Toast.LENGTH_SHORT).show()
108110
}
109-
}
111+
})
110112
}
111113
}
112114
)

0 commit comments

Comments
 (0)