Skip to content

Commit

Permalink
Resolve caching and concurrency issues affecting `ChartEntryModelProd…
Browse files Browse the repository at this point in the history
…ucer` and `ComposedChartEntryModelProducer` (WIP)

Co-authored-by: Patryk Goworowski <[email protected]>
  • Loading branch information
patrickmichalik and Gowsky committed Sep 30, 2023
1 parent 4988676 commit 85d706e
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import com.patrykandpatrick.vico.core.chart.values.ChartValuesManager
import com.patrykandpatrick.vico.core.entry.ChartEntryModel
import com.patrykandpatrick.vico.core.entry.ChartModelProducer
import com.patrykandpatrick.vico.core.entry.diff.MutableDrawingModelStore
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.launch
Expand All @@ -57,6 +59,7 @@ public fun <Model : ChartEntryModel> ChartModelProducer<Model>.collectAsState(
runInitialAnimation: Boolean = true,
chartValuesManager: ChartValuesManager,
getXStep: ((Model) -> Float)?,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
): State<Pair<Model?, Model?>> {
val chartEntryModelState = remember(chart, producerKey) { ChartEntryModelState<Model>() }

Expand All @@ -65,52 +68,70 @@ public fun <Model : ChartEntryModel> ChartModelProducer<Model>.collectAsState(

val scope = rememberCoroutineScope()
val isInPreview = LocalInspectionMode.current
var animationJob: Job? = null
var isAnimationRunning: Boolean?
var outerAnimationJob: Job? = null
var innerAnimationJob: Job? = null
var finalAnimationFrameJob: Job? = null
var isAnimationRunning: Boolean
var isAnimationFrameGenerationRunning = false
DisposableEffect(chart, producerKey, runInitialAnimation, isInPreview) {
val afterUpdate = { progressModel: (chartKey: Any, progress: Float) -> Unit ->
val afterUpdate: (progressModel: suspend (chartKey: Any, progress: Float) -> Unit) -> Unit = { progressModel ->
if (animationSpec != null && !isInPreview &&
(chartEntryModelState.value.first != null || runInitialAnimation)
) {
isAnimationRunning = false
animationJob = scope.launch {
isAnimationRunning = true
outerAnimationJob = scope.launch(dispatcher) {
animate(
initialValue = Animation.range.start,
targetValue = Animation.range.endInclusive,
animationSpec = animationSpec,
) { value, _ ->
if (isAnimationRunning == false) {
progressModel(chart, value)
when {
!isAnimationRunning -> return@animate
!isAnimationFrameGenerationRunning -> {
isAnimationFrameGenerationRunning = true
innerAnimationJob = scope.launch(dispatcher) {
progressModel(chart, value)
isAnimationFrameGenerationRunning = false
}
}
value == 1f -> {
finalAnimationFrameJob = scope.launch(dispatcher) {
innerAnimationJob?.cancelAndJoin()
progressModel(chart, value)
isAnimationFrameGenerationRunning = false
}
}
}
}
}
} else {
progressModel(chart, Animation.range.endInclusive)
scope.launch(dispatcher) { progressModel(chart, Animation.range.endInclusive) }
}
}
registerForUpdates(
key = chart,
cancelAnimation = {
runBlocking { animationJob?.cancelAndJoin() }
isAnimationRunning = true
},
startAnimation = afterUpdate,
getOldModel = { chartEntryModelState.value.first },
modelTransformerProvider = modelTransformerProvider,
drawingModelStore = drawingModelStore,
updateChartValues = { model ->
chartValuesManager.resetChartValues()
chart.updateChartValues(chartValuesManager, model, getXStep?.invoke(model))
chartValuesManager
},
) { updatedModel ->
chartEntryModelState.set(updatedModel)
}
onDispose {
unregisterFromUpdates(chart)
animationJob = null
isAnimationRunning = null
scope.launch(dispatcher) {
registerForUpdates(
key = chart,
cancelAnimation = {
runBlocking {
outerAnimationJob?.cancelAndJoin()
innerAnimationJob?.cancelAndJoin()
finalAnimationFrameJob?.cancelAndJoin()
}
isAnimationRunning = false
},
startAnimation = afterUpdate,
getOldModel = { chartEntryModelState.value.first },
modelTransformerProvider = modelTransformerProvider,
drawingModelStore = drawingModelStore,
updateChartValues = { model ->
chartValuesManager.resetChartValues()
chart.updateChartValues(chartValuesManager, model, getXStep?.invoke(model))
chartValuesManager
},
onModelCreated = chartEntryModelState::set,
)
}
onDispose { unregisterFromUpdates(chart) }
}
return chartEntryModelState
}
1 change: 1 addition & 0 deletions vico/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ afterEvaluate {
dependencies {

implementation libs.androidXAnnotation
implementation libs.coroutinesCore
implementation libs.kotlinStdLib
testImplementation libs.JUnit
testImplementation libs.JUnitExt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public interface Chart<in Model> : BoundsAware, ChartInsetter {
/**
* Carries out the pending difference animation. [fraction] is the animation progress.
*/
public abstract fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float)
public abstract suspend fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public open class ColumnChart(
)
}

override fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float) {
override suspend fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float) {
drawingModelStore[key] = drawingModelInterpolator.transform(fraction)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public class ComposedChart<Model : ChartEntryModel>(
}
}

override fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float) {
override suspend fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float) {
getModelTransformers().forEach { transformer ->
transformer.transform(drawingModelStore, fraction)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ public open class LineChart(
)
}

override fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float) {
override suspend fun transform(drawingModelStore: MutableDrawingModelStore, fraction: Float) {
drawingModelStore[key] = drawingModelInterpolator.transform(fraction)
}

Expand Down
Loading

0 comments on commit 85d706e

Please sign in to comment.