From f6847a79944a4a5232765e9738470e61cb1eb318 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] Add CartesianLayerPaddingProvider Add CartesianLayerPaddingProvider This commit introduces CartesianLayerPaddingProvider, a means to retrieve CartesianLayerPadding with an `ExtraStore`. This commit does not substantially conflict with pre-existing functionality as `CartesianLayerPaddingProvider.fixed` is identical. --- .../vico/sample/showcase/charts/Chart1.kt | 5 ++- .../vico/sample/showcase/charts/Chart4.kt | 5 ++- .../vico/sample/showcase/charts/Chart5.kt | 5 ++- .../vico/sample/showcase/charts/Chart6.kt | 5 ++- .../vico/sample/showcase/charts/Chart7.kt | 5 ++- .../vico/sample/showcase/charts/Chart8.kt | 5 ++- .../vico/compose/cartesian/CartesianChart.kt | 9 ++--- .../compose/cartesian/CartesianChartHost.kt | 2 +- .../vico/core/cartesian/CartesianChart.kt | 11 +++--- .../core/cartesian/CartesianLayerPadding.kt | 34 +++++++++++++++++++ .../views/cartesian/CartesianChartView.kt | 11 ++++-- .../vico/views/common/theme/ThemeHandler.kt | 5 ++- 12 files changed, 83 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..20cfca36a 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 @@ -33,6 +33,7 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState import com.patrykandpatrick.vico.compose.common.data.rememberExtraLambda import com.patrykandpatrick.vico.compose.common.fill +import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer @@ -83,7 +84,9 @@ private fun ComposeChart1(modelProducer: CartesianChartModelProducer, modifier: itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }, ), marker = marker, - layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp), + layerPaddingProvider = CartesianLayerPaddingProvider.fixed( + 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 d6543b724..af64945d6 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 @@ -35,6 +35,7 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent import com.patrykandpatrick.vico.compose.common.fill import com.patrykandpatrick.vico.compose.common.shape.rounded +import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer @@ -117,7 +118,9 @@ private fun ComposeChart4(modelProducer: CartesianChartModelProducer, modifier: ), endAxis = VerticalAxis.rememberEnd(), marker = rememberMarker(), - layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp), + layerPaddingProvider = CartesianLayerPaddingProvider.fixed( + 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 21fdc24d2..63f86b115 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 @@ -31,6 +31,7 @@ import com.patrykandpatrick.vico.compose.cartesian.layer.rememberColumnCartesian import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent +import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer @@ -119,7 +120,9 @@ private fun ComposeChart5(modelProducer: CartesianChartModelProducer, modifier: itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }, ), marker = rememberMarker(), - layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp), + layerPaddingProvider = CartesianLayerPaddingProvider.fixed( + 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 67130a1bb..d2edd2a4d 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 @@ -35,6 +35,7 @@ import com.patrykandpatrick.vico.compose.common.component.rememberShapeComponent import com.patrykandpatrick.vico.compose.common.component.rememberTextComponent import com.patrykandpatrick.vico.compose.common.component.shapeComponent import com.patrykandpatrick.vico.compose.common.dimensions +import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer @@ -109,7 +110,9 @@ private fun ComposeChart6(modelProducer: CartesianChartModelProducer, modifier: itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }, ), marker = rememberMarker(), - layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp), + layerPaddingProvider = CartesianLayerPaddingProvider.fixed( + 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 3781e51e2..a441e9d49 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 @@ -44,6 +44,7 @@ import com.patrykandpatrick.vico.compose.common.rememberVerticalLegend import com.patrykandpatrick.vico.compose.common.shape.rounded import com.patrykandpatrick.vico.compose.common.vicoTheme import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext +import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider import com.patrykandpatrick.vico.core.cartesian.CartesianMeasuringContext import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis @@ -117,7 +118,9 @@ private fun ComposeChart7(modelProducer: CartesianChartModelProducer, modifier: bottomAxis = HorizontalAxis.rememberBottom(itemPlacer = HorizontalAxis.ItemPlacer.segmented()), marker = rememberMarker(), - layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp), + layerPaddingProvider = CartesianLayerPaddingProvider.fixed( + 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 b37dcd149..7a221379a 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 @@ -35,6 +35,7 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent import com.patrykandpatrick.vico.compose.common.fill +import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider import com.patrykandpatrick.vico.core.cartesian.axis.Axis import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis @@ -123,7 +124,9 @@ private fun ComposeChart8(modelProducer: CartesianChartModelProducer, modifier: itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() } ), marker = rememberMarker(), - layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp), + layerPaddingProvider = CartesianLayerPaddingProvider.fixed( + 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..d731ff23e 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 @@ -24,6 +24,7 @@ import com.patrykandpatrick.vico.compose.cartesian.layer.rememberLineCartesianLa import com.patrykandpatrick.vico.core.cartesian.CartesianChart import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPadding +import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider import com.patrykandpatrick.vico.core.cartesian.CartesianMeasuringContext import com.patrykandpatrick.vico.core.cartesian.FadingEdges import com.patrykandpatrick.vico.core.cartesian.axis.Axis @@ -52,7 +53,7 @@ public fun rememberCartesianChart( bottomAxis: Axis? = null, marker: CartesianMarker? = null, markerVisibilityListener: CartesianMarkerVisibilityListener? = null, - layerPadding: CartesianLayerPadding = cartesianLayerPadding(), + layerPaddingProvider: CartesianLayerPaddingProvider = CartesianLayerPaddingProvider.fixed(), legend: Legend? = null, fadingEdges: FadingEdges? = null, decorations: List = emptyList(), @@ -68,7 +69,7 @@ public fun rememberCartesianChart( bottomAxis, marker, markerVisibilityListener, - layerPadding, + layerPaddingProvider, legend, fadingEdges, decorations, @@ -84,7 +85,7 @@ public fun rememberCartesianChart( bottomAxis = bottomAxis, marker = marker, markerVisibilityListener = markerVisibilityListener, - layerPadding = layerPadding, + layerPaddingProvider = layerPaddingProvider, legend = legend, fadingEdges = fadingEdges, decorations = decorations, @@ -99,7 +100,7 @@ public fun rememberCartesianChart( bottomAxis = bottomAxis, marker = marker, markerVisibilityListener = markerVisibilityListener, - layerPadding = layerPadding, + layerPaddingProvider = layerPaddingProvider, legend = legend, fadingEdges = fadingEdges, decorations = decorations, 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..24467f9a5 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,7 @@ internal fun CartesianChartHostImpl( ranges = ranges, scrollEnabled = scrollState.scrollEnabled, zoomEnabled = scrollState.scrollEnabled && zoomState.zoomEnabled, - layerPadding = chart.layerPadding, + layerPadding = chart.layerPaddingProvider.getPadding(extraStore = 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 62b97eab8..913695dd3 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 @@ -77,7 +77,8 @@ 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 layerPaddingProvider: CartesianLayerPaddingProvider = + CartesianLayerPaddingProvider.fixed(), protected val legend: Legend? = null, protected val fadingEdges: FadingEdges? = null, protected val decorations: List = emptyList(), @@ -387,7 +388,7 @@ public open class CartesianChart( bottomAxis: Axis? = this.bottomAxis, marker: CartesianMarker? = this.marker, markerVisibilityListener: CartesianMarkerVisibilityListener? = this.markerVisibilityListener, - layerPadding: CartesianLayerPadding = this.layerPadding, + layerPaddingProvider: CartesianLayerPaddingProvider = this.layerPaddingProvider, legend: Legend? = this.legend, fadingEdges: FadingEdges? = this.fadingEdges, decorations: List = this.decorations, @@ -402,7 +403,7 @@ public open class CartesianChart( bottomAxis = bottomAxis, marker = marker, markerVisibilityListener = markerVisibilityListener, - layerPadding = layerPadding, + layerPaddingProvider = layerPaddingProvider, legend = legend, fadingEdges = fadingEdges, decorations = decorations, @@ -417,7 +418,7 @@ public open class CartesianChart( id == other.id && marker == other.marker && markerVisibilityListener == other.markerVisibilityListener && - layerPadding == other.layerPadding && + layerPaddingProvider == other.layerPaddingProvider && legend == other.legend && fadingEdges == other.fadingEdges && decorations == other.decorations && @@ -430,7 +431,7 @@ public open class CartesianChart( id, marker, markerVisibilityListener, - layerPadding, + layerPaddingProvider, legend, fadingEdges, decorations, diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt index 49c832486..75426da9a 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt @@ -18,6 +18,40 @@ package com.patrykandpatrick.vico.core.cartesian import androidx.compose.runtime.Immutable import com.patrykandpatrick.vico.core.cartesian.layer.CartesianLayer +import com.patrykandpatrick.vico.core.common.data.ExtraStore + +/** Provides [CartesianLayerPadding] to a [CartesianChart]. */ +@Immutable +public fun interface CartesianLayerPaddingProvider { + /** Returns the [CartesianLayerPadding] for the specified [ExtraStore]. */ + public fun getPadding(extraStore: ExtraStore): CartesianLayerPadding + + public companion object { + /** Constructs a [CartesianLayerPaddingProvider] which always provides the same + * [CartesianLayerPadding] regardless of the stored values in the [ExtraStore]. */ + public fun fixed( + scalableStartDp: Float = 0f, + scalableEndDp: Float = 0f, + unscalableStartDp: Float = 0f, + unscalableEndDp: Float = 0f, + ): CartesianLayerPaddingProvider = object : CartesianLayerPaddingProvider { + val padding = CartesianLayerPadding( + scalableStartDp = scalableStartDp, + scalableEndDp = scalableEndDp, + unscalableStartDp = unscalableStartDp, + unscalableEndDp = unscalableEndDp, + ) + + override fun getPadding(extraStore: ExtraStore): CartesianLayerPadding = padding + } + + /** Constructs a [CartesianLayerPaddingProvider] which always provides the same + * [CartesianLayerPadding] regardless of the stored values in the [ExtraStore]. */ + public fun fixed( + layerPadding: CartesianLayerPadding, + ): CartesianLayerPaddingProvider = CartesianLayerPaddingProvider { layerPadding } + } +} /** * Stores [CartesianLayer] padding values. [scalableStartDp] and [scalableEndDp] are multiplied by 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 ffd2f21a3..0d6354d80 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 @@ -38,6 +38,7 @@ import com.patrykandpatrick.vico.core.cartesian.data.toImmutable import com.patrykandpatrick.vico.core.common.Defaults import com.patrykandpatrick.vico.core.common.NEW_PRODUCER_ERROR_MESSAGE import com.patrykandpatrick.vico.core.common.Point +import com.patrykandpatrick.vico.core.common.data.ExtraStore import com.patrykandpatrick.vico.core.common.spToPx import com.patrykandpatrick.vico.views.R import com.patrykandpatrick.vico.views.common.ChartView @@ -73,7 +74,8 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ranges = CartesianChartRanges.Empty, scrollEnabled = false, zoomEnabled = false, - layerPadding = themeHandler.chart?.layerPadding ?: CartesianLayerPadding(), + layerPadding = themeHandler.chart?.layerPaddingProvider?.getPadding(extraStore = extraStore) + ?: CartesianLayerPadding(), spToPx = context::spToPx, ) @@ -120,7 +122,8 @@ 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 + if (newValue != null) measuringContext.layerPadding = + newValue.layerPaddingProvider.getPadding(extraStore) tryInvalidate(chart = newValue, model = model, updateRanges = true) } @@ -350,3 +353,7 @@ internal val MotionEvent.movedXDistance: Float internal val MotionEvent.movedYDistance: Float get() = if (historySize > 0) abs(y - getHistoricalY(historySize - 1)) else 0f + +private fun CartesianChart.getLayerPadding(extraStore: ExtraStore?) = extraStore?.let { + layerPaddingProvider.getPadding(extraStore = it) +} ?: CartesianLayerPadding() 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 3b08c5243..88d6b6674 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 @@ -24,6 +24,7 @@ import android.util.Log import android.view.animation.AccelerateInterpolator import com.patrykandpatrick.vico.core.cartesian.CartesianChart import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPadding +import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider import com.patrykandpatrick.vico.core.cartesian.FadingEdges import com.patrykandpatrick.vico.core.cartesian.axis.Axis import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis @@ -227,7 +228,9 @@ internal class ThemeHandler(private val context: Context, attrs: AttributeSet?) endAxis = baseTypedArray.getAxis(Axis.Position.Vertical.End), bottomAxis = baseTypedArray.getAxis(Axis.Position.Horizontal.Bottom), fadingEdges = baseTypedArray.getFadingEdges(), - layerPadding = baseTypedArray.getLayerPadding(), + layerPaddingProvider = CartesianLayerPaddingProvider.fixed( + layerPadding = baseTypedArray.getLayerPadding(), + ), ) }