Skip to content

Commit

Permalink
Fix ChartImpl calling ChartScrollSpec#performAutoScroll too often
Browse files Browse the repository at this point in the history
Co-authored-by: Patryk Goworowski <[email protected]>
  • Loading branch information
patrickmichalik and Gowsky committed Sep 26, 2023
1 parent c98f7da commit 76c6db3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .idea/detekt-config.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
accompanist = "0.32.0"
activity = "1.7.2"
agp = "8.3.0-alpha05"
androidXAnnotation = "1.7.0"
androidXCore = "1.12.0"
appcompat = "1.6.1"
composeBom = "2023.09.01"
Expand All @@ -20,6 +21,7 @@ testCore = "1.5.0"

[libraries]
activityCompose = { group = "androidx.activity", name = "activity-compose", version.ref = "activity" }
androidXAnnotation = { group = "androidx.annotation", name = "annotation", version.ref = "androidXAnnotation" }
androidXCore = { group = "androidx.core", name = "core-ktx", version.ref = "androidXCore" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
buildTools = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ import com.patrykandpatrick.vico.core.marker.Marker
import com.patrykandpatrick.vico.core.marker.MarkerVisibilityChangeListener
import com.patrykandpatrick.vico.core.model.Point
import com.patrykandpatrick.vico.core.scroll.ScrollListener
import com.patrykandpatrick.vico.core.util.ValueWrapper
import com.patrykandpatrick.vico.core.util.getValue
import com.patrykandpatrick.vico.core.util.setValue
import kotlinx.coroutines.launch

/**
Expand Down Expand Up @@ -278,7 +281,7 @@ internal fun <Model : ChartEntryModel> ChartImpl(
val elevationOverlayColor = currentChartStyle.elevationOverlayColor.toArgb()
val (wasMarkerVisible, setWasMarkerVisible) = remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()
var previousModelID: Int? = remember { null }
var previousModelID by remember { ValueWrapper(model.id) }

val onZoom = rememberZoomState(
zoom = zoom,
Expand Down Expand Up @@ -332,10 +335,9 @@ internal fun <Model : ChartEntryModel> ChartImpl(

if (model.id != previousModelID) {
coroutineScope.launch { chartScrollSpec.performAutoScroll(model, oldModel, chartScrollState) }
previousModelID = model.id
}

previousModelID = model.id

chartScrollState.handleInitialScroll(initialScroll = chartScrollSpec.initialScroll)

val chartDrawContext = chartDrawContext(
Expand Down
1 change: 1 addition & 0 deletions vico/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ afterEvaluate {

dependencies {

implementation libs.androidXAnnotation
implementation libs.kotlinStdLib
testImplementation libs.JUnit
testImplementation libs.JUnitExt
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2023 by Patryk Goworowski and Patrick Michalik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.patrykandpatrick.vico.core.util

import androidx.annotation.RestrictTo
import kotlin.reflect.KProperty

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class ValueWrapper<T>(public var value: T)

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public operator fun <T> ValueWrapper<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public operator fun <T> ValueWrapper<T>.setValue(thisObj: Any?, property: KProperty<*>, value: T) {
this.value = value
}

0 comments on commit 76c6db3

Please sign in to comment.