Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5a834336 authored by omarmt's avatar omarmt
Browse files

PriorityNestedScrollConnection: make onStop suspendable and add onCancel

The onStop lambda is now a suspend function. This allows for performing
long-running operations, such as animations, within the onStop callback.

The onCancel lambda has been added, this lambda will be invoked when the
nested scrolling operation is canceled, for example, if the user cannot
scroll anymore.

Test: atest PriorityNestedScrollConnectionTest
Bug: 370949877
Flag: com.android.systemui.scene_container
Change-Id: I4cc652fe4f6baac800e1484e7bddfbf5510de2b1
parent 40c5c231
Loading
Loading
Loading
Loading
+8 −2
Original line number Original line Diff line number Diff line
@@ -73,13 +73,19 @@ fun NotificationScrimNestedScrollConnection(
            snapScrimOffset(currentHeight + amountConsumed)
            snapScrimOffset(currentHeight + amountConsumed)
            amountConsumed
            amountConsumed
        },
        },
        // Don't consume the velocity on pre/post fling
        onStop = { velocityAvailable ->
        onStop = { velocityAvailable ->
            onStop(velocityAvailable)
            onStop(velocityAvailable)
            if (scrimOffset() < minScrimOffset()) {
            if (scrimOffset() < minScrimOffset()) {
                animateScrimOffset(minScrimOffset())
                animateScrimOffset(minScrimOffset())
            }
            }
            { 0f }
            // Don't consume the velocity on pre/post fling
            0f
        },
        onCancel = {
            onStop(0f)
            if (scrimOffset() < minScrimOffset()) {
                animateScrimOffset(minScrimOffset())
            }
        },
        },
    )
    )
}
}
+2 −1
Original line number Original line Diff line number Diff line
@@ -104,7 +104,8 @@ fun NotificationStackNestedScrollConnection(
        },
        },
        onStop = { velocityAvailable ->
        onStop = { velocityAvailable ->
            onStop(velocityAvailable)
            onStop(velocityAvailable)
            suspend { velocityAvailable }
            velocityAvailable
        },
        },
        onCancel = { onStop(0f) },
    )
    )
}
}
+14 −5
Original line number Original line Diff line number Diff line
@@ -27,9 +27,10 @@ import com.android.compose.animation.scene.content.Content
import com.android.compose.animation.scene.content.state.TransitionState
import com.android.compose.animation.scene.content.state.TransitionState
import com.android.compose.animation.scene.content.state.TransitionState.HasOverscrollProperties.Companion.DistanceUnspecified
import com.android.compose.animation.scene.content.state.TransitionState.HasOverscrollProperties.Companion.DistanceUnspecified
import com.android.compose.nestedscroll.PriorityNestedScrollConnection
import com.android.compose.nestedscroll.PriorityNestedScrollConnection
import com.android.compose.nestedscroll.SuspendedValue
import kotlin.math.absoluteValue
import kotlin.math.absoluteValue


internal typealias SuspendedValue<T> = suspend () -> T

internal interface DraggableHandler {
internal interface DraggableHandler {
    /**
    /**
     * Start a drag in the given [startedPosition], with the given [overSlop] and number of
     * Start a drag in the given [startedPosition], with the given [overSlop] and number of
@@ -712,10 +713,18 @@ internal class NestedScrollHandlerImpl(
            },
            },
            onStop = { velocityAvailable ->
            onStop = { velocityAvailable ->
                val controller = dragController ?: error("Should be called after onStart")
                val controller = dragController ?: error("Should be called after onStart")

                try {
                    controller
                    controller
                        .onStop(velocity = velocityAvailable, canChangeContent = canChangeScene)
                        .onStop(velocity = velocityAvailable, canChangeContent = canChangeScene)
                    .also { dragController = null }
                        .invoke()
                } finally {
                    dragController = null
                }
            },
            onCancel = {
                val controller = dragController ?: error("Should be called after onStart")
                controller.onStop(velocity = 0f, canChangeContent = canChangeScene)
                dragController = null
            },
            },
        )
        )
    }
    }
+0 −1
Original line number Original line Diff line number Diff line
@@ -27,7 +27,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.IntSize
import com.android.compose.animation.scene.content.state.TransitionState
import com.android.compose.animation.scene.content.state.TransitionState
import com.android.compose.animation.scene.content.state.TransitionState.HasOverscrollProperties.Companion.DistanceUnspecified
import com.android.compose.animation.scene.content.state.TransitionState.HasOverscrollProperties.Companion.DistanceUnspecified
import com.android.compose.nestedscroll.SuspendedValue
import kotlin.math.absoluteValue
import kotlin.math.absoluteValue
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CompletableDeferred


+2 −1
Original line number Original line Diff line number Diff line
@@ -70,6 +70,7 @@ fun LargeTopAppBarNestedScrollConnection(
            amountConsumed
            amountConsumed
        },
        },
        // Don't consume the velocity on pre/post fling
        // Don't consume the velocity on pre/post fling
        onStop = { { 0f } },
        onStop = { 0f },
        onCancel = { /* do nothing */ },
    )
    )
}
}
Loading