Skip to content

Commit

Permalink
fix: implemented new swieable snackbar to avoid crashes when closing …
Browse files Browse the repository at this point in the history
…screen
  • Loading branch information
Garzas committed Oct 2, 2023
1 parent 7884f76 commit 1214357
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 116 deletions.
2 changes: 1 addition & 1 deletion app/src/main/kotlin/com/wire/android/WireApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class WireApplication : Application(), Configuration.Provider {
.detectDiskReads()
.detectDiskWrites()
.penaltyLog()
.penaltyDeath()
// .penaltyDeath()
.build()
)
StrictMode.setVmPolicy(
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/com/wire/android/ui/WireActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import com.wire.android.ui.destinations.WelcomeScreenDestination
import com.wire.android.ui.home.E2EIRequiredDialog
import com.wire.android.ui.home.E2EISnoozeDialog
import com.wire.android.ui.home.sync.FeatureFlagNotificationViewModel
import com.wire.android.ui.snackbar.LocalSnackbarHostState
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import com.wire.android.ui.theme.WireTheme
import com.wire.android.util.CurrentScreenManager
import com.wire.android.util.LocalSyncStateObserver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
*/
package com.wire.android.ui.common.scaffold

import SwipeableSnackbar
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.material3.FabPosition
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.wire.android.ui.common.snackbar.SwipeDismissSnackbarHost
import com.wire.android.ui.snackbar.LocalSnackbarHostState
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState

/**
* A custom scaffold that automatically applies system UI insets and IME (Input Method Editor)
Expand Down Expand Up @@ -68,12 +69,21 @@ fun WireScaffold(
topBar = topBar,
bottomBar = bottomBar,
snackbarHost = {
SwipeDismissSnackbarHost(hostState = LocalSnackbarHostState.current)
SnackbarHost(
hostState = LocalSnackbarHostState.current,
snackbar = { data ->
SwipeableSnackbar(
hostState = LocalSnackbarHostState.current,
data = data,
onDismiss = { data.dismiss() }
)
}
)
},
floatingActionButton,
floatingActionButtonPosition,
containerColor,
contentColor,
floatingActionButton = floatingActionButton,
floatingActionButtonPosition = floatingActionButtonPosition,
containerColor = containerColor,
contentColor = contentColor,
contentWindowInsets = WindowInsets(0, 0, 0, 0),
content = content
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.ui.snackbar
package com.wire.android.ui.common.snackbar

import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.staticCompositionLocalOf
Expand Down

This file was deleted.

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 }

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.ui.snackbar
package com.wire.android.ui.common.snackbar

import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import com.wire.android.ui.common.dialogs.UnblockUserDialogContent
import com.wire.android.ui.common.dialogs.UnblockUserDialogState
import com.wire.android.ui.common.dimensions
import com.wire.android.ui.common.visbility.rememberVisibilityState
import com.wire.android.ui.snackbar.LocalSnackbarHostState
import com.wire.android.ui.snackbar.collectAndShowSnackbar
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import com.wire.android.ui.common.snackbar.collectAndShowSnackbar
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.user.ConnectionState
import com.wire.kalium.logic.data.user.UserId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import com.wire.android.ui.common.bottomsheet.WireModalSheetState
import com.wire.android.ui.common.bottomsheet.rememberWireModalSheetState
import com.wire.android.ui.common.topappbar.search.SearchBarState
import com.wire.android.ui.common.topappbar.search.rememberSearchbarState
import com.wire.android.ui.snackbar.LocalSnackbarHostState
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.wire.android.ui.home.conversations

import SwipeableSnackbar
import android.net.Uri
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -30,6 +31,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarResult
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand Down Expand Up @@ -73,7 +75,7 @@ import com.wire.android.ui.common.dialogs.calling.ConfirmStartCallDialog
import com.wire.android.ui.common.dialogs.calling.JoinAnywayDialog
import com.wire.android.ui.common.dialogs.calling.OngoingActiveCallDialog
import com.wire.android.ui.common.error.CoreFailureErrorDialog
import com.wire.android.ui.common.snackbar.SwipeDismissSnackbarHost
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import com.wire.android.ui.destinations.GroupConversationDetailsScreenDestination
import com.wire.android.ui.destinations.InitiatingCallScreenDestination
import com.wire.android.ui.destinations.MediaGalleryScreenDestination
Expand Down Expand Up @@ -104,7 +106,6 @@ import com.wire.android.ui.home.messagecomposer.MessageComposer
import com.wire.android.ui.home.messagecomposer.state.MessageBundle
import com.wire.android.ui.home.messagecomposer.state.MessageComposerStateHolder
import com.wire.android.ui.home.messagecomposer.state.rememberMessageComposerStateHolder
import com.wire.android.ui.snackbar.LocalSnackbarHostState
import com.wire.android.util.extension.openAppInfoScreen
import com.wire.android.util.normalizeLink
import com.wire.android.util.ui.UIText
Expand Down Expand Up @@ -570,11 +571,15 @@ private fun ConversationScreen(
}
},
snackbarHost = {
SwipeDismissSnackbarHost(
SnackbarHost(
hostState = snackbarHostState,
modifier = Modifier
.fillMaxWidth()
.imePadding()
snackbar = { data ->
SwipeableSnackbar(
hostState = snackbarHostState,
data = data,
onDismiss = { data.dismiss() }
)
}
)
},
content = { internalPadding ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.wire.android.R
import com.wire.android.ui.common.bottomsheet.WireModalSheetState
import com.wire.android.ui.common.bottomsheet.rememberWireModalSheetState
import com.wire.android.ui.home.conversations.model.UIMessage
import com.wire.android.ui.snackbar.LocalSnackbarHostState
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import com.wire.android.ui.home.conversations.details.participants.GroupConversa
import com.wire.android.ui.home.conversations.details.participants.model.UIParticipant
import com.wire.android.ui.home.conversationslist.model.DialogState
import com.wire.android.ui.home.conversationslist.model.GroupDialogState
import com.wire.android.ui.snackbar.LocalSnackbarHostState
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import com.wire.android.ui.theme.WireTheme
import com.wire.android.ui.theme.wireDimensions
import com.wire.android.util.ui.UIText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import com.wire.android.ui.common.topappbar.WireCenterAlignedTopAppBar
import com.wire.android.ui.destinations.CreatePasswordProtectedGuestLinkScreenDestination
import com.wire.android.ui.home.conversations.details.editguestaccess.createPasswordProtectedGuestLink.CreatePasswordGuestLinkNavArgs
import com.wire.android.ui.home.conversationslist.common.FolderHeader
import com.wire.android.ui.snackbar.LocalSnackbarHostState
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import com.wire.android.ui.theme.wireColorScheme
import com.wire.android.ui.theme.wireDimensions
import com.wire.android.ui.theme.wireTypography
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import com.wire.android.ui.common.bottomsheet.WireModalSheetState
import com.wire.android.ui.common.bottomsheet.rememberWireModalSheetState
import com.wire.android.ui.snackbar.LocalSnackbarHostState
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

Expand Down
Loading

0 comments on commit 1214357

Please sign in to comment.