Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package com.android.developers.androidify.creation
import android.net.Uri
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.foundation.layout.ExperimentalLayoutApi
Expand All @@ -52,12 +51,12 @@ fun CreationScreen(
) {
creationViewModel.onBackPress()
}
val pickMedia = rememberLauncherForActivityResult(PickVisualMedia()) { uri ->
val snackbarHostState by creationViewModel.snackbarHostState.collectAsStateWithLifecycle()
val launcher = rememberLauncherForActivityResult(PickVisualMedia()) { uri ->
if (uri != null) {
creationViewModel.onImageSelected(uri)
}
}
val snackbarHostState by creationViewModel.snackbarHostState.collectAsStateWithLifecycle()

LaunchedEffect(uiState.resultBitmapUri) {
uiState.resultBitmapUri?.let { resultBitmapUri ->
Expand All @@ -84,7 +83,9 @@ fun CreationScreen(
onBackPressed = onBackPressed,
onAboutPressed = onAboutPressed,
uiState = uiState,
onChooseImageClicked = { pickMedia.launch(PickVisualMediaRequest(it)) },
onChooseImageClicked = {
creationViewModel.launchImageChooser(launcher)
},
onPromptOptionSelected = creationViewModel::onSelectedPromptOptionChanged,
onUndoPressed = creationViewModel::onUndoPressed,
onPromptGenerationPressed = creationViewModel::onPromptGenerationClicked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package com.android.developers.androidify.creation

import android.content.Context
import android.net.Uri
import androidx.activity.compose.ManagedActivityResultLauncher
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.material3.SnackbarHostState
import androidx.compose.ui.graphics.Color
Expand Down Expand Up @@ -93,6 +96,15 @@ class CreationViewModel @AssistedInject constructor(
}
}

fun launchImageChooser(launcher: ManagedActivityResultLauncher<PickVisualMediaRequest, Uri?>) {
try {
launcher.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally we've been trying to avoid starting activities from the ViewModel as the ViewModel can outlive the Fragment/Composable. Is it possible to keep this in the Composable? Also why doesn't the device have a media picker 😄

} catch (exception: Exception) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Catching a generic Exception is too broad and can hide other unexpected issues. Since the goal is to handle the case where no media picker activity is available, it's better to catch the more specific android.content.ActivityNotFoundException.

Suggested change
} catch (exception: Exception) {
} catch (exception: android.content.ActivityNotFoundException) {

Timber.e(exception, "Failed to open the visual media picker")
displayOpenImageChooserError()
}
}

fun onBotColorChanged(botColor: BotColor) {
_uiState.update {
it.copy(botColor = botColor)
Expand Down Expand Up @@ -132,6 +144,12 @@ class CreationViewModel @AssistedInject constructor(
}
}

fun displayOpenImageChooserError() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function is only used within the CreationViewModel. It's good practice to make it private to improve encapsulation.

Suggested change
fun displayOpenImageChooserError() {
private fun displayOpenImageChooserError() {

viewModelScope.launch {
_snackbarHostState.value.showSnackbar(context.getString(R.string.error_open_visual_media_picker))
}
}

fun startClicked() {
imageGenerationJob?.cancel()
imageGenerationJob = viewModelScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package com.android.developers.androidify.creation

import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ExperimentalLayoutApi
Expand Down Expand Up @@ -67,7 +66,7 @@ fun EditScreen(
onBackPressed: () -> Unit,
onAboutPressed: () -> Unit,
uiState: CreationState,
onChooseImageClicked: (PickVisualMedia.VisualMediaType) -> Unit,
onChooseImageClicked: () -> Unit,
onPromptOptionSelected: (PromptType) -> Unit,
onUndoPressed: () -> Unit,
onPromptGenerationPressed: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
package com.android.developers.androidify.creation

import android.net.Uri
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -71,7 +70,7 @@ fun EditScreenContentsCompact(
dropBehaviourFactory: DropBehaviourFactory,
onCameraPressed: () -> Unit,
uiState: CreationState,
onChooseImageClicked: (PickVisualMedia.VisualMediaType) -> Unit,
onChooseImageClicked: () -> Unit,
onPromptOptionSelected: (PromptType) -> Unit,
onUndoPressed: () -> Unit,
onPromptGenerationPressed: () -> Unit,
Expand All @@ -97,9 +96,7 @@ fun EditScreenContentsCompact(
dropBehaviourFactory = dropBehaviourFactory,
modifier = Modifier.weight(1f),
onCameraPressed = onCameraPressed,
onChooseImageClicked = {
onChooseImageClicked(PickVisualMedia.ImageOnly)
},
onChooseImageClicked = onChooseImageClicked,
onUndoPressed = onUndoPressed,
onPromptGenerationPressed = onPromptGenerationPressed,
onSelectedPromptOptionChanged = onPromptOptionSelected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
package com.android.developers.androidify.creation

import android.net.Uri
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.foundation.background
import androidx.compose.foundation.border
Expand Down Expand Up @@ -56,7 +55,7 @@ fun EditScreenContentsMedium(
dropBehaviourFactory: DropBehaviourFactory,
onCameraPressed: () -> Unit,
uiState: CreationState,
onChooseImageClicked: (PickVisualMedia.VisualMediaType) -> Unit,
onChooseImageClicked: () -> Unit,
onPromptOptionSelected: (PromptType) -> Unit,
onUndoPressed: () -> Unit,
onPromptGenerationPressed: () -> Unit,
Expand All @@ -79,9 +78,7 @@ fun EditScreenContentsMedium(
dropBehaviourFactory = dropBehaviourFactory,
modifier = Modifier.weight(.6f),
onCameraPressed = onCameraPressed,
onChooseImageClicked = {
onChooseImageClicked(PickVisualMedia.ImageOnly)
},
onChooseImageClicked = onChooseImageClicked,
onUndoPressed = onUndoPressed,
onPromptGenerationPressed = onPromptGenerationPressed,
onSelectedPromptOptionChanged = onPromptOptionSelected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.android.developers.androidify.creation.xr

import android.net.Uri
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -66,7 +65,7 @@ fun EditScreenSpatial(
onAboutPressed: () -> Unit,
uiState: CreationState,
snackbarHostState: SnackbarHostState,
onChooseImageClicked: (PickVisualMedia.VisualMediaType) -> Unit,
onChooseImageClicked: () -> Unit,
onPromptOptionSelected: (PromptType) -> Unit,
onUndoPressed: () -> Unit,
onPromptGenerationPressed: () -> Unit,
Expand Down Expand Up @@ -131,9 +130,7 @@ fun EditScreenSpatial(
uiState = uiState,
dropBehaviourFactory = dropBehaviourFactory,
onCameraPressed = onCameraPressed,
onChooseImageClicked = {
onChooseImageClicked(PickVisualMedia.ImageOnly)
},
onChooseImageClicked = onChooseImageClicked,
onUndoPressed = onUndoPressed,
onPromptGenerationPressed = onPromptGenerationPressed,
onSelectedPromptOptionChanged = onPromptOptionSelected,
Expand Down
1 change: 1 addition & 0 deletions feature/creation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<string name="error_upload_generic">An error occurred whilst uploading, try again later.</string>
<string name="error_image_validation">The image is not a valid picture of a person.</string>
<string name="error_choose_image_prompt">Choose an image or use a prompt instead.</string>
<string name="error_open_visual_media_picker">An error occurred whilst opening the media picker.</string>
<string-array name="generation_prompts">
<item>Oh, this one is going to be nice…</item>
<item>Now… onto the spray paint</item>
Expand Down