Skip to content

Commit

Permalink
Hide list item spacer and divider if the item is not visible (#3408)
Browse files Browse the repository at this point in the history
* Hide list item spacer and divider if the item is not visible

* Add doc

* spotless
  • Loading branch information
FikriMilano authored Jul 30, 2024
1 parent fd0da17 commit 5936d55
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,51 @@ fun List(
.testTag(VERTICAL_ORIENTATION),
) {
currentListResourceData.forEachIndexed { index, listResourceData ->
Spacer(modifier = modifier.height(6.dp))
Column(
modifier =
Modifier.padding(
horizontal = viewProperties.padding.dp,
vertical = viewProperties.padding.div(4).dp,
),
) {
AnimatedVisibility(
visible = true,
enter =
slideInVertically {
// Slide in from 40 dp from the top.
with(density) { -40.dp.roundToPx() }
},
// Interpolate ViewProperties up-front to hide the child view spacers and divider when
// the child view is not visible
val interpolatedChildViewProperties =
viewProperties.registerCard.views.map { viewProperty ->
viewProperty.interpolate(listResourceData.computedValuesMap)
}
// At least 1 child view must be visible in order to show the spacers and divider
val areChildViewsVisible =
interpolatedChildViewProperties.any { viewProperty ->
viewProperty.visible.toBooleanStrict()
}
if (areChildViewsVisible) {
Spacer(modifier = modifier.height(6.dp))
Column(
modifier =
Modifier.padding(
horizontal = viewProperties.padding.dp,
vertical = viewProperties.padding.div(4).dp,
),
) {
ViewRenderer(
viewProperties = viewProperties.registerCard.views,
resourceData = listResourceData,
navController = navController,
decodedImageMap = decodedImageMap,
)
AnimatedVisibility(
visible = true,
enter =
slideInVertically {
// Slide in from 40 dp from the top.
with(density) { -40.dp.roundToPx() }
},
) {
ViewRenderer(
viewProperties = interpolatedChildViewProperties,
resourceData = listResourceData,
navController = navController,
decodedImageMap = decodedImageMap,
areViewPropertiesInterpolated =
true, // Prevents double interpolation (in this function and inside the
// ViewRenderer) which is a waste
)
}
}
Spacer(modifier = modifier.height(6.dp))
// viewProperties in this case belongs to the List, setting the showDivider will
// apply to all child items under the List
if (index < currentListResourceData.lastIndex && viewProperties.showDivider) {
Divider(color = DividerColor, thickness = 0.5.dp)
}
}
Spacer(modifier = modifier.height(6.dp))
if (index < currentListResourceData.lastIndex && viewProperties.showDivider) {
Divider(color = DividerColor, thickness = 0.5.dp)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ fun ViewRenderer(
resourceData: ResourceData,
navController: NavController,
decodedImageMap: SnapshotStateMap<String, Bitmap>,
areViewPropertiesInterpolated: Boolean = false,
) {
viewProperties.forEach { properties ->
val interpolatedProperties =
if (areViewPropertiesInterpolated) {
properties
} else properties.interpolate(resourceData.computedValuesMap)
GenerateView(
modifier = generateModifier(properties),
properties = properties.interpolate(resourceData.computedValuesMap),
properties = interpolatedProperties,
resourceData = resourceData,
navController = navController,
decodedImageMap = decodedImageMap,
Expand Down

0 comments on commit 5936d55

Please sign in to comment.