-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I4fa2a9b3d1d56fa1773b52c2cf7d3ad6f0d49d16
- Loading branch information
1 parent
b720b8b
commit a063171
Showing
6 changed files
with
205 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
.../draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDropUsingCompose.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.platform.ui.draganddrop | ||
|
||
import android.content.ClipData | ||
import android.content.ClipDescription | ||
import androidx.compose.foundation.ExperimentalFoundationApi | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.draganddrop.dragAndDropSource | ||
import androidx.compose.foundation.draganddrop.dragAndDropTarget | ||
import androidx.compose.foundation.gestures.detectTapGestures | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.lazy.grid.GridCells | ||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid | ||
import androidx.compose.material3.HorizontalDivider | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateListOf | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draganddrop.DragAndDropEvent | ||
import androidx.compose.ui.draganddrop.DragAndDropTarget | ||
import androidx.compose.ui.draganddrop.DragAndDropTransferData | ||
import androidx.compose.ui.draganddrop.mimeTypes | ||
import androidx.compose.ui.draganddrop.toAndroidDragEvent | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.dp | ||
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi | ||
import com.bumptech.glide.integration.compose.GlideImage | ||
import com.google.android.catalog.framework.annotations.Sample | ||
|
||
@Sample( | ||
name = "Drag and Drop in Compose", | ||
description = "Drag and drop in Compose", | ||
documentation = "", | ||
) | ||
@Composable | ||
fun DragAndDropCompose() { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(8.dp), | ||
) { | ||
DragBox(Modifier.weight(1f)) | ||
HorizontalDivider() | ||
DropBox(Modifier.weight(2f)) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalFoundationApi::class) | ||
@Composable | ||
fun DropBox(modifier: Modifier) { | ||
val targetPhotoUrls = remember { | ||
mutableStateListOf<String>() | ||
} | ||
var backgroundColor by remember { | ||
mutableStateOf(Color(0xffE5E4E2)) | ||
} | ||
val dragAndDropTarget = remember { | ||
object : DragAndDropTarget { | ||
override fun onDrop(event: DragAndDropEvent): Boolean { | ||
val data = event.toAndroidDragEvent().clipData.getItemAt(0).text | ||
targetPhotoUrls.add(data.toString()) | ||
return true | ||
} | ||
|
||
override fun onEntered(event: DragAndDropEvent) { | ||
super.onEntered(event) | ||
backgroundColor = Color(0xffD3D3D3) | ||
} | ||
|
||
override fun onEnded(event: DragAndDropEvent) { | ||
super.onExited(event) | ||
backgroundColor = Color(0xffE5E4E2) | ||
} | ||
|
||
override fun onExited(event: DragAndDropEvent) { | ||
super.onExited(event) | ||
backgroundColor = Color(0xffE5E4E2) | ||
} | ||
} | ||
} | ||
Box( | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.padding(8.dp) | ||
.background(color = backgroundColor) | ||
.dragAndDropTarget( | ||
shouldStartDragAndDrop = { event -> | ||
event | ||
.mimeTypes() | ||
.contains(ClipDescription.MIMETYPE_TEXT_PLAIN) | ||
}, | ||
target = dragAndDropTarget, | ||
), | ||
) { | ||
LazyVerticalGrid( | ||
columns = GridCells.Adaptive(minSize = 100.dp), | ||
verticalArrangement = Arrangement.spacedBy(10.dp), | ||
horizontalArrangement = Arrangement.spacedBy(10.dp), | ||
) { | ||
items(targetPhotoUrls.size) { index -> | ||
Photo(targetPhotoUrls[index]) | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun DragBox(modifier: Modifier) { | ||
Box( | ||
modifier = modifier.fillMaxWidth(), | ||
) { | ||
val sourcePhotoUrls = remember { | ||
mutableStateListOf( | ||
"https://services.google.com/fh/files/misc/qq10.jpeg", | ||
"https://services.google.com/fh/files/misc/qq9.jpeg", | ||
"https://services.google.com/fh/files/misc/qq8.jpeg", | ||
) | ||
} | ||
LazyVerticalGrid( | ||
columns = GridCells.Adaptive(minSize = 100.dp), | ||
verticalArrangement = Arrangement.spacedBy(10.dp), | ||
horizontalArrangement = Arrangement.spacedBy(10.dp), | ||
) { | ||
items(sourcePhotoUrls.size) { index -> | ||
Photo(sourcePhotoUrls[index]) | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalGlideComposeApi::class, ExperimentalFoundationApi::class) | ||
@Composable | ||
fun Photo(urlStr: String) { | ||
val url by remember { | ||
mutableStateOf(urlStr) | ||
} | ||
GlideImage( | ||
model = url, | ||
contentDescription = "demo", | ||
modifier = Modifier | ||
.size(100.dp) | ||
.dragAndDropSource { | ||
detectTapGestures( | ||
onLongPress = { | ||
startTransfer( | ||
DragAndDropTransferData( | ||
ClipData.newPlainText( | ||
"image Url", url, | ||
), | ||
), | ||
) | ||
}, | ||
) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters