Skip to content

Commit

Permalink
Merge pull request #19 from MohamedRejeb/0.2.x
Browse files Browse the repository at this point in the history
Remove duplication in DraggableItem and ReorderableItem
  • Loading branch information
MohamedRejeb authored Sep 16, 2024
2 parents d8307ae + e3a84e9 commit 814d714
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2023, Mohamed Ben Rejeb and the Compose Dnd project contributors
*
* 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
*
* http://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.mohamedrejeb.compose.dnd.drag

import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.SpringSpec
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.key
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onPlaced
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.unit.toSize
import com.mohamedrejeb.compose.dnd.DragAndDropState
import com.mohamedrejeb.compose.dnd.gesture.detectDragStartGesture

/**
* Wrapper Composable for draggable item.
*
* @param modifier - modifier for the DraggableItem composable
* @param key - unique key for this item
* @param data - data that will be passed to drop target on drop
* @param state - state of the drag and drop
* @param enabled - whether the drag and drop is enabled
* @param dragAfterLongPress if true, drag will start after long press, otherwise drag will start after simple press
* @param dropTargets - list of drop targets ids to which this item can be dropped, if empty, item can be dropped to any drop target
* @param dropStrategy - strategy to determine the drop target
* @param dropAnimationSpec - animation spec for the position drop animation
* @param sizeDropAnimationSpec - animation spec for the size drop animation
* @param draggableContent The content of the draggable item
* @param content - content that will be shown when item is not dragged
*/
@Composable
internal fun <T> CoreDraggableItem(
modifier: Modifier = Modifier,
key: Any,
data: T,
state: DragAndDropState<T>,
enabled: Boolean = true,
dragAfterLongPress: Boolean = state.dragAfterLongPress,
dropTargets: List<Any> = emptyList(),
dropStrategy: DropStrategy = DropStrategy.SurfacePercentage,
dropAnimationSpec: AnimationSpec<Offset> = SpringSpec(),
sizeDropAnimationSpec: AnimationSpec<Size> = SpringSpec(),
draggableContent: @Composable () -> Unit,
content: @Composable () -> Unit,
) {
val draggableItemState = remember(key) {
DraggableItemState(
key = key,
data = data,
positionInRoot = Offset.Zero,
size = Size.Zero,
dropTargets = dropTargets,
dropStrategy = dropStrategy,
dropAnimationSpec = dropAnimationSpec,
sizeDropAnimationSpec = sizeDropAnimationSpec,
content = draggableContent,
)
}

LaunchedEffect(draggableItemState, data) {
draggableItemState.data = data
}

LaunchedEffect(draggableItemState, dropTargets) {
draggableItemState.dropTargets = dropTargets
}

LaunchedEffect(draggableItemState, dropStrategy) {
draggableItemState.dropStrategy = dropStrategy
}

LaunchedEffect(draggableItemState, dropAnimationSpec) {
draggableItemState.dropAnimationSpec = dropAnimationSpec
}

LaunchedEffect(draggableItemState, sizeDropAnimationSpec) {
draggableItemState.sizeDropAnimationSpec = sizeDropAnimationSpec
}

LaunchedEffect(draggableItemState, draggableContent) {
draggableItemState.content = draggableContent
}

DisposableEffect(key, state, draggableItemState) {
state.addOrUpdateDraggableItem(draggableItemState)

onDispose {
state.removeDraggableItem(key)
}
}

Box(
modifier = modifier
.onPlaced {
draggableItemState.positionInRoot = it.positionInRoot()
}
.onSizeChanged {
draggableItemState.size = it.toSize()
}
.pointerInput(enabled, key, state, state.enabled) {
detectDragStartGesture(
key = key,
state = state,
enabled = enabled && state.enabled,
dragAfterLongPress = dragAfterLongPress,
)
},
) {
content()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@ package com.mohamedrejeb.compose.dnd.drag

import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.SpringSpec
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.key
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.unit.toSize
import com.mohamedrejeb.compose.dnd.DragAndDropState
import com.mohamedrejeb.compose.dnd.gesture.detectDragStartGesture

/**
* Wrapper Composable for draggable item.
Expand Down Expand Up @@ -63,32 +56,6 @@ fun <T> DraggableItem(
draggableContent: (@Composable () -> Unit)? = null,
content: @Composable DraggableItemScope.() -> Unit,
) {
LaunchedEffect(key, state, data) {
state.draggableItemMap[key]?.data = data
}

LaunchedEffect(key, state, dropTargets) {
state.draggableItemMap[key]?.dropTargets = dropTargets
}

LaunchedEffect(key, state, dropStrategy) {
state.draggableItemMap[key]?.dropStrategy = dropStrategy
}

LaunchedEffect(key, state, dropAnimationSpec) {
state.draggableItemMap[key]?.dropAnimationSpec = dropAnimationSpec
}

LaunchedEffect(key, state, sizeDropAnimationSpec) {
state.draggableItemMap[key]?.sizeDropAnimationSpec = sizeDropAnimationSpec
}

DisposableEffect(key, state) {
onDispose {
state.removeDraggableItem(key)
}
}

val draggableItemScopeImpl = remember(key, state) {
DraggableItemScopeImpl(
key = key,
Expand All @@ -102,39 +69,24 @@ fun <T> DraggableItem(
)
}

with(draggableItemScopeImpl) {
Box(
modifier = modifier
.onGloballyPositioned {
val draggableItemState = DraggableItemState(
key = key,
data = data,
positionInRoot = it.positionInRoot(),
size = it.size.toSize(),
dropTargets = dropTargets,
dropStrategy = dropStrategy,
dropAnimationSpec = dropAnimationSpec,
sizeDropAnimationSpec = sizeDropAnimationSpec,
content = draggableContent ?: {
with(draggableItemScopeShadowImpl) {
content()
}
},
)

state.addOrUpdateDraggableItem(
state = draggableItemState,
)
}
.pointerInput(enabled, key, state, state.enabled) {
detectDragStartGesture(
key = key,
state = state,
enabled = enabled && state.enabled,
dragAfterLongPress = dragAfterLongPress,
)
},
) {
CoreDraggableItem(
modifier = modifier,
key = key,
data = data,
state = state,
enabled = enabled,
dragAfterLongPress = dragAfterLongPress,
dropTargets = dropTargets,
dropStrategy = dropStrategy,
dropAnimationSpec = dropAnimationSpec,
sizeDropAnimationSpec = sizeDropAnimationSpec,
draggableContent = draggableContent ?: {
with(draggableItemScopeShadowImpl) {
content()
}
},
) {
with(draggableItemScopeImpl) {
content()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal class DraggableItemState<T>(
var positionInRoot: Offset,
var size: Size,

val content: @Composable () -> Unit,
var content: @Composable () -> Unit,
) {
fun copy(): DraggableItemState<T> {
return DraggableItemState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,16 @@ package com.mohamedrejeb.compose.dnd.reorder

import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.SpringSpec
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.unit.toSize
import com.mohamedrejeb.compose.dnd.annotation.ExperimentalDndApi
import com.mohamedrejeb.compose.dnd.drag.DraggableItemState
import com.mohamedrejeb.compose.dnd.drag.CoreDraggableItem
import com.mohamedrejeb.compose.dnd.drag.DraggedItemState
import com.mohamedrejeb.compose.dnd.drag.DropStrategy
import com.mohamedrejeb.compose.dnd.drop.dropTarget
import com.mohamedrejeb.compose.dnd.gesture.detectDragStartGesture

/**
* Mark this composable as a reorderable item.
Expand Down Expand Up @@ -79,32 +71,6 @@ fun <T> ReorderableItem(
draggableContent: (@Composable () -> Unit)? = null,
content: @Composable ReorderableItemScope.() -> Unit,
) {
LaunchedEffect(key, state, data) {
state.dndState.draggableItemMap[key]?.data = data
}

LaunchedEffect(key, state, dropTargets) {
state.dndState.draggableItemMap[key]?.dropTargets = dropTargets
}

LaunchedEffect(key, state, dropStrategy) {
state.dndState.draggableItemMap[key]?.dropStrategy = dropStrategy
}

LaunchedEffect(key, state, dropAnimationSpec) {
state.dndState.draggableItemMap[key]?.dropAnimationSpec = dropAnimationSpec
}

LaunchedEffect(key, state, sizeDropAnimationSpec) {
state.dndState.draggableItemMap[key]?.sizeDropAnimationSpec = sizeDropAnimationSpec
}

DisposableEffect(key, state) {
onDispose {
state.dndState.removeDraggableItem(key)
}
}

val reorderableItemScopeImpl = remember(key, state) {
ReorderableItemScopeImpl(
key = key,
Expand All @@ -118,46 +84,30 @@ fun <T> ReorderableItem(
)
}

Box(
modifier = Modifier
.onGloballyPositioned {
val draggableItemState = DraggableItemState(
key = key,
data = data,
positionInRoot = it.positionInRoot(),
size = it.size.toSize(),
dropTargets = dropTargets,
dropStrategy = dropStrategy,
dropAnimationSpec = dropAnimationSpec,
sizeDropAnimationSpec = sizeDropAnimationSpec,
content = draggableContent ?: {
with(reorderableItemScopeShadowImpl) {
content()
}
},
)

state.dndState.addOrUpdateDraggableItem(
state = draggableItemState,
)
}
.pointerInput(enabled, key, state, state.dndState.enabled) {
detectDragStartGesture(
key = key,
state = state.dndState,
enabled = enabled && state.dndState.enabled,
dragAfterLongPress = dragAfterLongPress,
)
}
CoreDraggableItem(
modifier = modifier
.dropTarget(
key = key,
state = state.dndState,
zIndex = zIndex,
onDrop = onDrop,
onDragEnter = onDragEnter,
onDragExit = onDragExit,
)
.then(modifier),
),
key = key,
data = data,
state = state.dndState,
enabled = enabled,
dragAfterLongPress = dragAfterLongPress,
dropTargets = dropTargets,
dropStrategy = dropStrategy,
dropAnimationSpec = dropAnimationSpec,
sizeDropAnimationSpec = sizeDropAnimationSpec,
draggableContent = draggableContent ?: {
with(reorderableItemScopeShadowImpl) {
content()
}
},
) {
with(reorderableItemScopeImpl) {
content()
Expand Down

0 comments on commit 814d714

Please sign in to comment.