Skip to content

Commit

Permalink
ChartEntryModelProducer and ComposedChartEntryModelProducer: Intr…
Browse files Browse the repository at this point in the history
…oduce extras

Co-authored-by: Patryk Goworowski <[email protected]>
  • Loading branch information
patrickmichalik and Gowsky committed Nov 12, 2023
1 parent 62f3a4d commit 32152c8
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import com.patrykandpatrick.vico.core.chart.values.ChartValuesProvider
import com.patrykandpatrick.vico.core.chart.values.toChartValuesProvider
import com.patrykandpatrick.vico.core.entry.ChartEntryModel
import com.patrykandpatrick.vico.core.entry.ChartModelProducer
import com.patrykandpatrick.vico.core.entry.diff.MutableDrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.MutableExtraStore
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -66,7 +66,7 @@ public fun <Model : ChartEntryModel> ChartModelProducer<Model>.collectAsState(
): State<ChartEntryModelWrapper<Model>> {
val chartEntryModelWrapperState = remember(chart, producerKey) { ChartEntryModelWrapperState<Model>() }
val modelTransformerProvider = remember(chart) { chart.modelTransformerProvider }
val drawingModelStore = remember(chart) { MutableDrawingModelStore() }
val extraStore = remember(chart) { MutableExtraStore() }
val scope = rememberCoroutineScope()
val isInPreview = LocalInspectionMode.current
DisposableEffect(chart, producerKey, runInitialAnimation, isInPreview) {
Expand Down Expand Up @@ -127,7 +127,7 @@ public fun <Model : ChartEntryModel> ChartModelProducer<Model>.collectAsState(
startAnimation = startAnimation,
getOldModel = { chartEntryModelWrapperState.value.chartEntryModel },
modelTransformerProvider = modelTransformerProvider,
drawingModelStore = drawingModelStore,
extraStore = extraStore,
updateChartValues = { model ->
chartValuesManager.resetChartValues()
if (model != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import com.patrykandpatrick.vico.core.context.MeasureContext
import com.patrykandpatrick.vico.core.dimensions.BoundsAware
import com.patrykandpatrick.vico.core.entry.ChartEntryModel
import com.patrykandpatrick.vico.core.entry.diff.DrawingModel
import com.patrykandpatrick.vico.core.entry.diff.DrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.MutableDrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.ExtraStore
import com.patrykandpatrick.vico.core.entry.diff.MutableExtraStore
import com.patrykandpatrick.vico.core.marker.Marker

internal const val AXIS_VALUES_DEPRECATION_MESSAGE: String = "Axis values should be overridden via " +
Expand Down Expand Up @@ -219,24 +219,24 @@ public interface Chart<in Model> : BoundsAware, ChartInsetter {
*/
public abstract class ModelTransformer<in Model> {
/**
* Used for writing to and reading from the host’s [DrawingModelStore].
* Used for writing to and reading from the host’s [ExtraStore].
*/
protected abstract val key: DrawingModelStore.Key<*>
protected abstract val key: ExtraStore.Key<*>

/**
* Prepares the [Chart] for a difference animation.
*/
public abstract fun prepareForTransformation(
oldModel: Model?,
newModel: Model?,
drawingModelStore: MutableDrawingModelStore,
extraStore: MutableExtraStore,
chartValuesProvider: ChartValuesProvider,
)

/**
* Carries out the pending difference animation. [fraction] is the animation progress.
*/
public abstract suspend fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float)
public abstract suspend fun transform(extraStore: MutableExtraStore, fraction: Float)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import com.patrykandpatrick.vico.core.entry.ChartEntry
import com.patrykandpatrick.vico.core.entry.ChartEntryModel
import com.patrykandpatrick.vico.core.entry.diff.DefaultDrawingModelInterpolator
import com.patrykandpatrick.vico.core.entry.diff.DrawingModelInterpolator
import com.patrykandpatrick.vico.core.entry.diff.DrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.MutableDrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.ExtraStore
import com.patrykandpatrick.vico.core.entry.diff.MutableExtraStore
import com.patrykandpatrick.vico.core.extension.doubled
import com.patrykandpatrick.vico.core.extension.getRepeating
import com.patrykandpatrick.vico.core.extension.getStart
Expand Down Expand Up @@ -115,7 +115,7 @@ public open class ColumnChart(
*/
protected val horizontalDimensions: MutableHorizontalDimensions = MutableHorizontalDimensions()

protected val drawingModelKey: DrawingModelStore.Key<ColumnChartDrawingModel> = DrawingModelStore.Key()
protected val drawingModelKey: ExtraStore.Key<ColumnChartDrawingModel> = ExtraStore.Key()

override val entryLocationMap: HashMap<Float, MutableList<Marker.EntryModel>> = HashMap()

Expand All @@ -127,7 +127,7 @@ public open class ColumnChart(
drawChartInternal(
chartValues = chartValuesProvider.getChartValues(axisPosition = targetVerticalAxisPosition),
model = model,
drawingModel = model.drawingModelStore.getOrNull(drawingModelKey),
drawingModel = model.extraStore.getOrNull(drawingModelKey),
)
heightMap.clear()
}
Expand Down Expand Up @@ -451,7 +451,7 @@ public open class ColumnChart(
}

protected class ColumnChartModelTransformer(
override val key: DrawingModelStore.Key<ColumnChartDrawingModel>,
override val key: ExtraStore.Key<ColumnChartDrawingModel>,
private val getTargetVerticalAxisPosition: () -> AxisPosition.Vertical?,
private val getDrawingModelInterpolator: () -> DrawingModelInterpolator<
ColumnChartDrawingModel.ColumnInfo,
Expand All @@ -462,20 +462,20 @@ public open class ColumnChart(
override fun prepareForTransformation(
oldModel: ChartEntryModel?,
newModel: ChartEntryModel?,
drawingModelStore: MutableDrawingModelStore,
extraStore: MutableExtraStore,
chartValuesProvider: ChartValuesProvider,
) {
getDrawingModelInterpolator().setModels(
drawingModelStore.getOrNull(key),
extraStore.getOrNull(key),
newModel?.toDrawingModel(chartValuesProvider.getChartValues(getTargetVerticalAxisPosition())),
)
}

override suspend fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float) {
override suspend fun transform(extraStore: MutableExtraStore, fraction: Float) {
getDrawingModelInterpolator()
.transform(fraction)
?.let { drawingModelStore[key] = it }
?: drawingModelStore.remove(key)
?.let { extraStore[key] = it }
?: extraStore.remove(key)
}

private fun ChartEntryModel.toDrawingModel(chartValues: ChartValues): ColumnChartDrawingModel = entries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import com.patrykandpatrick.vico.core.chart.values.ChartValuesManager
import com.patrykandpatrick.vico.core.chart.values.ChartValuesProvider
import com.patrykandpatrick.vico.core.context.MeasureContext
import com.patrykandpatrick.vico.core.entry.ChartEntryModel
import com.patrykandpatrick.vico.core.entry.diff.DrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.MutableDrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.ExtraStore
import com.patrykandpatrick.vico.core.entry.diff.MutableExtraStore
import com.patrykandpatrick.vico.core.extension.set
import com.patrykandpatrick.vico.core.extension.updateAll
import com.patrykandpatrick.vico.core.marker.Marker
Expand Down Expand Up @@ -160,28 +160,28 @@ public class ComposedChart<Model : ChartEntryModel>(
private val getModelTransformers: () -> List<Chart.ModelTransformer<T>>,
) : Chart.ModelTransformer<T>() {

override val key: DrawingModelStore.Key<Nothing> = DrawingModelStore.Key()
override val key: ExtraStore.Key<Nothing> = ExtraStore.Key()

override fun prepareForTransformation(
oldModel: T?,
newModel: T?,
drawingModelStore: MutableDrawingModelStore,
extraStore: MutableExtraStore,
chartValuesProvider: ChartValuesProvider,
) {
getModelTransformers().forEachIndexed { index, transformer ->
@Suppress("UNCHECKED_CAST")
transformer.prepareForTransformation(
(oldModel as ComposedChartEntryModel<*>?)?.composedEntryCollections?.getOrNull(index) as T?,
(newModel as ComposedChartEntryModel<*>).composedEntryCollections[index] as T,
drawingModelStore,
extraStore,
chartValuesProvider,
)
}
}

override suspend fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float) {
override suspend fun transform(extraStore: MutableExtraStore, fraction: Float) {
getModelTransformers().forEach { transformer ->
transformer.transform(drawingModelStore, fraction)
transformer.transform(extraStore, fraction)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ import com.patrykandpatrick.vico.core.entry.ChartEntry
import com.patrykandpatrick.vico.core.entry.ChartEntryModel
import com.patrykandpatrick.vico.core.entry.diff.DefaultDrawingModelInterpolator
import com.patrykandpatrick.vico.core.entry.diff.DrawingModelInterpolator
import com.patrykandpatrick.vico.core.entry.diff.DrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.MutableDrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.ExtraStore
import com.patrykandpatrick.vico.core.entry.diff.MutableExtraStore
import com.patrykandpatrick.vico.core.extension.doubled
import com.patrykandpatrick.vico.core.extension.getRepeating
import com.patrykandpatrick.vico.core.extension.getStart
Expand Down Expand Up @@ -279,7 +279,7 @@ public open class LineChart(
*/
protected val lineBackgroundPath: Path = Path()

protected val drawingModelKey: DrawingModelStore.Key<LineChartDrawingModel> = DrawingModelStore.Key()
protected val drawingModelKey: ExtraStore.Key<LineChartDrawingModel> = ExtraStore.Key()

override val entryLocationMap: HashMap<Float, MutableList<Marker.EntryModel>> = HashMap()

Expand All @@ -289,7 +289,7 @@ public open class LineChart(
): Unit = with(context) {
resetTempData()

val drawingModel = model.drawingModelStore.getOrNull(drawingModelKey)
val drawingModel = model.extraStore.getOrNull(drawingModelKey)

model.entries.forEachIndexed { entryListIndex, entries ->

Expand Down Expand Up @@ -601,7 +601,7 @@ public open class LineChart(
}

protected class LineChartModelTransformer(
override val key: DrawingModelStore.Key<LineChartDrawingModel>,
override val key: ExtraStore.Key<LineChartDrawingModel>,
private val getTargetVerticalAxisPosition: () -> AxisPosition.Vertical?,
private val getDrawingModelInterpolator: () -> DrawingModelInterpolator<
LineChartDrawingModel.PointInfo,
Expand All @@ -612,20 +612,20 @@ public open class LineChart(
override fun prepareForTransformation(
oldModel: ChartEntryModel?,
newModel: ChartEntryModel?,
drawingModelStore: MutableDrawingModelStore,
extraStore: MutableExtraStore,
chartValuesProvider: ChartValuesProvider,
) {
getDrawingModelInterpolator().setModels(
drawingModelStore.getOrNull(key),
extraStore.getOrNull(key),
newModel?.toDrawingModel(chartValuesProvider.getChartValues(getTargetVerticalAxisPosition())),
)
}

override suspend fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float) {
override suspend fun transform(extraStore: MutableExtraStore, fraction: Float) {
getDrawingModelInterpolator()
.transform(fraction)
?.let { drawingModelStore[key] = it }
?: drawingModelStore.remove(key)
?.let { extraStore[key] = it }
?: extraStore.remove(key)
}

private fun ChartEntryModel.toDrawingModel(chartValues: ChartValues): LineChartDrawingModel = entries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider
import com.patrykandpatrick.vico.core.chart.values.ChartValues
import com.patrykandpatrick.vico.core.entry.composed.ComposedChartEntryModelProducer
import com.patrykandpatrick.vico.core.entry.diff.DrawingModel
import com.patrykandpatrick.vico.core.entry.diff.DrawingModelStore
import com.patrykandpatrick.vico.core.entry.diff.ExtraStore

/**
* Contains the data for a [Chart]. Pre-calculates values needed for the rendering of the [Chart].
Expand Down Expand Up @@ -92,10 +92,10 @@ public interface ChartEntryModel {
public val xGcd: Float

/**
* Houses [DrawingModel]s.
* Houses auxiliary data, including [DrawingModel]s.
*/
public val drawingModelStore: DrawingModelStore
get() = DrawingModelStore.empty
public val extraStore: ExtraStore
get() = ExtraStore.empty

/**
* Returns an immutable copy of this [ChartEntryModel].
Expand Down
Loading

0 comments on commit 32152c8

Please sign in to comment.