Skip to content

Commit

Permalink
Convert CartesianLayerPadding into an ExtraStore lambda
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick Michalik <[email protected]>
Co-authored-by: Patryk Goworowski <[email protected]>
  • Loading branch information
3 people committed Dec 12, 2024
1 parent a456063 commit 31d52ad
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private fun ComposeChart1(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
),
marker = marker,
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPadding = { cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp) },
persistentMarkers = rememberExtraLambda(marker) { marker at PERSISTENT_MARKER_X },
),
modelProducer = modelProducer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private fun ComposeChart4(modelProducer: CartesianChartModelProducer, modifier:
),
endAxis = VerticalAxis.rememberEnd(),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPadding = { cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp) },
),
modelProducer = modelProducer,
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private fun ComposeChart5(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPadding = { cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp) },
),
modelProducer = modelProducer,
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private fun ComposeChart6(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPadding = { cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp) },
decorations = listOf(rememberComposeHorizontalBox()),
),
modelProducer = modelProducer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private fun ComposeChart7(modelProducer: CartesianChartModelProducer, modifier:
bottomAxis =
HorizontalAxis.rememberBottom(itemPlacer = HorizontalAxis.ItemPlacer.segmented()),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPadding = { cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp) },
legend = rememberLegend(),
),
modelProducer = modelProducer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private fun ComposeChart8(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }
),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPadding = { cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp) },
),
modelProducer = modelProducer,
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public fun rememberCartesianChart(
bottomAxis: Axis<Axis.Position.Horizontal.Bottom>? = null,
marker: CartesianMarker? = null,
markerVisibilityListener: CartesianMarkerVisibilityListener? = null,
layerPadding: CartesianLayerPadding = cartesianLayerPadding(),
layerPadding: ((ExtraStore) -> CartesianLayerPadding) = { cartesianLayerPadding() },
legend: Legend<CartesianMeasuringContext, CartesianDrawingContext>? = null,
fadingEdges: FadingEdges? = null,
decorations: List<Decoration> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ internal fun CartesianChartHostImpl(
ranges = ranges,
scrollEnabled = scrollState.scrollEnabled,
zoomEnabled = scrollState.scrollEnabled && zoomState.zoomEnabled,
layerPadding = chart.layerPadding,
layerPadding =
remember(chart.layerPadding, model.extraStore) { chart.layerPadding(model.extraStore) },
spToPx = with(LocalContext.current) { ::spToPx },
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import kotlin.math.abs
* @property layers the [CartesianLayer]s.
* @property marker appears when the [CartesianChart] is tapped.
* @property markerVisibilityListener allows for listening to [marker] visibility changes.
* @property layerPadding stores the [CartesianLayer] padding values.
* @property layerPadding returns the [CartesianLayerPadding].
* @property legend the legend.
* @property fadingEdges applies a horizontal fade to the edges of the [CartesianChart], provided
* that it’s scrollable.
Expand All @@ -80,7 +80,7 @@ public open class CartesianChart(
@get:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) public val marker: CartesianMarker? = null,
protected val markerVisibilityListener: CartesianMarkerVisibilityListener? = null,
@get:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public val layerPadding: CartesianLayerPadding = CartesianLayerPadding(),
public val layerPadding: ((ExtraStore) -> CartesianLayerPadding) = { CartesianLayerPadding() },
protected val legend: Legend<CartesianMeasuringContext, CartesianDrawingContext>? = null,
protected val fadingEdges: FadingEdges? = null,
protected val decorations: List<Decoration> = emptyList(),
Expand Down Expand Up @@ -408,7 +408,7 @@ public open class CartesianChart(
bottomAxis: Axis<Axis.Position.Horizontal.Bottom>? = this.bottomAxis,
marker: CartesianMarker? = this.marker,
markerVisibilityListener: CartesianMarkerVisibilityListener? = this.markerVisibilityListener,
layerPadding: CartesianLayerPadding = this.layerPadding,
layerPadding: ((ExtraStore) -> CartesianLayerPadding) = this.layerPadding,
legend: Legend<CartesianMeasuringContext, CartesianDrawingContext>? = this.legend,
fadingEdges: FadingEdges? = this.fadingEdges,
decorations: List<Decoration> = this.decorations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class CartesianChartTest {
public fun `Given CartesianChart is copied with changes, when it is compared to the original, then they are NOT equal`() {
val chart = getCartesianChart()

val copiedChart = chart.copy(layerPadding = CartesianLayerPadding(10f, 10f, 10f, 10f))
val copiedChart = chart.copy(layerPadding = { CartesianLayerPadding(10f, 10f, 10f, 10f) })

Assertions.assertNotEquals(chart, copiedChart)
Assertions.assertNotEquals(chart.hashCode(), copiedChart.hashCode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.patrykandpatrick.vico.views.common.gesture.ChartScaleGestureListener
import com.patrykandpatrick.vico.views.common.gesture.MotionEventHandler
import com.patrykandpatrick.vico.views.common.isLtr
import com.patrykandpatrick.vico.views.common.theme.ThemeHandler
import java.util.Objects
import kotlin.math.abs
import kotlin.properties.Delegates.observable
import kotlin.properties.ReadWriteProperty
Expand Down Expand Up @@ -81,7 +82,7 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
ranges = CartesianChartRanges.Empty,
scrollEnabled = false,
zoomEnabled = false,
layerPadding = themeHandler.chart?.layerPadding ?: CartesianLayerPadding(),
layerPadding = CartesianLayerPadding(),
spToPx = context::spToPx,
)

Expand All @@ -96,6 +97,8 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0

private var horizontalDimensions = MutableHorizontalDimensions()

private var previousLayerPaddingHashCode: Int? = null

/**
* Houses information on the [CartesianChart]’s scroll value. Allows for scroll customization and
* programmatic scrolling.
Expand Down Expand Up @@ -128,7 +131,6 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
/** The [CartesianChart] displayed by this [View]. */
public var chart: CartesianChart? by
observable(themeHandler.chart) { _, _, newValue ->
if (newValue != null) measuringContext.layerPadding = newValue.layerPadding
tryInvalidate(chart = newValue, model = model, updateRanges = true)
}

Expand Down Expand Up @@ -243,10 +245,17 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
updateRanges: Boolean,
) {
measuringContext.model = model ?: return
if (chart != null && updateRanges) {
ranges.reset()
chart.updateRanges(ranges, model)
measuringContext.ranges = ranges.toImmutable()
if (chart != null) {
val layerPaddingHashCode = Objects.hash(chart.layerPadding, model.extraStore)
if (layerPaddingHashCode != previousLayerPaddingHashCode) {
measuringContext.layerPadding = chart.layerPadding(model.extraStore)
previousLayerPaddingHashCode = layerPaddingHashCode
}
if (updateRanges) {
ranges.reset()
chart.updateRanges(ranges, model)
measuringContext.ranges = ranges.toImmutable()
}
}
if (isAttachedToWindow) invalidate()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ internal class ThemeHandler(private val context: Context, attrs: AttributeSet?)
val candlestickLayer =
if (layerFlags.hasFlag(CANDLESTICK_LAYER)) typedArray.getCandlestickCartesianLayer(context)
else null
val layerPadding = typedArray.getLayerPadding()

return CartesianChart(
layers =
Expand All @@ -225,7 +226,7 @@ internal class ThemeHandler(private val context: Context, attrs: AttributeSet?)
endAxis = typedArray.getAxis(Axis.Position.Vertical.End),
bottomAxis = typedArray.getAxis(Axis.Position.Horizontal.Bottom),
fadingEdges = typedArray.getFadingEdges(),
layerPadding = typedArray.getLayerPadding(),
layerPadding = { layerPadding },
)
}

Expand Down

0 comments on commit 31d52ad

Please sign in to comment.