-
Notifications
You must be signed in to change notification settings - Fork 7
/
ReorderableLazyGrid.kt
225 lines (209 loc) · 10.2 KB
/
ReorderableLazyGrid.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
/*
* 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.grid.LazyGridItemInfo
import androidx.compose.foundation.lazy.grid.LazyGridItemScope
import androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
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.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 [ReorderableLazyGridState] that is remembered across compositions.
*
* Changes to [lazyGridState], [scrollThresholdPadding], [scrollThreshold], and [scroller] will result in [ReorderableLazyGridState] being updated.
*
* @param lazyGridState The return value of [rememberLazyGridState](androidx.compose.foundation.lazy.LazyGridStateKt.rememberLazyGridState)
* @param scrollThresholdPadding The padding that will be added to the top and bottom, or start and end of the grid 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 grid that will trigger scrolling
* @param scroller The [Scroller] that will be used to scroll the grid. 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 `rememberReorderableLazyGridState` scope, allowing for async processing, if desired. Note that the scope used here is the one provided by the composition where `rememberReorderableLazyGridState` is called, for long running work that needs to outlast `rememberReorderableLazyGridState` being in the composition you should use a scope that fits the lifecycle needed.
*/
@Composable
fun rememberReorderableLazyGridState(
lazyGridState: LazyGridState,
scrollThresholdPadding: PaddingValues = PaddingValues(0.dp),
scrollThreshold: Dp = ReorderableLazyCollectionDefaults.ScrollThreshold,
scroller: Scroller = rememberScroller(
scrollableState = lazyGridState,
pixelAmountProvider = { lazyGridState.layoutInfo.mainAxisViewportSize * ScrollAmountMultiplier },
),
onMove: suspend CoroutineScope.(from: LazyGridItemInfo, to: LazyGridItemInfo) -> Unit,
): ReorderableLazyGridState {
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 state = remember(
scope, lazyGridState, scrollThreshold, scrollThresholdPadding, scroller,
) {
ReorderableLazyGridState(
state = lazyGridState,
scope = scope,
onMoveState = onMoveState,
scrollThreshold = scrollThresholdPx,
scrollThresholdPadding = absoluteScrollThresholdPadding,
scroller = scroller,
layoutDirection = layoutDirection,
)
}
return state
}
private val LazyGridLayoutInfo.mainAxisViewportSize: Int
get() = when (orientation) {
Orientation.Vertical -> viewportSize.height
Orientation.Horizontal -> viewportSize.width
}
private fun LazyGridItemInfo.toLazyCollectionItemInfo() =
object : LazyCollectionItemInfo<LazyGridItemInfo> {
override val index: Int
get() = this@toLazyCollectionItemInfo.index
override val key: Any
get() = this@toLazyCollectionItemInfo.key
override val offset: IntOffset
get() = this@toLazyCollectionItemInfo.offset
override val size: IntSize
get() = this@toLazyCollectionItemInfo.size
override val data: LazyGridItemInfo
get() = this@toLazyCollectionItemInfo
}
private fun LazyGridLayoutInfo.toLazyCollectionLayoutInfo() =
object : LazyCollectionLayoutInfo<LazyGridItemInfo> {
override val visibleItemsInfo: List<LazyCollectionItemInfo<LazyGridItemInfo>>
get() = this@toLazyCollectionLayoutInfo.visibleItemsInfo.map {
it.toLazyCollectionItemInfo()
}
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 LazyGridState.toLazyCollectionState() =
object : LazyCollectionState<LazyGridItemInfo> {
override val firstVisibleItemIndex: Int
get() = this@toLazyCollectionState.firstVisibleItemIndex
override val firstVisibleItemScrollOffset: Int
get() = this@toLazyCollectionState.firstVisibleItemScrollOffset
override val layoutInfo: LazyCollectionLayoutInfo<LazyGridItemInfo>
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 ReorderableLazyGridState internal constructor(
state: LazyGridState,
scope: CoroutineScope,
onMoveState: State<suspend CoroutineScope.(from: LazyGridItemInfo, to: LazyGridItemInfo) -> Unit>,
/**
* The threshold in pixels for scrolling the grid when dragging an item.
* If the dragged item is within this threshold of the top or bottom of the grid, the grid will scroll.
* Must be greater than 0.
*/
scrollThreshold: Float,
scrollThresholdPadding: AbsolutePixelPadding,
scroller: Scroller,
layoutDirection: LayoutDirection,
) : ReorderableLazyCollectionState<LazyGridItemInfo>(
state.toLazyCollectionState(),
scope,
onMoveState,
scrollThreshold,
scrollThresholdPadding,
scroller,
layoutDirection,
)
/**
* A composable that allows an item in LazyVerticalGrid or LazyHorizontalGrid to be reordered by dragging.
*
* @param state The return value of [rememberReorderableLazyGridState]
* @param key The key of the item, must be the same as the key passed to [LazyGridScope.item](androidx.compose.foundation.lazy.grid.item), [LazyGridScope.items](androidx.compose.foundation.lazy.grid.items) or similar functions in [LazyGridScope](androidx.compose.foundation.lazy.grid.LazyGridScope)
* @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 LazyGridItemScope.ReorderableItem(
state: ReorderableLazyGridState,
key: Any,
modifier: Modifier = Modifier,
enabled: Boolean = true,
content: @Composable ReorderableCollectionItemScope.(isDragging: Boolean) -> Unit,
) {
val dragging by state.isItemDragging(key)
val offsetModifier = if (dragging) {
Modifier
.zIndex(1f)
.graphicsLayer {
translationY = state.draggingItemOffset.y
translationX = state.draggingItemOffset.x
}
} else if (key == state.previousDraggingItemKey) {
Modifier
.zIndex(1f)
.graphicsLayer {
translationY =
state.previousDraggingItemOffset.value.y
translationX =
state.previousDraggingItemOffset.value.x
}
} else {
Modifier.animateItemPlacement()
}
ReorderableCollectionItem(
state = state,
key = key,
modifier = modifier.then(offsetModifier),
enabled = enabled,
dragging = dragging,
content = content,
)
}