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

Commit ee753d72 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "STL avoid multiple onStop calls" into main

parents 52305288 31706cda
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)
    }
}