Created
May 19, 2022 16:24
-
-
Save horseunnamed/76650ce8bccd54271a91a779e6aa452f to your computer and use it in GitHub Desktop.
NavBar Impl
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
/** | |
* Фиксирует значения для collapsing-трансформации тайтла [NavBar] | |
* Расчитаны по значениям из дизайн-системы для используемых нами стилей тайтла [NavBar] | |
* | |
* @param titleOffsetYPx количество пикселей, на которое надо сдвинуть title при переходе в collapsed-состояние | |
* @param heightToCollapsePx количество пикселей, на которое должна уменьшиться высота toolbar при collapsing-е | |
* @param scaleFactor множитель для перехода от размера высоты исходного текста к высоте текста в collapsed-состоянии | |
*/ | |
@Stable | |
data class CollapsingTitleSpec( | |
val titleOffsetYPx: Int, | |
val heightToCollapsePx: Int, | |
val scaleFactor: Float, | |
) { | |
companion object { | |
val Section: CollapsingTitleSpec | |
@Composable | |
get() = create(fromStyle = HHTextStyles.LargeTitle, toStyle = HHTextStyles.Title1) | |
val Subsection: CollapsingTitleSpec | |
@Composable | |
get() = create(fromStyle = HHTextStyles.Title2, toStyle = HHTextStyles.Title1) | |
@Composable | |
private fun create( | |
fromStyle: TextStyle, | |
toStyle: TextStyle, | |
): CollapsingTitleSpec { | |
with(LocalDensity.current) { | |
val fromLineHeightPx = fromStyle.lineHeight.roundToPx() | |
val toLineHeightPx = toStyle.lineHeight.roundToPx() | |
val collapsedTitlePaddingPx = 16.dp.roundToPx() | |
val expandedTitlePaddingPx = 8.dp.roundToPx() | |
return CollapsingTitleSpec( | |
titleOffsetYPx = collapsedTitlePaddingPx + toLineHeightPx, | |
heightToCollapsePx = expandedTitlePaddingPx + fromLineHeightPx, | |
scaleFactor = toLineHeightPx.toFloat() / fromLineHeightPx.toFloat() | |
) | |
} | |
} | |
} |
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
/** | |
* HACK: Compose tracks velocity with a local coordinate system which leads to an undesired | |
* scroll experience. To mitigate this issue, we use RelativeVelocityTracker which tracks velocity | |
* with a global coordinate system. In NestedScrollConnection, onPreScroll() gives us a delta | |
* based on a global coordinate, we can use this value to properly calculate the velocity. | |
* | |
* The fundamental goal of this class is to override the Compose-calculated scroll velocity to | |
* our manually calculated one. | |
* | |
* @see <a href="https://issuetracker.google.com/issues/179417109">this issue</a> | |
*/ | |
internal class RelativeVelocityTracker { | |
private val tracker = VelocityTracker() | |
private var lastY: Float? = null | |
fun addYDelta(delta: Float) { | |
val new = (lastY ?: 0f) + delta | |
tracker.addPosition(SystemClock.uptimeMillis(), Offset(0f, new)) | |
lastY = new | |
} | |
fun reset(): Float { | |
lastY = null | |
val velocity = tracker.calculateVelocity() | |
tracker.resetTracking() | |
return velocity.y | |
} | |
} |
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
// Пример использования NavBar | |
@Composable | |
fun NavBarSample() { | |
val scrollConnection = createNavBarScrollConnection() | |
Scaffold( | |
topBar = { | |
NavBar( | |
titleContent = { | |
Text( | |
text = "Title", | |
style = HHTextStyles.Title1, | |
maxLines = 1, | |
overflow = TextOverflow.Ellipsis | |
) | |
}, | |
scrollConnection = scrollConnection, | |
) | |
}, | |
modifier = Modifier | |
.nestedScroll(scrollConnection) | |
) { | |
LazyColumn(content = { /* scrollable content */ }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment