From 31d52ad7235bca0e98e2e882c050875ba7682ce2 Mon Sep 17 00:00:00 2001 From: Tyler-Lopez <77797048+tyler-lopez@users.noreply.github.com> Date: Fri, 1 Nov 2024 12:37:50 -0600 Subject: [PATCH] Convert `CartesianLayerPadding` into an `ExtraStore` lambda Co-authored-by: Patrick Michalik <120058021+patrickmichalik@users.noreply.github.com> Co-authored-by: Patryk Goworowski --- .../vico/sample/showcase/charts/Chart1.kt | 2 +- .../vico/sample/showcase/charts/Chart4.kt | 2 +- .../vico/sample/showcase/charts/Chart5.kt | 2 +- .../vico/sample/showcase/charts/Chart6.kt | 2 +- .../vico/sample/showcase/charts/Chart7.kt | 2 +- .../vico/sample/showcase/charts/Chart8.kt | 2 +- .../vico/compose/cartesian/CartesianChart.kt | 2 +- .../compose/cartesian/CartesianChartHost.kt | 3 ++- .../vico/core/cartesian/CartesianChart.kt | 6 +++--- .../vico/core/cartesian/CartesianChartTest.kt | 2 +- .../views/cartesian/CartesianChartView.kt | 21 +++++++++++++------ .../vico/views/common/theme/ThemeHandler.kt | 3 ++- 12 files changed, 30 insertions(+), 19 deletions(-) diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt index c04c1d508..56b56365e 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt @@ -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, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt index e00ba88ea..5bd1264b6 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt @@ -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, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt index 9691fb393..fec319955 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt @@ -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, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt index a2b727e3c..4fd4a46e4 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt @@ -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, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt index 5f0b299bb..8cf5aef8d 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt @@ -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, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt index 860f519f7..11f2959c6 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt @@ -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, diff --git a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChart.kt b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChart.kt index 64f5a5e23..63ceb24b3 100644 --- a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChart.kt +++ b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChart.kt @@ -52,7 +52,7 @@ public fun rememberCartesianChart( bottomAxis: Axis? = null, marker: CartesianMarker? = null, markerVisibilityListener: CartesianMarkerVisibilityListener? = null, - layerPadding: CartesianLayerPadding = cartesianLayerPadding(), + layerPadding: ((ExtraStore) -> CartesianLayerPadding) = { cartesianLayerPadding() }, legend: Legend? = null, fadingEdges: FadingEdges? = null, decorations: List = emptyList(), diff --git a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChartHost.kt b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChartHost.kt index f6372c077..376e878cb 100644 --- a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChartHost.kt +++ b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChartHost.kt @@ -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 }, ) diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianChart.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianChart.kt index 2da0369bb..f22aaec37 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianChart.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianChart.kt @@ -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. @@ -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? = null, protected val fadingEdges: FadingEdges? = null, protected val decorations: List = emptyList(), @@ -408,7 +408,7 @@ public open class CartesianChart( bottomAxis: Axis? = this.bottomAxis, marker: CartesianMarker? = this.marker, markerVisibilityListener: CartesianMarkerVisibilityListener? = this.markerVisibilityListener, - layerPadding: CartesianLayerPadding = this.layerPadding, + layerPadding: ((ExtraStore) -> CartesianLayerPadding) = this.layerPadding, legend: Legend? = this.legend, fadingEdges: FadingEdges? = this.fadingEdges, decorations: List = this.decorations, diff --git a/vico/core/src/test/java/com/patrykandpatrick/vico/core/cartesian/CartesianChartTest.kt b/vico/core/src/test/java/com/patrykandpatrick/vico/core/cartesian/CartesianChartTest.kt index 7b2e85974..4b8e8466b 100644 --- a/vico/core/src/test/java/com/patrykandpatrick/vico/core/cartesian/CartesianChartTest.kt +++ b/vico/core/src/test/java/com/patrykandpatrick/vico/core/cartesian/CartesianChartTest.kt @@ -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()) diff --git a/vico/views/src/main/java/com/patrykandpatrick/vico/views/cartesian/CartesianChartView.kt b/vico/views/src/main/java/com/patrykandpatrick/vico/views/cartesian/CartesianChartView.kt index 3ef29689f..14feedbee 100644 --- a/vico/views/src/main/java/com/patrykandpatrick/vico/views/cartesian/CartesianChartView.kt +++ b/vico/views/src/main/java/com/patrykandpatrick/vico/views/cartesian/CartesianChartView.kt @@ -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 @@ -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, ) @@ -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. @@ -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) } @@ -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() } diff --git a/vico/views/src/main/java/com/patrykandpatrick/vico/views/common/theme/ThemeHandler.kt b/vico/views/src/main/java/com/patrykandpatrick/vico/views/common/theme/ThemeHandler.kt index ff9ae056e..c5b0f1202 100644 --- a/vico/views/src/main/java/com/patrykandpatrick/vico/views/common/theme/ThemeHandler.kt +++ b/vico/views/src/main/java/com/patrykandpatrick/vico/views/common/theme/ThemeHandler.kt @@ -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 = @@ -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 }, ) }