forked from vfsfitvnm/ViMusic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve compose-reordering (vfsfitvnm#176)
- Loading branch information
Showing
8 changed files
with
539 additions
and
194 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
153 changes: 0 additions & 153 deletions
153
compose-reordering/src/main/kotlin/it/vfsfitvnm/reordering/DragToReorder.kt
This file was deleted.
Oops, something went wrong.
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
51 changes: 51 additions & 0 deletions
51
compose-reordering/src/main/kotlin/it/vfsfitvnm/reordering/Reorder.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,51 @@ | ||
package it.vfsfitvnm.reordering | ||
|
||
import androidx.compose.foundation.gestures.detectDragGestures | ||
import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.input.pointer.PointerInputChange | ||
import androidx.compose.ui.input.pointer.PointerInputScope | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
|
||
private fun Modifier.reorder( | ||
reorderingState: ReorderingState, | ||
index: Int, | ||
detectDragGestures: DetectDragGestures, | ||
): Modifier = pointerInput(reorderingState) { | ||
with(detectDragGestures) { | ||
detectDragGestures( | ||
onDragStart = { reorderingState.onDragStart(index) }, | ||
onDrag = reorderingState::onDrag, | ||
onDragEnd = reorderingState::onDragEnd, | ||
onDragCancel = reorderingState::onDragEnd, | ||
) | ||
} | ||
} | ||
|
||
fun Modifier.reorder( | ||
reorderingState: ReorderingState, | ||
index: Int, | ||
): Modifier = reorder( | ||
reorderingState = reorderingState, | ||
index = index, | ||
detectDragGestures = PointerInputScope::detectDragGestures, | ||
) | ||
|
||
fun Modifier.reorderAfterLongPress( | ||
reorderingState: ReorderingState, | ||
index: Int | ||
): Modifier = reorder( | ||
reorderingState = reorderingState, | ||
index = index, | ||
detectDragGestures = PointerInputScope::detectDragGesturesAfterLongPress, | ||
) | ||
|
||
private fun interface DetectDragGestures { | ||
suspend fun PointerInputScope.detectDragGestures( | ||
onDragStart: (Offset) -> Unit, | ||
onDragEnd: () -> Unit, | ||
onDragCancel: () -> Unit, | ||
onDrag: (change: PointerInputChange, dragAmount: Offset) -> Unit | ||
) | ||
} |
41 changes: 41 additions & 0 deletions
41
compose-reordering/src/main/kotlin/it/vfsfitvnm/reordering/ReorderingLazyColumn.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,41 @@ | ||
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") | ||
|
||
package it.vfsfitvnm.reordering | ||
|
||
import androidx.compose.foundation.gestures.FlingBehavior | ||
import androidx.compose.foundation.gestures.ScrollableDefaults | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.lazy.LazyListScope | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun ReorderingLazyColumn( | ||
reorderingState: ReorderingState, | ||
modifier: Modifier = Modifier, | ||
contentPadding: PaddingValues = PaddingValues(0.dp), | ||
reverseLayout: Boolean = false, | ||
verticalArrangement: Arrangement.Vertical = | ||
if (!reverseLayout) Arrangement.Top else Arrangement.Bottom, | ||
horizontalAlignment: Alignment.Horizontal = Alignment.Start, | ||
flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(), | ||
userScrollEnabled: Boolean = true, | ||
content: LazyListScope.() -> Unit | ||
) { | ||
ReorderingLazyList( | ||
modifier = modifier, | ||
state = reorderingState.lazyListState, | ||
reorderingState = reorderingState, | ||
contentPadding = contentPadding, | ||
flingBehavior = flingBehavior, | ||
horizontalAlignment = horizontalAlignment, | ||
verticalArrangement = verticalArrangement, | ||
isVertical = true, | ||
reverseLayout = reverseLayout, | ||
userScrollEnabled = userScrollEnabled, | ||
content = content | ||
) | ||
} |
Oops, something went wrong.