Created
December 4, 2024 07:09
-
-
Save shoaibmushtaq25/92632b418223439955956c83e1963811 to your computer and use it in GitHub Desktop.
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
class CollapsingAppBarNestedScrollConnection : NestedScrollConnection { | |
var headerOffset: Float by mutableFloatStateOf(0f) | |
private set | |
var progress: Float by mutableFloatStateOf(1f) | |
private set | |
var maxHeight: Float by mutableFloatStateOf(0f) | |
var minHeight: Float by mutableFloatStateOf(0f) | |
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset { | |
val delta = available.y | |
/** | |
* when direction is negative, meaning scrolling downward, | |
* we are not consuming delta but passing it for Node Consumption | |
*/ | |
if (delta >= 0f) { | |
return Offset.Zero | |
} | |
val newOffset = headerOffset + delta | |
val previousOffset = headerOffset | |
val heightDelta = -(maxHeight - minHeight) | |
headerOffset = if (heightDelta > 0) 0f else newOffset.coerceIn(heightDelta, 0f) | |
progress = 1f - headerOffset / -maxHeight | |
val consumed = headerOffset - previousOffset | |
return Offset(0f, consumed) | |
} | |
override fun onPostScroll(consumed: Offset, available: Offset, source: NestedScrollSource): Offset { | |
val delta = available.y | |
val newOffset = headerOffset + delta | |
val previousOffset = headerOffset | |
val heightDelta = -(maxHeight - minHeight) | |
headerOffset = if (heightDelta > 0) 0f else newOffset.coerceIn(heightDelta, 0f) | |
progress = 1f - headerOffset / -maxHeight | |
val consumedValue = headerOffset - previousOffset | |
return Offset(0f, consumedValue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment