summaryrefslogtreecommitdiffstats
path: root/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/compose/SwipeToDismissBox.kt
blob: 8c5159baec39f07cda7b7b39ccaef005c7da2f5a (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.compose

import androidx.compose.animation.core.tween
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.AnchoredDraggableState
import androidx.compose.foundation.gestures.DraggableAnchors
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.anchoredDraggable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.material.Snackbar
import androidx.compose.material.SnackbarHost
import androidx.compose.material.SnackbarHostState
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import org.mozilla.fenix.theme.FirefoxTheme
import kotlin.math.roundToInt

/**
 * The anchors that establish the swipe gesture directions.
 */
enum class SwipeToDismissAnchor {
    Start,
    Default,
    End,
    ;

    companion object {

        /**
         * The anchors that establish the swipe gesture for both directions.
         */
        fun swipeBothDirectionsAnchors() = entries

        /**
         * The anchors that establish the swipe gesture for the start direction.
         */
        fun swipeToStartAnchors() = listOf(Start, Default)

        /**
         * The anchors that establish the swipe gesture for the end direction.
         */
        fun swipeToEndAnchors() = listOf(Default, End)
    }
}

/**
 * The percent of the screen an item has to be swiped before it is considered dismissed.
 */
private const val DISMISS_THRESHOLD = 0.5f

/**
 * The velocity (in DP per second) the item has to exceed in order to animate to the next state,
 * even if the [AnchoredDraggableState.positionalThreshold] has not been reached.
 */
private val VELOCITY_THRESHOLD_DP = 150.dp

/**
 * The UI state for [SwipeToDismissBox].
 *
 * @param density [Density] used to derive the underlying [AnchoredDraggableState.velocityThreshold].
 * @property anchoredDraggableState [AnchoredDraggableState] for the underlying [Modifier.anchoredDraggable].
 * @property anchors A list of [SwipeToDismissAnchor] which establish the swipe directions of [SwipeToDismissBox].
 * @property enabled Whether the swipe gesture is active.
 */
@OptIn(ExperimentalFoundationApi::class)
class SwipeToDismissState(
    density: Density,
    val anchoredDraggableState: AnchoredDraggableState<SwipeToDismissAnchor> = AnchoredDraggableState(
        initialValue = SwipeToDismissAnchor.Default,
        positionalThreshold = { distance: Float -> distance * DISMISS_THRESHOLD },
        velocityThreshold = { with(density) { VELOCITY_THRESHOLD_DP.toPx() } },
        animationSpec = tween(),
    ),
    val anchors: List<SwipeToDismissAnchor> = SwipeToDismissAnchor.swipeBothDirectionsAnchors(),
    val enabled: Boolean = true,
) {

    /**
     * Whether there is a swipe gesture in-progress.
     */
    val swipingActive: Boolean
        get() = !anchoredDraggableState.offset.isNaN() && anchoredDraggableState.offset != 0f

    /**
     * The [SwipeToDismissAnchor] the swipe gesture is targeting.
     */
    private val swipeDestination: SwipeToDismissAnchor
        get() = anchoredDraggableState.anchors.closestAnchor(
            position = anchoredDraggableState.offset,
            searchUpwards = anchoredDraggableState.offset > 0,
        ) ?: SwipeToDismissAnchor.Default

    /**
     * Whether the swipe gesture is in the start direction.
     */
    val isSwipingToStart: Boolean
        get() = swipeDestination == SwipeToDismissAnchor.Start

    /**
     * The current [IntOffset] of the swipe. If the X-offset is currently [Float.NaN], it will return 0.
     */
    val safeSwipeOffset: IntOffset
        get() {
            val xOffset = if (anchoredDraggableState.offset.isNaN()) {
                0
            } else {
                anchoredDraggableState.offset.roundToInt()
            }

            return IntOffset(x = xOffset, y = 0)
        }
}

/**
 * A container that can be dismissed by swiping left or right.
 *
 * @param modifier Optional [Modifier] for this component.
 * @param state [SwipeToDismissState] containing the UI state of [SwipeToDismissBox].
 * @param onItemDismiss Invoked when the item is dismissed.
 * @param backgroundContent A composable that is stacked behind the primary content and is exposed
 * when the content is swiped. You can/should use the [state] to have different backgrounds on each side.
 * @param dismissContent The content that can be dismissed.
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SwipeToDismissBox(
    modifier: Modifier = Modifier,
    state: SwipeToDismissState = SwipeToDismissState(density = LocalDensity.current),
    onItemDismiss: () -> Unit,
    backgroundContent: @Composable BoxScope.() -> Unit,
    dismissContent: @Composable BoxScope.() -> Unit,
) {
    val swipeWidth = with(LocalDensity.current) {
        LocalConfiguration.current.screenWidthDp.dp.toPx()
    }
    val draggableAnchors = DraggableAnchors {
        state.anchors.forEach { anchor ->
            val anchorPosition = when (anchor) {
                SwipeToDismissAnchor.Start -> -swipeWidth
                SwipeToDismissAnchor.Default -> 0f
                SwipeToDismissAnchor.End -> swipeWidth
            }
            anchor at anchorPosition
        }
    }

    SideEffect {
        state.anchoredDraggableState.updateAnchors(draggableAnchors)
    }

    LaunchedEffect(state.anchoredDraggableState.currentValue) {
        when (state.anchoredDraggableState.currentValue) {
            SwipeToDismissAnchor.Start, SwipeToDismissAnchor.End -> onItemDismiss()
            SwipeToDismissAnchor.Default -> {} // no-op
        }
    }

    Box(
        modifier = Modifier
            .anchoredDraggable(
                state = state.anchoredDraggableState,
                orientation = Orientation.Horizontal,
                enabled = state.enabled,
                reverseDirection = LocalLayoutDirection.current == LayoutDirection.Rtl,
            )
            .then(modifier),
    ) {
        Box(
            modifier = Modifier.matchParentSize(),
            content = backgroundContent,
        )

        Box(
            modifier = Modifier.offset { state.safeSwipeOffset },
            content = dismissContent,
        )
    }
}

@Composable
@Preview
@Preview(locale = "ar", name = "RTL")
private fun SwipeToDismissBoxPreview() {
    val snackbarState = remember { SnackbarHostState() }
    val coroutineScope = rememberCoroutineScope()

    FirefoxTheme {
        Box(
            modifier = Modifier.fillMaxSize(),
        ) {
            Column {
                SwipeableItem(
                    anchors = SwipeToDismissAnchor.swipeToEndAnchors(),
                    text = "Swipe to right ->",
                    onSwipeToEnd = {
                        coroutineScope.launch {
                            snackbarState.showSnackbar("Dismiss")
                        }
                    },
                )

                Spacer(Modifier.height(30.dp))

                SwipeableItem(
                    anchors = SwipeToDismissAnchor.swipeToStartAnchors(),
                    text = "<- Swipe to left",
                    onSwipeToStart = {
                        coroutineScope.launch {
                            snackbarState.showSnackbar("Dismiss")
                        }
                    },
                )

                Spacer(Modifier.height(30.dp))

                SwipeableItem(
                    anchors = SwipeToDismissAnchor.swipeBothDirectionsAnchors(),
                    text = "<- Swipe both ways ->",
                    onSwipeToStart = {
                        coroutineScope.launch {
                            snackbarState.showSnackbar("Dismiss start")
                        }
                    },
                    onSwipeToEnd = {
                        coroutineScope.launch {
                            snackbarState.showSnackbar("Dismiss end")
                        }
                    },
                )
            }

            SnackbarHost(
                hostState = snackbarState,
                modifier = Modifier.align(Alignment.BottomCenter),
            ) { snackbarData ->
                Snackbar(
                    snackbarData = snackbarData,
                )
            }
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun SwipeableItem(
    text: String,
    anchors: List<SwipeToDismissAnchor>,
    onSwipeToStart: () -> Unit = {},
    onSwipeToEnd: () -> Unit = {},
) {
    val density = LocalDensity.current
    val swipeState = remember {
        SwipeToDismissState(
            density = density,
            anchors = anchors,
        )
    }

    Box(
        modifier = Modifier
            .height(30.dp)
            .fillMaxWidth(),
    ) {
        SwipeToDismissBox(
            state = swipeState,
            onItemDismiss = {
                if (swipeState.anchoredDraggableState.currentValue == SwipeToDismissAnchor.Start) {
                    onSwipeToStart()
                } else {
                    onSwipeToEnd()
                }
            },
            backgroundContent = {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(FirefoxTheme.colors.layerAccent),
                )
            },
        ) {
            Row(
                modifier = Modifier
                    .fillMaxSize()
                    .background(FirefoxTheme.colors.layer1),
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Text(text)
            }
        }
    }
}