From 76c6db31373e137dc9bf8b59c341af6df5a86e1c Mon Sep 17 00:00:00 2001 From: Patrick Michalik <120058021+patrickmichalik@users.noreply.github.com> Date: Tue, 26 Sep 2023 11:38:29 +0200 Subject: [PATCH] Fix `ChartImpl` calling `ChartScrollSpec#performAutoScroll` too often Co-authored-by: Patryk Goworowski --- .idea/detekt-config.yml | 6 ++++ gradle/libs.versions.toml | 2 ++ .../vico/compose/chart/Charts.kt | 8 +++-- vico/core/build.gradle | 1 + .../vico/core/util/ValueWrapper.kt | 31 +++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/util/ValueWrapper.kt diff --git a/.idea/detekt-config.yml b/.idea/detekt-config.yml index 2553969aa..36efa3f3a 100644 --- a/.idea/detekt-config.yml +++ b/.idea/detekt-config.yml @@ -10,6 +10,8 @@ comments: - "**/jsTest/**" - "**/iosTest/**" - "**/com/patrykandpatrick/vico/sample/**" + ignoreAnnotated: + - "RestrictTo" UndocumentedPublicFunction: active: true excludes: @@ -20,6 +22,8 @@ comments: - "**/jsTest/**" - "**/iosTest/**" - "**/com/patrykandpatrick/vico/sample/**" + ignoreAnnotated: + - "RestrictTo" UndocumentedPublicProperty: active: true excludes: @@ -30,6 +34,8 @@ comments: - "**/jsTest/**" - "**/iosTest/**" - "**/com/patrykandpatrick/vico/sample/**" + ignoreAnnotated: + - "RestrictTo" complexity: LongMethod: threshold: 90 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d7654c0dd..7c6900e39 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" @@ -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" } diff --git a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/Charts.kt b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/Charts.kt index eaab7714b..a4a66ae90 100644 --- a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/Charts.kt +++ b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/Charts.kt @@ -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 /** @@ -278,7 +281,7 @@ internal fun 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, @@ -332,10 +335,9 @@ internal fun 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( diff --git a/vico/core/build.gradle b/vico/core/build.gradle index d44f20257..ec3f2feb0 100644 --- a/vico/core/build.gradle +++ b/vico/core/build.gradle @@ -60,6 +60,7 @@ afterEvaluate { dependencies { + implementation libs.androidXAnnotation implementation libs.kotlinStdLib testImplementation libs.JUnit testImplementation libs.JUnitExt diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/util/ValueWrapper.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/util/ValueWrapper.kt new file mode 100644 index 000000000..8d4122e5a --- /dev/null +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/util/ValueWrapper.kt @@ -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(public var value: T) + +@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) +public operator fun ValueWrapper.getValue(thisObj: Any?, property: KProperty<*>): T = value + +@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) +public operator fun ValueWrapper.setValue(thisObj: Any?, property: KProperty<*>, value: T) { + this.value = value +}