|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 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 | + * https://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 | +@file:OptIn( |
| 17 | + ExperimentalLayoutApi::class, |
| 18 | + ExperimentalSharedTransitionApi::class, |
| 19 | + ExperimentalMaterial3ExpressiveApi::class, |
| 20 | + ExperimentalMaterial3Api::class, |
| 21 | +) |
| 22 | + |
| 23 | +package com.android.developers.androidify.creation |
| 24 | + |
| 25 | +import android.net.Uri |
| 26 | +import androidx.activity.ComponentActivity |
| 27 | +import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia |
| 28 | +import androidx.compose.animation.ExperimentalSharedTransitionApi |
| 29 | +import androidx.compose.foundation.layout.Box |
| 30 | +import androidx.compose.foundation.layout.ExperimentalLayoutApi |
| 31 | +import androidx.compose.foundation.layout.PaddingValues |
| 32 | +import androidx.compose.foundation.layout.fillMaxSize |
| 33 | +import androidx.compose.foundation.layout.imePadding |
| 34 | +import androidx.compose.foundation.layout.padding |
| 35 | +import androidx.compose.foundation.layout.safeContentPadding |
| 36 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 37 | +import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi |
| 38 | +import androidx.compose.material3.MaterialTheme |
| 39 | +import androidx.compose.material3.Scaffold |
| 40 | +import androidx.compose.material3.Snackbar |
| 41 | +import androidx.compose.material3.SnackbarDefaults |
| 42 | +import androidx.compose.material3.SnackbarHost |
| 43 | +import androidx.compose.material3.SnackbarHostState |
| 44 | +import androidx.compose.runtime.Composable |
| 45 | +import androidx.compose.ui.Modifier |
| 46 | +import androidx.compose.ui.draganddrop.DragAndDropEvent |
| 47 | +import androidx.compose.ui.draganddrop.DragAndDropTarget |
| 48 | +import androidx.compose.ui.unit.dp |
| 49 | +import com.android.developers.androidify.data.DropBehaviourFactory |
| 50 | +import com.android.developers.androidify.theme.AndroidifyTheme |
| 51 | +import com.android.developers.androidify.theme.SharedElementContextPreview |
| 52 | +import com.android.developers.androidify.theme.components.AboutButton |
| 53 | +import com.android.developers.androidify.theme.components.AndroidifyTopAppBar |
| 54 | +import com.android.developers.androidify.theme.components.SquiggleBackground |
| 55 | +import com.android.developers.androidify.util.AdaptivePreview |
| 56 | +import com.android.developers.androidify.util.isAtLeastMedium |
| 57 | + |
| 58 | +@Composable |
| 59 | +fun EditScreen( |
| 60 | + snackbarHostState: SnackbarHostState, |
| 61 | + dropBehaviourFactory: DropBehaviourFactory, |
| 62 | + isExpanded: Boolean = isAtLeastMedium(), |
| 63 | + onCameraPressed: () -> Unit, |
| 64 | + onBackPressed: () -> Unit, |
| 65 | + onAboutPressed: () -> Unit, |
| 66 | + uiState: CreationState, |
| 67 | + onChooseImageClicked: (PickVisualMedia.VisualMediaType) -> Unit, |
| 68 | + onPromptOptionSelected: (PromptType) -> Unit, |
| 69 | + onUndoPressed: () -> Unit, |
| 70 | + onPromptGenerationPressed: () -> Unit, |
| 71 | + onBotColorSelected: (BotColor) -> Unit, |
| 72 | + onStartClicked: () -> Unit, |
| 73 | + onDropCallback: (Uri) -> Unit = {}, |
| 74 | +) { |
| 75 | + EditScreenScaffold( |
| 76 | + snackbarHostState, |
| 77 | + topBar = { |
| 78 | + AndroidifyTopAppBar( |
| 79 | + backEnabled = true, |
| 80 | + isMediumWindowSize = isExpanded, |
| 81 | + onBackPressed = onBackPressed, |
| 82 | + expandedCenterButtons = { |
| 83 | + PromptTypeToolbar( |
| 84 | + uiState.selectedPromptOption, |
| 85 | + modifier = Modifier.padding(start = 16.dp, end = 16.dp), |
| 86 | + onOptionSelected = onPromptOptionSelected, |
| 87 | + ) |
| 88 | + }, |
| 89 | + actions = { |
| 90 | + AboutButton { onAboutPressed() } |
| 91 | + } |
| 92 | + ) |
| 93 | + }, |
| 94 | + ) { contentPadding -> |
| 95 | + SquiggleBackground(offsetHeightFraction = 0.5f) |
| 96 | + Box( |
| 97 | + modifier = Modifier |
| 98 | + .fillMaxSize() |
| 99 | + .padding(contentPadding) |
| 100 | + .imePadding(), |
| 101 | + ) { |
| 102 | + if (isExpanded) { |
| 103 | + EditScreenContentsMedium( |
| 104 | + dropBehaviourFactory, |
| 105 | + onCameraPressed, |
| 106 | + uiState, |
| 107 | + onChooseImageClicked, |
| 108 | + onPromptOptionSelected, |
| 109 | + onUndoPressed, |
| 110 | + onPromptGenerationPressed, |
| 111 | + onBotColorSelected, |
| 112 | + onStartClicked, |
| 113 | + onDropCallback, |
| 114 | + ) |
| 115 | + } else { |
| 116 | + EditScreenContentsCompact( |
| 117 | + dropBehaviourFactory, |
| 118 | + onCameraPressed, |
| 119 | + uiState, |
| 120 | + onChooseImageClicked, |
| 121 | + onPromptOptionSelected, |
| 122 | + onUndoPressed, |
| 123 | + onPromptGenerationPressed, |
| 124 | + onBotColorSelected, |
| 125 | + onStartClicked, |
| 126 | + onDropCallback, |
| 127 | + ) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +@Composable |
| 134 | +fun EditScreenScaffold( |
| 135 | + snackbarHostState: SnackbarHostState, |
| 136 | + topBar: @Composable () -> Unit = {}, |
| 137 | + content: @Composable (PaddingValues) -> Unit, |
| 138 | +) { |
| 139 | + Scaffold( |
| 140 | + containerColor = MaterialTheme.colorScheme.surface, |
| 141 | + snackbarHost = { |
| 142 | + SnackbarHost( |
| 143 | + hostState = snackbarHostState, |
| 144 | + snackbar = { snackbarData -> |
| 145 | + Snackbar( |
| 146 | + snackbarData, |
| 147 | + shape = SnackbarDefaults.shape, |
| 148 | + modifier = Modifier.padding(4.dp), |
| 149 | + ) |
| 150 | + }, |
| 151 | + modifier = Modifier.safeContentPadding(), |
| 152 | + ) |
| 153 | + }, |
| 154 | + topBar = topBar, |
| 155 | + content = content, |
| 156 | + ) |
| 157 | +} |
| 158 | + |
| 159 | +@Composable |
| 160 | +@AdaptivePreview |
| 161 | +private fun EditScreenPreview() { |
| 162 | + AndroidifyTheme { |
| 163 | + SharedElementContextPreview { |
| 164 | + EditScreen( |
| 165 | + snackbarHostState = SnackbarHostState(), |
| 166 | + dropBehaviourFactory = fakeDropBehaviourFactory, |
| 167 | + onCameraPressed = { }, |
| 168 | + uiState = CreationState(), |
| 169 | + onChooseImageClicked = {}, |
| 170 | + onPromptOptionSelected = {}, |
| 171 | + onUndoPressed = {}, |
| 172 | + onPromptGenerationPressed = {}, |
| 173 | + onBotColorSelected = {}, |
| 174 | + onStartClicked = {}, |
| 175 | + onDropCallback = {}, |
| 176 | + onBackPressed = {}, |
| 177 | + onAboutPressed = {}, |
| 178 | + ) |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +val fakeDropBehaviourFactory = object : DropBehaviourFactory { |
| 184 | + override fun shouldStartDragAndDrop(event: DragAndDropEvent): Boolean { |
| 185 | + TODO("Stub") |
| 186 | + } |
| 187 | + |
| 188 | + override fun createTargetCallback( |
| 189 | + activity: ComponentActivity, |
| 190 | + onImageDropped: (Uri) -> Unit, |
| 191 | + onDropStarted: () -> Unit, |
| 192 | + onDropEnded: () -> Unit, |
| 193 | + ): DragAndDropTarget { |
| 194 | + TODO("Stub") |
| 195 | + } |
| 196 | +} |
0 commit comments