-
Notifications
You must be signed in to change notification settings - Fork 7
/
ReorderableLazyList.kt
325 lines (304 loc) · 15.9 KB
/
ReorderableLazyList.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright 2023 Calvin Liang
*
* 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 sh.calvin.reorderable
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.animateScrollBy
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.lazy.LazyItemScope
import androidx.compose.foundation.lazy.LazyListItemInfo
import androidx.compose.foundation.lazy.LazyListLayoutInfo
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import kotlinx.coroutines.CoroutineScope
/**
* Creates a [ReorderableLazyListState] that is remembered across compositions.
*
* Changes to [lazyListState], [scrollThresholdPadding], [scrollThreshold], and [scroller] will result in [ReorderableLazyListState] being updated.
*
* @param lazyListState The return value of [rememberLazyListState](androidx.compose.foundation.lazy.LazyListStateKt.rememberLazyListState)
* @param scrollThresholdPadding The padding that will be added to the top and bottom of the list to determine the scrollThreshold. Useful for when the grid is displayed under the navigation bar or notification bar.
* @param scrollThreshold The distance in dp from the top or bottom of the list that will trigger scrolling
* @param scroller The [Scroller] that will be used to scroll the list. Use [rememberScroller](sh.calvin.reorderable.ScrollerKt.rememberScroller) to create a [Scroller].
* @param onMove The function that is called when an item is moved. Make sure this function returns only after the items are moved. This suspend function is invoked with the `rememberReorderableLazyColumnState` scope, allowing for async processing, if desired. Note that the scope used here is the one provided by the composition where `rememberReorderableLazyColumnState` is called, for long running work that needs to outlast `rememberReorderableLazyColumnState` being in the composition you should use a scope that fits the lifecycle needed.
*/
@Deprecated(
message = "Use rememberReorderableLazyListState instead",
replaceWith = ReplaceWith(
"rememberReorderableLazyListState(lazyListState, scrollThresholdPadding, scrollThreshold, scroller, onMove)",
"sh.calvin.reorderable.rememberReorderableLazyListState",
),
)
@Composable
fun rememberReorderableLazyColumnState(
lazyListState: LazyListState,
scrollThresholdPadding: PaddingValues = PaddingValues(0.dp),
scrollThreshold: Dp = ReorderableLazyCollectionDefaults.ScrollThreshold,
scroller: Scroller = rememberScroller(
scrollableState = lazyListState,
pixelAmountProvider = { lazyListState.layoutInfo.mainAxisViewportSize * ScrollAmountMultiplier },
),
onMove: suspend CoroutineScope.(from: LazyListItemInfo, to: LazyListItemInfo) -> Unit,
) = rememberReorderableLazyListState(
lazyListState,
scrollThresholdPadding,
scrollThreshold,
scroller,
onMove,
)
/**
* Creates a [ReorderableLazyListState] that is remembered across compositions.
*
* Changes to [lazyListState], [scrollThresholdPadding], [scrollThreshold], and [scroller] will result in [ReorderableLazyListState] being updated.
*
* @param lazyListState The return value of [rememberLazyListState](androidx.compose.foundation.lazy.LazyListStateKt.rememberLazyListState)
* @param scrollThresholdPadding The padding that will be added to the left and right of the list to determine the scrollThreshold. Useful for when the grid is displayed under the navigation bar or notification bar.
* @param scrollThreshold The distance in dp from the left or right of the list that will trigger scrolling
* @param scroller The [Scroller] that will be used to scroll the list. Use [rememberScroller](sh.calvin.reorderable.ScrollerKt.rememberScroller) to create a [Scroller].
* @param onMove The function that is called when an item is moved. Make sure this function returns only after the items are moved. This suspend function is invoked with the `rememberReorderableLazyRowState` scope, allowing for async processing, if desired. Note that the scope used here is the one provided by the composition where `rememberReorderableLazyRowState` is called, for long running work that needs to outlast `rememberReorderableLazyRowState` being in the composition you should use a scope that fits the lifecycle needed.
*/
@Deprecated(
message = "Use rememberReorderableLazyListState instead",
replaceWith = ReplaceWith(
"rememberReorderableLazyListState(lazyListState, scrollThresholdPadding, scrollThreshold, scroller, onMove)",
"sh.calvin.reorderable.rememberReorderableLazyListState",
),
)
@Composable
fun rememberReorderableLazyRowState(
lazyListState: LazyListState,
scrollThresholdPadding: PaddingValues = PaddingValues(0.dp),
scrollThreshold: Dp = ReorderableLazyCollectionDefaults.ScrollThreshold,
scroller: Scroller = rememberScroller(
scrollableState = lazyListState,
pixelAmountProvider = { lazyListState.layoutInfo.mainAxisViewportSize * ScrollAmountMultiplier },
),
onMove: suspend CoroutineScope.(from: LazyListItemInfo, to: LazyListItemInfo) -> Unit,
) = rememberReorderableLazyListState(
lazyListState,
scrollThresholdPadding,
scrollThreshold,
scroller,
onMove,
)
/**
* Creates a [ReorderableLazyListState] that is remembered across compositions.
*
* Changes to [lazyListState], [scrollThresholdPadding], [scrollThreshold], and [scroller] will result in [ReorderableLazyListState] being updated.
*
* @param lazyListState The return value of [rememberLazyListState](androidx.compose.foundation.lazy.LazyListStateKt.rememberLazyListState)
* @param scrollThresholdPadding The padding that will be added to the top and bottom, or start and end of the list to determine the scrollThreshold. Useful for when the grid is displayed under the navigation bar or notification bar.
* @param scrollThreshold The distance in dp from the top and bottom, or start and end of the list that will trigger scrolling
* @param scroller The [Scroller] that will be used to scroll the list. Use [rememberScroller](sh.calvin.reorderable.ScrollerKt.rememberScroller) to create a [Scroller].
* @param onMove The function that is called when an item is moved. Make sure this function returns only after the items are moved. This suspend function is invoked with the `rememberReorderableLazyListState` scope, allowing for async processing, if desired. Note that the scope used here is the one provided by the composition where `rememberReorderableLazyListState` is called, for long running work that needs to outlast `rememberReorderableLazyListState` being in the composition you should use a scope that fits the lifecycle needed.
*/
@Composable
fun rememberReorderableLazyListState(
lazyListState: LazyListState,
scrollThresholdPadding: PaddingValues = PaddingValues(0.dp),
scrollThreshold: Dp = ReorderableLazyCollectionDefaults.ScrollThreshold,
scroller: Scroller = rememberScroller(
scrollableState = lazyListState,
pixelAmountProvider = { lazyListState.layoutInfo.mainAxisViewportSize * ScrollAmountMultiplier },
),
onMove: suspend CoroutineScope.(from: LazyListItemInfo, to: LazyListItemInfo) -> Unit,
): ReorderableLazyListState {
val density = LocalDensity.current
val scrollThresholdPx = with(density) { scrollThreshold.toPx() }
val scope = rememberCoroutineScope()
val onMoveState = rememberUpdatedState(onMove)
val layoutDirection = LocalLayoutDirection.current
val absoluteScrollThresholdPadding = AbsolutePixelPadding(
start = with(density) {
scrollThresholdPadding.calculateStartPadding(layoutDirection).toPx()
},
end = with(density) {
scrollThresholdPadding.calculateEndPadding(layoutDirection).toPx()
},
top = with(density) { scrollThresholdPadding.calculateTopPadding().toPx() },
bottom = with(density) { scrollThresholdPadding.calculateBottomPadding().toPx() },
)
val orientation by derivedStateOf { lazyListState.layoutInfo.orientation }
val state = remember(
scope,
lazyListState,
scrollThreshold,
scrollThresholdPadding,
scroller,
orientation,
) {
ReorderableLazyListState(
state = lazyListState,
scope = scope,
onMoveState = onMoveState,
scrollThreshold = scrollThresholdPx,
scrollThresholdPadding = absoluteScrollThresholdPadding,
scroller = scroller,
layoutDirection = layoutDirection,
shouldItemMove = when (orientation) {
Orientation.Vertical -> { draggingItem, item ->
item.center.y in draggingItem.top..<draggingItem.bottom
}
Orientation.Horizontal -> { draggingItem, item ->
item.center.x in draggingItem.left..<draggingItem.right
}
},
)
}
return state
}
private val LazyListLayoutInfo.mainAxisViewportSize: Int
get() = when (orientation) {
Orientation.Vertical -> viewportSize.height
Orientation.Horizontal -> viewportSize.width
}
private fun LazyListItemInfo.toLazyCollectionItemInfo(orientation: Orientation) =
object : LazyCollectionItemInfo<LazyListItemInfo> {
override val index: Int
get() = this@toLazyCollectionItemInfo.index
override val key: Any
get() = this@toLazyCollectionItemInfo.key
override val offset: IntOffset
get() = IntOffset.fromAxis(orientation, this@toLazyCollectionItemInfo.offset)
override val size: IntSize
get() = IntSize.fromAxis(orientation, this@toLazyCollectionItemInfo.size)
override val data: LazyListItemInfo
get() = this@toLazyCollectionItemInfo
}
private fun LazyListLayoutInfo.toLazyCollectionLayoutInfo() =
object : LazyCollectionLayoutInfo<LazyListItemInfo> {
override val visibleItemsInfo: List<LazyCollectionItemInfo<LazyListItemInfo>>
get() = this@toLazyCollectionLayoutInfo.visibleItemsInfo.map {
it.toLazyCollectionItemInfo(orientation)
}
override val viewportSize: IntSize
get() = this@toLazyCollectionLayoutInfo.viewportSize
override val orientation: Orientation
get() = this@toLazyCollectionLayoutInfo.orientation
override val reverseLayout: Boolean
get() = this@toLazyCollectionLayoutInfo.reverseLayout
override val beforeContentPadding: Int
get() = this@toLazyCollectionLayoutInfo.beforeContentPadding
}
private fun LazyListState.toLazyCollectionState() =
object : LazyCollectionState<LazyListItemInfo> {
override val firstVisibleItemIndex: Int
get() = this@toLazyCollectionState.firstVisibleItemIndex
override val firstVisibleItemScrollOffset: Int
get() = this@toLazyCollectionState.firstVisibleItemScrollOffset
override val layoutInfo: LazyCollectionLayoutInfo<LazyListItemInfo>
get() = this@toLazyCollectionState.layoutInfo.toLazyCollectionLayoutInfo()
override suspend fun animateScrollBy(value: Float, animationSpec: AnimationSpec<Float>) =
this@toLazyCollectionState.animateScrollBy(value, animationSpec)
override suspend fun scrollToItem(scrollToIndex: Int, firstVisibleItemScrollOffset: Int) =
this@toLazyCollectionState.scrollToItem(scrollToIndex, firstVisibleItemScrollOffset)
}
class ReorderableLazyListState internal constructor(
state: LazyListState,
scope: CoroutineScope,
onMoveState: State<suspend CoroutineScope.(from: LazyListItemInfo, to: LazyListItemInfo) -> Unit>,
/**
* The threshold in pixels for scrolling the list when dragging an item.
* If the dragged item is within this threshold of the top or bottom of the list, the list will scroll.
* Must be greater than 0.
*/
scrollThreshold: Float,
scrollThresholdPadding: AbsolutePixelPadding,
scroller: Scroller,
layoutDirection: LayoutDirection,
shouldItemMove: (draggingItem: Rect, item: Rect) -> Boolean,
) : ReorderableLazyCollectionState<LazyListItemInfo>(
state.toLazyCollectionState(),
scope,
onMoveState,
scrollThreshold,
scrollThresholdPadding,
scroller,
layoutDirection,
shouldItemMove = shouldItemMove,
)
/**
* A composable that allows an item in LazyColumn or LazyRow to be reordered by dragging.
*
* @param state The return value of [rememberReorderableLazyListState]
* @param key The key of the item, must be the same as the key passed to [LazyListScope.item](androidx.compose.foundation.lazy.item), [LazyListScope.items](androidx.compose.foundation.lazy.items) or similar functions in [LazyListScope](androidx.compose.foundation.lazy.LazyListScope)
* @param enabled Whether or this item is reorderable. If true, the item will not move for other items but may still be draggable. To make an item not draggable, set `enable = false` in [Modifier.draggable] or [Modifier.longPressDraggable] instead.
*/
@ExperimentalFoundationApi
@Composable
fun LazyItemScope.ReorderableItem(
state: ReorderableLazyListState,
key: Any,
modifier: Modifier = Modifier,
enabled: Boolean = true,
content: @Composable ReorderableCollectionItemScope.(isDragging: Boolean) -> Unit,
) {
val orientation = state.orientation
val dragging by state.isItemDragging(key)
val offsetModifier = if (dragging) {
Modifier
.zIndex(1f)
.then(when (orientation) {
Orientation.Vertical -> Modifier.graphicsLayer {
translationY = state.draggingItemOffset.y
}
Orientation.Horizontal -> Modifier.graphicsLayer {
translationX = state.draggingItemOffset.x
}
})
} else if (key == state.previousDraggingItemKey) {
Modifier
.zIndex(1f)
.then(when (orientation) {
Orientation.Vertical -> Modifier.graphicsLayer {
translationY = state.previousDraggingItemOffset.value.y
}
Orientation.Horizontal -> Modifier.graphicsLayer {
translationX = state.previousDraggingItemOffset.value.x
}
})
} else {
Modifier.animateItemPlacement()
}
ReorderableCollectionItem(
state = state,
key = key,
modifier = modifier.then(offsetModifier),
enabled = enabled,
dragging = dragging,
content = content,
)
}