-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implemented new swieable snackbar to avoid crashes when closing …
…screen
- Loading branch information
Showing
24 changed files
with
163 additions
and
116 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
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
82 changes: 0 additions & 82 deletions
82
app/src/main/kotlin/com/wire/android/ui/common/snackbar/SwipeDismissSnackbar.kt
This file was deleted.
Oops, something went wrong.
114 changes: 114 additions & 0 deletions
114
app/src/main/kotlin/com/wire/android/ui/common/snackbar/SwipeableSnackbar.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,114 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
|
||
import androidx.compose.animation.core.SpringSpec | ||
import androidx.compose.foundation.ExperimentalFoundationApi | ||
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.offset | ||
import androidx.compose.material3.Snackbar | ||
import androidx.compose.material3.SnackbarData | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalConfiguration | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.unit.IntOffset | ||
import androidx.compose.ui.unit.dp | ||
import kotlin.math.roundToInt | ||
|
||
/** | ||
* A swipeable [Snackbar] that allows users to manually dismiss it by dragging. | ||
* | ||
* This composable function extends the default Snackbar behavior by adding a draggable gesture. | ||
* The Snackbar can be swiped horizontally to dismiss it, based on predefined positional and velocity thresholds. | ||
* | ||
* @param hostState The state of the [SnackbarHostState] this Snackbar is associated with. This allows | ||
* the Snackbar to notify its host when it's dismissed. | ||
* @param data The [SnackbarData] containing the message and optional action to display on the Snackbar. | ||
* @param onDismiss An optional callback function to be executed when the Snackbar is swiped away. | ||
* The default behavior will dismiss the current Snackbar from the [hostState]. | ||
* @see Snackbar | ||
* @see SnackbarData | ||
* @see SnackbarHostState | ||
*/ | ||
@OptIn(ExperimentalFoundationApi::class) | ||
@Composable | ||
fun SwipeableSnackbar( | ||
hostState: SnackbarHostState, | ||
data: SnackbarData, | ||
onDismiss: () -> Unit = { hostState.currentSnackbarData?.dismiss() }, | ||
) { | ||
val density = LocalDensity.current | ||
val configuration = LocalConfiguration.current | ||
|
||
val currentScreenWidth = with(density) { configuration.screenWidthDp.dp.toPx() } | ||
|
||
val anchors = DraggableAnchors { | ||
SnackBarState.Visible at 0f | ||
SnackBarState.Dismissed at currentScreenWidth | ||
} | ||
|
||
// Determines how far the user needs to drag (as a fraction of total distance) for an action to be triggered. | ||
// In this example, the Snackbar will trigger an action if dragged to half (0.5) of its width. | ||
val positionalThreshold: (Float) -> Float = { distance -> distance * 0.5f } | ||
|
||
// Determines the minimum velocity (in pixels per second) with which the user needs to drag for an action to be triggered, | ||
// even if the positional threshold hasn't been reached. | ||
// Here, it's set to 125 device-independent pixels per second. | ||
val velocityThreshold: () -> Float = with(density) { { 125.dp.toPx() } } | ||
|
||
val state = remember { | ||
AnchoredDraggableState( | ||
initialValue = SnackBarState.Visible, | ||
anchors = anchors, | ||
positionalThreshold = positionalThreshold, | ||
velocityThreshold = velocityThreshold, | ||
animationSpec = SpringSpec(), | ||
confirmValueChange = { true } | ||
) | ||
} | ||
|
||
LaunchedEffect(state.currentValue) { | ||
if (state.currentValue == SnackBarState.Dismissed) { | ||
onDismiss() | ||
} | ||
} | ||
|
||
Snackbar( | ||
snackbarData = data, | ||
modifier = Modifier | ||
.anchoredDraggable( | ||
state = state, | ||
orientation = Orientation.Horizontal | ||
) | ||
.offset { | ||
IntOffset( | ||
state | ||
.requireOffset() | ||
.roundToInt(), 0 | ||
) | ||
}) | ||
} | ||
|
||
private enum class SnackBarState { Visible, Dismissed } | ||
|
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
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
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
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
Oops, something went wrong.