Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 31706cda authored by omarmt's avatar omarmt
Browse files

STL avoid multiple onStop calls

Due to NestedScrollDispatcher, the order of method calls (pre/post
scroll/fling) cannot be guaranteed and they could be called multiple
times.

Test: atest PriorityNestedScrollConnectionTest
Bug: 377666808
Flag: com.android.systemui.scene_container
Change-Id: Ic8334e37b90f99d583c9bb79a13d636661810b2a
parent 538a83b3
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -207,6 +207,9 @@ class PriorityNestedScrollConnection(
    }

    override suspend fun onPreFling(available: Velocity): Velocity {
        // Note: This method may be called multiple times. Due to NestedScrollDispatcher, the order
        // of method calls (pre/post scroll/fling) cannot be guaranteed.
        if (isStopping) return Velocity.Zero
        val controller = currentController ?: return Velocity.Zero

        // If in priority mode and can stop on pre-fling phase, stop the scroll.
@@ -219,6 +222,9 @@ class PriorityNestedScrollConnection(
    }

    override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
        // Note: This method may be called multiple times. Due to NestedScrollDispatcher, the order
        // of method calls (pre/post scroll/fling) cannot be guaranteed.
        if (isStopping) return Velocity.Zero
        val availableFloat = available.toFloat()
        val controller = currentController

@@ -315,6 +321,7 @@ class PriorityNestedScrollConnection(
     * @return The consumed velocity.
     */
    suspend fun stop(velocity: Float): Velocity {
        if (isStopping) return Velocity.Zero
        val controller = requireController(isStopping = false)
        return coroutineScope {
            try {
+14 −0
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.compose.test.runMonotonicClockTest
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
@@ -262,4 +264,16 @@ class PriorityNestedScrollConnectionTest {
        scrollConnection.onPostFling(consumed = Velocity.Zero, available = Velocity.Zero)
        assertThat(isStarted).isEqualTo(true)
    }

    @Test
    fun handleMultipleOnPreFlingCalls() = runTest {
        startPriorityModePostScroll()

        coroutineScope {
            launch { scrollConnection.onPreFling(available = Velocity.Zero) }
            launch { scrollConnection.onPreFling(available = Velocity.Zero) }
        }

        assertThat(lastStop).isEqualTo(0f)
    }
}