Skip to content

Commit

Permalink
Merge pull request onebone#26 from RareScrap/bugfix/performance
Browse files Browse the repository at this point in the history
Removed redundant SubcomposeLayout in favor of Layout
  • Loading branch information
onebone authored Jan 5, 2022
2 parents b40e108 + 368633a commit 5decc54
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions lib/src/main/java/me/onebone/toolbar/CollapsingToolbarScaffold.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.layout.Layout
import kotlin.math.max

@Stable
Expand Down Expand Up @@ -88,34 +88,33 @@ fun CollapsingToolbarScaffold(

val toolbarState = state.toolbarState

SubcomposeLayout(
Layout(
content = {
CollapsingToolbar(
modifier = toolbarModifier,
collapsingToolbarState = toolbarState
) {
toolbar()
}
body()
},
modifier = modifier
.then(
if(enabled) {
if (enabled) {
Modifier.nestedScroll(nestedScrollConnection)
}else{
} else {
Modifier
}
)
) { constraints ->
) { measurables, constraints ->
val toolbarConstraints = constraints.copy(
minWidth = 0,
minHeight = 0
)

val toolbarPlaceables = subcompose(CollapsingToolbarScaffoldContent.Toolbar) {
CollapsingToolbar(
modifier = toolbarModifier,
collapsingToolbarState = toolbarState
) {
toolbar()
}
}.map { it.measure(toolbarConstraints) }

val bodyConstraints = constraints.copy(
minWidth = 0,
minHeight = 0,
maxHeight = when(scrollStrategy) {
maxHeight = when (scrollStrategy) {
ScrollStrategy.ExitUntilCollapsed ->
(constraints.maxHeight - toolbarState.minHeight).coerceAtLeast(0)

Expand All @@ -124,17 +123,16 @@ fun CollapsingToolbarScaffold(
}
)

val bodyPlaceables = subcompose(CollapsingToolbarScaffoldContent.Body) {
body()
}.map { it.measure(bodyConstraints) }
val toolbarPlaceable = measurables[0].measure(toolbarConstraints)
val bodyPlaceables =
measurables.drop(1).map { it.measure(bodyConstraints) }

val toolbarHeight = toolbarPlaceable.height

val width = max(
toolbarPlaceables.maxOfOrNull { it.width } ?: 0,
toolbarPlaceable.width,
bodyPlaceables.maxOfOrNull { it.width } ?: 0
).coerceIn(constraints.minWidth, constraints.maxWidth)

val toolbarHeight = toolbarPlaceables.maxOfOrNull { it.height } ?: 0

val height = max(
toolbarHeight,
bodyPlaceables.maxOfOrNull { it.height } ?: 0
Expand All @@ -144,14 +142,7 @@ fun CollapsingToolbarScaffold(
bodyPlaceables.forEach {
it.place(0, toolbarHeight + state.offsetY)
}

toolbarPlaceables.forEach {
it.place(0, state.offsetY)
}
toolbarPlaceable.place(0, state.offsetY)
}
}
}

private enum class CollapsingToolbarScaffoldContent {
Toolbar, Body
}

0 comments on commit 5decc54

Please sign in to comment.