-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: toast crash [WPB-4937] #2293
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1214357
fix: implemented new swieable snackbar to avoid crashes when closing …
Garzas bb054e9
detekt fix
Garzas 1959c13
Merge branch 'develop' into fix/snackbar-crash
yamilmedina db9a8be
Merge branch 'develop' into fix/snackbar-crash
Garzas 15be997
typo fix
Garzas e3ad8b3
Merge branch 'fix/snackbar-crash' of github.com:wireapp/wire-android-…
Garzas 3371876
Merge branch 'develop' into fix/snackbar-crash
Garzas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
113 changes: 113 additions & 0 deletions
113
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,113 @@ | ||
/* | ||
* 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it here?