Skip to content

Commit

Permalink
Disallow for changing modelProducer after setting it once
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick Michalik <[email protected]>
  • Loading branch information
Gowsky and patrickmichalik committed Apr 10, 2024
1 parent b664681 commit 996d2b9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public fun CartesianChartHost(
) {
val mutableChartValues = remember(chart) { MutableChartValues() }
val modelWrapper by modelProducer
.collectAsState(chart, modelProducer, diffAnimationSpec, runInitialAnimation, mutableChartValues, getXStep)
.collectAsState(chart, diffAnimationSpec, runInitialAnimation, mutableChartValues, getXStep)
val (model, previousModel, chartValues) = modelWrapper

CartesianChartHostBox(modifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import com.patrykandpatrick.vico.core.cartesian.model.CartesianChartModelProduce
import com.patrykandpatrick.vico.core.cartesian.toImmutable
import com.patrykandpatrick.vico.core.common.Animation
import com.patrykandpatrick.vico.core.common.MutableExtraStore
import com.patrykandpatrick.vico.core.common.NEW_PRODUCER_ERROR_MESSAGE
import com.patrykandpatrick.vico.core.common.ValueWrapper
import com.patrykandpatrick.vico.core.common.getValue
import com.patrykandpatrick.vico.core.common.setValue
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand All @@ -45,18 +49,21 @@ internal val defaultCartesianDiffAnimationSpec: AnimationSpec<Float> = tween(dur
@Composable
internal fun CartesianChartModelProducer.collectAsState(
chart: CartesianChart,
producerKey: Any,
animationSpec: AnimationSpec<Float>? = defaultCartesianDiffAnimationSpec,
runInitialAnimation: Boolean = true,
mutableChartValues: MutableChartValues,
getXStep: ((CartesianChartModel) -> Float)?,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
): State<CartesianChartModelWrapper> {
val modelWrapperState = remember(chart, producerKey) { CartesianChartModelWrapperState() }
var previousHashCode by remember { ValueWrapper<Int?>(null) }
val hashCode = hashCode()
check(previousHashCode == null || hashCode == previousHashCode) { NEW_PRODUCER_ERROR_MESSAGE }
previousHashCode = hashCode
val modelWrapperState = remember(chart) { CartesianChartModelWrapperState() }
val extraStore = remember(chart) { MutableExtraStore() }
val scope = rememberCoroutineScope()
val isInPreview = LocalInspectionMode.current
DisposableEffect(chart, producerKey, runInitialAnimation, isInPreview) {
DisposableEffect(chart, runInitialAnimation, isInPreview) {
var mainAnimationJob: Job? = null
var animationFrameJob: Job? = null
var finalAnimationFrameJob: Job? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ internal const val FULL_DEGREES: Float = 360f
internal const val ERR_REPEATING_COLLECTION_EMPTY = "Cannot get repeated item from empty collection."

internal const val ELLIPSIS: String = ""

public const val NEW_PRODUCER_ERROR_MESSAGE: String =
"A new `CartesianChartModelProducer` was provided. Run data " +
"updates via `tryRunTransaction` or `runTransaction`, not by creating new `CartesianChartModelProducer`s."
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.patrykandpatrick.vico.core.cartesian.model.CartesianChartModel
import com.patrykandpatrick.vico.core.cartesian.model.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.cartesian.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.extension.spToPx
import com.patrykandpatrick.vico.views.R
Expand Down Expand Up @@ -152,7 +153,7 @@ public open class CartesianChartView
public var modelProducer: CartesianChartModelProducer? = null
set(value) {
if (field === value) return
field?.unregisterFromUpdates(key = this)
check(field == null) { NEW_PRODUCER_ERROR_MESSAGE }
field = value
if (isAttachedToWindowCompat) registerForUpdates()
}
Expand Down

0 comments on commit 996d2b9

Please sign in to comment.