A fully functional Android app built entirely with Kotlin and Jetpack Compose
Move state into a state holder and remove redundant Box
Ben Trengroved8880e98f073
diff --git a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/AppScrollbars.kt b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/AppScrollbars.ktindex beece9071..fa913cb27 100644--- a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/AppScrollbars.kt+++ b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/AppScrollbars.kt@@ -38,7 +38,6 @@ import androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.material3.MaterialThemeimport androidx.compose.runtime.Composableimport androidx.compose.runtime.LaunchedEffect-import androidx.compose.runtime.Stateimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.remember@@ -67,7 +66,7 @@ private const val SCROLLBAR_INACTIVE_TO_DORMANT_TIME_IN_MS = 2_000L@Composablefun ScrollableState.DraggableScrollbar(modifier: Modifier = Modifier,- state: State<ScrollbarState>,+ state: ScrollbarState,orientation: Orientation,onThumbMoved: (Float) -> Unit,) {@@ -97,7 +96,7 @@ fun ScrollableState.DraggableScrollbar(@Composablefun ScrollableState.DecorativeScrollbar(modifier: Modifier = Modifier,- state: State<ScrollbarState>,+ state: ScrollbarState,orientation: Orientation,) {val interactionSource = remember { MutableInteractionSource() }diff --git a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/Scrollbar.kt b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/Scrollbar.ktindex b147ac2d2..bb3b08c3a 100644--- a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/Scrollbar.kt+++ b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/Scrollbar.kt@@ -32,9 +32,9 @@ import androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.runtime.Composableimport androidx.compose.runtime.Immutableimport androidx.compose.runtime.LaunchedEffect-import androidx.compose.runtime.Stateimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableFloatStateOf+import androidx.compose.runtime.mutableLongStateOfimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValue@@ -44,10 +44,10 @@ import androidx.compose.ui.Modifierimport androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.input.pointer.PointerInputChangeimport androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.layout.Layoutimport androidx.compose.ui.layout.layoutimport androidx.compose.ui.layout.onGloballyPositionedimport androidx.compose.ui.layout.positionInRoot-import androidx.compose.ui.platform.LocalDensityimport androidx.compose.ui.unit.Constraintsimport androidx.compose.ui.unit.Dpimport androidx.compose.ui.unit.IntOffset@@ -75,21 +75,53 @@ private const val SCROLLBAR_PRESS_DELAY_MS = 10L*/private const val SCROLLBAR_PRESS_DELTA_PCT = 0.02f+class ScrollbarState {+ private var packedValue by mutableLongStateOf(0L)++ internal fun onScroll(stateValue: ScrollbarStateValue) {+ packedValue = stateValue.packedValue+ }++ /**+ * Returns the thumb size of the scrollbar as a percentage of the total track size+ */+ val thumbSizePercent+ get() = unpackFloat1(packedValue)++ /**+ * Returns the distance the thumb has traveled as a percentage of total track size+ */+ val thumbMovedPercent+ get() = unpackFloat2(packedValue)+}++/**+ * Returns the size of the scrollbar track in pixels+ */+private val ScrollbarTrack.size+ get() = unpackFloat2(packedValue) - unpackFloat1(packedValue)++/**+ * Returns the position of the scrollbar thumb on the track as a percentage+ */+private fun ScrollbarTrack.thumbPosition(+ dimension: Float,+): Float = max(+ a = min(+ a = dimension / size,+ b = 1f,+ ),+ b = 0f,+)+/*** Class definition for the core properties of a scroll bar*/@Immutable@JvmInline-value class ScrollbarState internal constructor(+value class ScrollbarStateValue internal constructor(internal val packedValue: Long,-) {- companion object {- val FULL = ScrollbarState(- thumbSizePercent = 1f,- thumbMovedPercent = 0f,- )- }-}+)/*** Class definition for the core properties of a scroll bar track@@ -106,54 +138,23 @@ private value class ScrollbarTrack(}/**- * Creates a [ScrollbarState] with the listed properties+ * Creates a [ScrollbarStateValue] with the listed properties* @param thumbSizePercent the thumb size of the scrollbar as a percentage of the total track size.* Refers to either the thumb width (for horizontal scrollbars)* or height (for vertical scrollbars).* @param thumbMovedPercent the distance the thumb has traveled as a percentage of total* track size.*/-fun ScrollbarState(+fun scrollbarStateValue(thumbSizePercent: Float,thumbMovedPercent: Float,-) = ScrollbarState(+) = ScrollbarStateValue(packFloats(val1 = thumbSizePercent,val2 = thumbMovedPercent,),)-/**- * Returns the thumb size of the scrollbar as a percentage of the total track size- */-val ScrollbarState.thumbSizePercent- get() = unpackFloat1(packedValue)--/**- * Returns the distance the thumb has traveled as a percentage of total track size- */-val ScrollbarState.thumbMovedPercent- get() = unpackFloat2(packedValue)--/**- * Returns the size of the scrollbar track in pixels- */-private val ScrollbarTrack.size- get() = unpackFloat2(packedValue) - unpackFloat1(packedValue)--/**- * Returns the position of the scrollbar thumb on the track as a percentage- */-private fun ScrollbarTrack.thumbPosition(- dimension: Float,-): Float = max(- a = min(- a = dimension / size,- b = 1f,- ),- b = 0f,-)-/*** Returns the value of [offset] along the axis specified by [this]*/@@ -192,14 +193,12 @@ internal fun Orientation.valueOf(intOffset: IntOffset) = when (this) {fun Scrollbar(modifier: Modifier = Modifier,orientation: Orientation,- state: State<ScrollbarState>,+ state: ScrollbarState,minThumbSize: Dp = 40.dp,interactionSource: MutableInteractionSource? = null,thumb: @Composable () -> Unit,onThumbMoved: ((Float) -> Unit)? = null,) {- val localDensity = LocalDensity.current-// Using Offset.Unspecified and Float.NaN instead of null// to prevent unnecessary boxing of primitivesvar pressedOffset by remember { mutableStateOf(Offset.Unspecified) }@@ -305,68 +304,57 @@ fun Scrollbar(},) {// scrollbar thumb container- Box(- modifier = Modifier- .align(Alignment.TopStart)- .layout { measurable, constraints ->- val state = state.value- val thumbSizePx = max(- a = state.thumbSizePercent * track.size,- b = minThumbSize.toPx(),- )+ Layout(content = { thumb() }) { measurables, constraints ->+ val measurable = measurables.first()- val thumbTravelPercent = when {- interactionThumbTravelPercent.isNaN() -> state.thumbMovedPercent- else -> interactionThumbTravelPercent- }-- val thumbMovedPx = min(- a = track.size * thumbTravelPercent,- b = track.size - thumbSizePx,- )+ val thumbSizePx = max(+ a = state.thumbSizePercent * track.size,+ b = minThumbSize.toPx(),+ )- val scrollbarThumbMovedDp = max(- a = thumbMovedPx,- b = 0f,- )+ val thumbTravelPercent = when {+ interactionThumbTravelPercent.isNaN() -> state.thumbMovedPercent+ else -> interactionThumbTravelPercent+ }- val y = when (orientation) {- Horizontal -> 0- Vertical -> scrollbarThumbMovedDp- }- val x = when (orientation) {- Horizontal -> scrollbarThumbMovedDp- Vertical -> 0- }+ val thumbMovedPx = min(+ a = track.size * thumbTravelPercent,+ b = track.size - thumbSizePx,+ )- val offset = IntOffset(x.toInt(), y.toInt())+ val scrollbarThumbMovedPx = max(+ a = thumbMovedPx.roundToInt(),+ b = 0,+ )- val constrained = when (orientation) {- Horizontal -> {- Constraints(- minWidth = thumbSizePx.roundToInt(),- maxWidth = thumbSizePx.roundToInt(),- minHeight = constraints.minHeight,- maxHeight = constraints.maxHeight- )- }- Vertical -> {- Constraints(- minWidth = constraints.minWidth,- maxWidth = constraints.maxWidth,- minHeight = thumbSizePx.roundToInt(),- maxHeight = thumbSizePx.roundToInt()- )- }- }+ val y = when (orientation) {+ Horizontal -> 0+ Vertical -> scrollbarThumbMovedPx+ }+ val x = when (orientation) {+ Horizontal -> scrollbarThumbMovedPx+ Vertical -> 0+ }- val placeable = measurable.measure(constrained)- layout(placeable.width, placeable.height) {- placeable.place(offset)- }+ val updatedConstraints = when (orientation) {+ Horizontal -> {+ constraints.copy(+ minWidth = thumbSizePx.roundToInt(),+ maxWidth = thumbSizePx.roundToInt()+ )}- ) {- thumb()+ Vertical -> {+ constraints.copy(+ minHeight = thumbSizePx.roundToInt(),+ maxHeight = thumbSizePx.roundToInt()+ )+ }+ }++ val placeable = measurable.measure(updatedConstraints)+ layout(placeable.width, placeable.height) {+ placeable.place(x, y)+ }}}@@ -381,7 +369,7 @@ fun Scrollbar(return@collect}- var currentThumbMovedPercent = state.value.thumbMovedPercent+ var currentThumbMovedPercent = state.thumbMovedPercentval destinationThumbMovedPercent = track.thumbPosition(dimension = orientation.valueOf(pressedOffset),)diff --git a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.ktindex 67abce1b7..a55f62f5f 100644--- a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt+++ b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt@@ -24,8 +24,8 @@ import androidx.compose.foundation.lazy.grid.LazyGridStateimport androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridItemInfoimport androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridStateimport androidx.compose.runtime.Composable-import androidx.compose.runtime.State-import androidx.compose.runtime.produceState+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.rememberimport androidx.compose.runtime.snapshotFlowimport kotlinx.coroutines.flow.distinctUntilChangedimport kotlinx.coroutines.flow.filterNotNull@@ -41,57 +41,57 @@ import kotlin.math.minfun LazyListState.scrollbarState(itemsAvailable: Int,itemIndex: (LazyListItemInfo) -> Int = LazyListItemInfo::index,-): State<ScrollbarState> = produceState(- initialValue = ScrollbarState.FULL,- key1 = this,- key2 = itemsAvailable,-) {- snapshotFlow {- if (itemsAvailable == 0) return@snapshotFlow null-- val visibleItemsInfo = layoutInfo.visibleItemsInfo- if (visibleItemsInfo.isEmpty()) return@snapshotFlow null-- val firstIndex = min(- a = interpolateFirstItemIndex(- visibleItems = visibleItemsInfo,- itemSize = { it.size },- offset = { it.offset },- nextItemOnMainAxis = { first -> visibleItemsInfo.find { it != first } },- itemIndex = itemIndex,- ),- b = itemsAvailable.toFloat(),- )- if (firstIndex.isNaN()) return@snapshotFlow null-- val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->- itemVisibilityPercentage(- itemSize = itemInfo.size,- itemStartOffset = itemInfo.offset,- viewportStartOffset = layoutInfo.viewportStartOffset,- viewportEndOffset = layoutInfo.viewportEndOffset,+): ScrollbarState {+ val state = remember { ScrollbarState() }+ LaunchedEffect(this, itemsAvailable) {+ snapshotFlow {+ if (itemsAvailable == 0) return@snapshotFlow null++ val visibleItemsInfo = layoutInfo.visibleItemsInfo+ if (visibleItemsInfo.isEmpty()) return@snapshotFlow null++ val firstIndex = min(+ a = interpolateFirstItemIndex(+ visibleItems = visibleItemsInfo,+ itemSize = { it.size },+ offset = { it.offset },+ nextItemOnMainAxis = { first -> visibleItemsInfo.find { it != first } },+ itemIndex = itemIndex,+ ),+ b = itemsAvailable.toFloat(),+ )+ if (firstIndex.isNaN()) return@snapshotFlow null++ val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->+ itemVisibilityPercentage(+ itemSize = itemInfo.size,+ itemStartOffset = itemInfo.offset,+ viewportStartOffset = layoutInfo.viewportStartOffset,+ viewportEndOffset = layoutInfo.viewportEndOffset,+ )+ }++ val thumbTravelPercent = min(+ a = firstIndex / itemsAvailable,+ b = 1f,+ )+ val thumbSizePercent = min(+ a = itemsVisible / itemsAvailable,+ b = 1f,+ )+ scrollbarStateValue(+ thumbSizePercent = thumbSizePercent,+ thumbMovedPercent = when {+ layoutInfo.reverseLayout -> 1f - thumbTravelPercent+ else -> thumbTravelPercent+ },)}-- val thumbTravelPercent = min(- a = firstIndex / itemsAvailable,- b = 1f,- )- val thumbSizePercent = min(- a = itemsVisible / itemsAvailable,- b = 1f,- )- ScrollbarState(- thumbSizePercent = thumbSizePercent,- thumbMovedPercent = when {- layoutInfo.reverseLayout -> 1f - thumbTravelPercent- else -> thumbTravelPercent- },- )+ .filterNotNull()+ .distinctUntilChanged()+ .collect { state.onScroll(it) }}- .filterNotNull()- .distinctUntilChanged()- .collect { value = it }+ return state}/**@@ -104,67 +104,67 @@ fun LazyListState.scrollbarState(fun LazyGridState.scrollbarState(itemsAvailable: Int,itemIndex: (LazyGridItemInfo) -> Int = LazyGridItemInfo::index,-): State<ScrollbarState> = produceState(- initialValue = ScrollbarState.FULL,- key1 = this,- key2 = itemsAvailable,-) {- snapshotFlow {- if (itemsAvailable == 0) return@snapshotFlow null-- val visibleItemsInfo = layoutInfo.visibleItemsInfo- if (visibleItemsInfo.isEmpty()) return@snapshotFlow null-- val firstIndex = min(- a = interpolateFirstItemIndex(- visibleItems = visibleItemsInfo,- itemSize = { layoutInfo.orientation.valueOf(it.size) },- offset = { layoutInfo.orientation.valueOf(it.offset) },- nextItemOnMainAxis = { first ->- when (layoutInfo.orientation) {- Orientation.Vertical -> visibleItemsInfo.find {- it != first && it.row != first.row- }-- Orientation.Horizontal -> visibleItemsInfo.find {- it != first && it.column != first.column+): ScrollbarState {+ val state = remember { ScrollbarState() }+ LaunchedEffect(this, itemsAvailable) {+ snapshotFlow {+ if (itemsAvailable == 0) return@snapshotFlow null++ val visibleItemsInfo = layoutInfo.visibleItemsInfo+ if (visibleItemsInfo.isEmpty()) return@snapshotFlow null++ val firstIndex = min(+ a = interpolateFirstItemIndex(+ visibleItems = visibleItemsInfo,+ itemSize = { layoutInfo.orientation.valueOf(it.size) },+ offset = { layoutInfo.orientation.valueOf(it.offset) },+ nextItemOnMainAxis = { first ->+ when (layoutInfo.orientation) {+ Orientation.Vertical -> visibleItemsInfo.find {+ it != first && it.row != first.row+ }++ Orientation.Horizontal -> visibleItemsInfo.find {+ it != first && it.column != first.column+ }}- }+ },+ itemIndex = itemIndex,+ ),+ b = itemsAvailable.toFloat(),+ )+ if (firstIndex.isNaN()) return@snapshotFlow null++ val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->+ itemVisibilityPercentage(+ itemSize = layoutInfo.orientation.valueOf(itemInfo.size),+ itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),+ viewportStartOffset = layoutInfo.viewportStartOffset,+ viewportEndOffset = layoutInfo.viewportEndOffset,+ )+ }++ val thumbTravelPercent = min(+ a = firstIndex / itemsAvailable,+ b = 1f,+ )+ val thumbSizePercent = min(+ a = itemsVisible / itemsAvailable,+ b = 1f,+ )+ scrollbarStateValue(+ thumbSizePercent = thumbSizePercent,+ thumbMovedPercent = when {+ layoutInfo.reverseLayout -> 1f - thumbTravelPercent+ else -> thumbTravelPercent},- itemIndex = itemIndex,- ),- b = itemsAvailable.toFloat(),- )- if (firstIndex.isNaN()) return@snapshotFlow null-- val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->- itemVisibilityPercentage(- itemSize = layoutInfo.orientation.valueOf(itemInfo.size),- itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),- viewportStartOffset = layoutInfo.viewportStartOffset,- viewportEndOffset = layoutInfo.viewportEndOffset,)}-- val thumbTravelPercent = min(- a = firstIndex / itemsAvailable,- b = 1f,- )- val thumbSizePercent = min(- a = itemsVisible / itemsAvailable,- b = 1f,- )- ScrollbarState(- thumbSizePercent = thumbSizePercent,- thumbMovedPercent = when {- layoutInfo.reverseLayout -> 1f - thumbTravelPercent- else -> thumbTravelPercent- },- )+ .filterNotNull()+ .distinctUntilChanged()+ .collect { state.onScroll(it) }}- .filterNotNull()- .distinctUntilChanged()- .collect { value = it }+ return state}/**@@ -178,56 +178,56 @@ fun LazyGridState.scrollbarState(fun LazyStaggeredGridState.scrollbarState(itemsAvailable: Int,itemIndex: (LazyStaggeredGridItemInfo) -> Int = LazyStaggeredGridItemInfo::index,-): State<ScrollbarState> = produceState(- initialValue = ScrollbarState.FULL,- key1 = this,- key2 = itemsAvailable,-) {- snapshotFlow {- if (itemsAvailable == 0) return@snapshotFlow null-- val visibleItemsInfo = layoutInfo.visibleItemsInfo- if (visibleItemsInfo.isEmpty()) return@snapshotFlow null-- val firstIndex = min(- a = interpolateFirstItemIndex(- visibleItems = visibleItemsInfo,- itemSize = { layoutInfo.orientation.valueOf(it.size) },- offset = { layoutInfo.orientation.valueOf(it.offset) },- nextItemOnMainAxis = { first ->- visibleItemsInfo.find { it != first && it.lane == first.lane }- },- itemIndex = itemIndex,- ),- b = itemsAvailable.toFloat(),- )- if (firstIndex.isNaN()) return@snapshotFlow null-- val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->- itemVisibilityPercentage(- itemSize = layoutInfo.orientation.valueOf(itemInfo.size),- itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),- viewportStartOffset = layoutInfo.viewportStartOffset,- viewportEndOffset = layoutInfo.viewportEndOffset,+): ScrollbarState {+ val state = remember { ScrollbarState() }+ LaunchedEffect(this, itemsAvailable) {+ snapshotFlow {+ if (itemsAvailable == 0) return@snapshotFlow null++ val visibleItemsInfo = layoutInfo.visibleItemsInfo+ if (visibleItemsInfo.isEmpty()) return@snapshotFlow null++ val firstIndex = min(+ a = interpolateFirstItemIndex(+ visibleItems = visibleItemsInfo,+ itemSize = { layoutInfo.orientation.valueOf(it.size) },+ offset = { layoutInfo.orientation.valueOf(it.offset) },+ nextItemOnMainAxis = { first ->+ visibleItemsInfo.find { it != first && it.lane == first.lane }+ },+ itemIndex = itemIndex,+ ),+ b = itemsAvailable.toFloat(),+ )+ if (firstIndex.isNaN()) return@snapshotFlow null++ val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->+ itemVisibilityPercentage(+ itemSize = layoutInfo.orientation.valueOf(itemInfo.size),+ itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),+ viewportStartOffset = layoutInfo.viewportStartOffset,+ viewportEndOffset = layoutInfo.viewportEndOffset,+ )+ }++ val thumbTravelPercent = min(+ a = firstIndex / itemsAvailable,+ b = 1f,+ )+ val thumbSizePercent = min(+ a = itemsVisible / itemsAvailable,+ b = 1f,+ )+ scrollbarStateValue(+ thumbSizePercent = thumbSizePercent,+ thumbMovedPercent = thumbTravelPercent,)}-- val thumbTravelPercent = min(- a = firstIndex / itemsAvailable,- b = 1f,- )- val thumbSizePercent = min(- a = itemsVisible / itemsAvailable,- b = 1f,- )- ScrollbarState(- thumbSizePercent = thumbSizePercent,- thumbMovedPercent = thumbTravelPercent,- )+ .filterNotNull()+ .distinctUntilChanged()+ .collect { state.onScroll(it) }}- .filterNotNull()- .distinctUntilChanged()- .collect { value = it }+ return state}private inline fun <T> List<T>.floatSumOf(selector: (T) -> Float): Float {