@@ -23,20 +23,22 @@ import androidx.compose.ui.unit.dp
2323@Stable
2424internal class CollapsibleHeaderState (
2525 private val density : Density ,
26- val stickyHeaderHeightDp : Dp ,
26+ val initialStickyHeaderHeightDp : Dp ,
2727 val expandedHeaderHeightDp : Dp ,
2828) {
29- private val stickyHeaderHeightPx: Float = with (density) { stickyHeaderHeightDp.toPx() }
30-
3129 private val expandedHeaderHeightPx: Float = with (density) { expandedHeaderHeightDp.toPx() }
3230
33- val collapsedContentOffsetDp: Dp = with (density) { stickyHeaderHeightPx.toDp() + 18 .dp }
31+ var stickyHeaderActualBottomPx by mutableFloatStateOf(0f )
32+ internal set
33+
34+ val collapsedContentOffsetDp: Dp
35+ get() = with (density) { stickyHeaderActualBottomPx.toDp() }
3436
3537 var currentHeightPx by mutableFloatStateOf(expandedHeaderHeightPx)
3638 private set
3739
3840 val expansionProgress: Float
39- get() = if (expandedHeaderHeightPx > 0f ) ( currentHeightPx / expandedHeaderHeightPx).coerceIn(0f , 1f ) else 1f
41+ get() = ( currentHeightPx / expandedHeaderHeightPx).coerceIn(0f , 1f )
4042
4143 val nestedScrollConnection = object : NestedScrollConnection {
4244 override fun onPreScroll (available : Offset , source : NestedScrollSource ) =
@@ -46,33 +48,23 @@ internal class CollapsibleHeaderState(
4648 if (available.y > 0 ) consumeDelta(available.y) else Offset .Zero
4749
4850 override suspend fun onPreFling (available : Velocity ): Velocity {
49- if (currentHeightPx <= 0f || currentHeightPx >= expandedHeaderHeightPx) return Velocity .Zero
51+ val isFullyCollapsed = currentHeightPx < 1f
52+ val isFullyExpanded = currentHeightPx > expandedHeaderHeightPx - 1f
5053
51- val collapse = 0f
52- val expand = expandedHeaderHeightPx
54+ if (isFullyCollapsed || isFullyExpanded) return Velocity .Zero
5355
5456 val target = when {
55- available.y < - 50f -> collapse
56- available.y > 50f -> expand
57- else -> if (currentHeightPx - collapse < expand - currentHeightPx) collapse else expand
57+ available.y < - 50f -> 0f
58+ available.y > 50f -> expandedHeaderHeightPx
59+ else -> if (currentHeightPx < expandedHeaderHeightPx / 2 ) 0f
60+ else expandedHeaderHeightPx
5861 }
5962
6063 snapTo(targetHeight = target, velocity = available.y)
61-
62- return Velocity (0f , available.y)
64+ return Velocity .Zero
6365 }
6466
6567 override suspend fun onPostFling (consumed : Velocity , available : Velocity ): Velocity {
66- if (available.y > 0 && currentHeightPx < expandedHeaderHeightPx) {
67- snapTo(targetHeight = expandedHeaderHeightPx, velocity = available.y)
68- return Velocity (0f , available.y)
69- }
70-
71- if (available.y < 0 && currentHeightPx > 0f ) {
72- snapTo(targetHeight = 0f , velocity = available.y)
73- return Velocity (0f , available.y)
74- }
75-
7668 return Velocity .Zero
7769 }
7870 }
@@ -94,27 +86,26 @@ internal class CollapsibleHeaderState(
9486 dampingRatio = Spring .DampingRatioNoBouncy ,
9587 stiffness = Spring .StiffnessMediumLow ,
9688 ),
97- ) { value, _ ->
98- currentHeightPx = value
99- }
89+ ) { value, _ -> currentHeightPx = value.coerceIn(0f , expandedHeaderHeightPx) }
90+
10091 }
10192}
10293
10394@Composable
10495internal fun rememberCollapsibleHeaderState (
10596 density : Density = LocalDensity .current,
10697 windowInfo : WindowInfo = LocalWindowInfo .current,
107- stickyHeaderHeight : Dp = 48.dp,
98+ initialStickyHeaderHeightDp : Dp = 48.dp,
10899 minExpandedHeaderHeight : Dp = 146.dp,
109100): CollapsibleHeaderState {
110101 val containerSize = windowInfo.containerSize
111- return remember(density, containerSize, minExpandedHeaderHeight, stickyHeaderHeight ) {
102+ return remember(density, containerSize, minExpandedHeaderHeight, initialStickyHeaderHeightDp ) {
112103 val screenHeightDp = with (density) { containerSize.height.toDp() }
113104 val expandedHeaderHeightDp = (screenHeightDp * 0.18f ).coerceAtLeast(minExpandedHeaderHeight)
114105
115106 CollapsibleHeaderState (
116107 density = density,
117- stickyHeaderHeightDp = stickyHeaderHeight ,
108+ initialStickyHeaderHeightDp = initialStickyHeaderHeightDp ,
118109 expandedHeaderHeightDp = expandedHeaderHeightDp,
119110 )
120111 }
0 commit comments