All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.peanuuutz.fork.ui.foundation.input.ScrollState.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 The Android Open Source Project
 * Modifications Copyright 2022 Peanuuutz
 *
 * 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 net.peanuuutz.fork.ui.foundation.input

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import net.peanuuutz.fork.ui.animation.animate
import net.peanuuutz.fork.ui.animation.spec.target.DefaultFloatAnimationSpec
import net.peanuuutz.fork.ui.animation.spec.target.composite.FiniteAnimationSpec
import net.peanuuutz.fork.ui.util.MutationPriority
import net.peanuuutz.fork.ui.util.MutatorMutex

@Stable
interface ScrollState {
    val isScrolling: Boolean

    suspend fun scroll(
        priority: MutationPriority = MutationPriority.Default,
        scope: suspend ScrollScope.() -> Unit
    )

    fun scrollBy(amount: Float): Float
}

fun ScrollState(
    onScroll: (amount: Float) -> Float
): ScrollState {
    return DefaultScrollState(onScroll)
}

@Composable
fun rememberScrollState(
    onScroll: (amount: Float) -> Float
): ScrollState {
    val onScrollRemembered by rememberUpdatedState(onScroll)
    return remember { ScrollState { onScrollRemembered(it) } }
}

interface ScrollScope {
    fun scrollBy(amount: Float): Float
}

// -------- Extensions --------

suspend fun ScrollState.interrupt(
    priority: MutationPriority = MutationPriority.Default
) {
    scroll(priority) {}
}

suspend fun ScrollState.animateScrollBy(
    amount: Float,
    animationSpec: FiniteAnimationSpec = DefaultFloatAnimationSpec,
    priority: MutationPriority = MutationPriority.Default
): Float {
    var previousValue = 0.0f
    scroll(priority) {
        animate(
            initialValue = 0.0f,
            targetValue = amount,
            animationSpec = animationSpec
        ) { value, _ ->
            previousValue += scrollBy(value - previousValue)
        }
    }
    return previousValue
}

// ======== Internal ========

private class DefaultScrollState(private val onScroll: (Float) -> Float) : ScrollState {
    private val scrollScope: ScrollScope = object : ScrollScope {
        override fun scrollBy(amount: Float): Float {
            return onScroll(amount)
        }
    }

    private val mutatorMutex: MutatorMutex = MutatorMutex()

    override var isScrolling: Boolean by mutableStateOf(false)

    override suspend fun scroll(priority: MutationPriority, scope: suspend ScrollScope.() -> Unit) {
        isScrolling = true
        try {
            mutatorMutex.mutate(
                receiver = scrollScope,
                priority = priority,
                block = scope
            )
        } finally {
            isScrolling = false
        }
    }

    override fun scrollBy(amount: Float): Float {
        return onScroll(amount)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy