Skip to content

Commit

Permalink
Remove lateinit properties from SwipeableActionsState
Browse files Browse the repository at this point in the history
Should fix #19
  • Loading branch information
saket committed Sep 21, 2023
1 parent ccbead4 commit e9f8a22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.absoluteOffset
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -58,23 +57,19 @@ fun SwipeableActionsBox(
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
val leftActions = if (isRtl) endActions else startActions
val rightActions = if (isRtl) startActions else endActions
val swipeThresholdPx = LocalDensity.current.run { swipeThreshold.toPx() }

state.canSwipeTowardsRight = leftActions.isNotEmpty()
state.canSwipeTowardsLeft = rightActions.isNotEmpty()

val ripple = remember {
SwipeRippleState()
}
val actions = remember(leftActions, rightActions) {
ActionFinder(left = leftActions, right = rightActions)
}
LaunchedEffect(state, actions) {
state.run {
canSwipeTowardsRight = { leftActions.isNotEmpty() }
canSwipeTowardsLeft = { rightActions.isNotEmpty() }
}
}

val offset = state.offset.value
val thresholdCrossed = abs(offset) > swipeThresholdPx
val thresholdCrossed = abs(offset) > LocalDensity.current.run { swipeThreshold.toPx() }

var swipedAction: SwipeActionMeta? by remember {
mutableStateOf(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class SwipeableActionsState internal constructor() {
var isResettingOnRelease: Boolean by mutableStateOf(false)
private set

internal lateinit var canSwipeTowardsRight: () -> Boolean
internal lateinit var canSwipeTowardsLeft: () -> Boolean
internal var canSwipeTowardsRight = false
internal var canSwipeTowardsLeft = false

internal val draggableState = DraggableState { delta ->
val targetOffset = offsetState.value + delta
val isAllowed = isResettingOnRelease
|| targetOffset > 0f && canSwipeTowardsRight()
|| targetOffset < 0f && canSwipeTowardsLeft()
|| targetOffset > 0f && canSwipeTowardsRight
|| targetOffset < 0f && canSwipeTowardsLeft

// Add some resistance if needed.
offsetState.value += if (isAllowed) delta else delta / 10
Expand Down

0 comments on commit e9f8a22

Please sign in to comment.