Add observable progress

This commit is contained in:
Oleksandr Balan
2022-07-24 20:36:16 +02:00
parent faa198f7b0
commit 43e5c076b9
2 changed files with 34 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@@ -29,8 +30,10 @@ import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.toOffset import androidx.compose.ui.unit.toOffset
import eu.wewox.pagecurl.components.SettingsAction import eu.wewox.pagecurl.components.SettingsAction
import eu.wewox.pagecurl.components.SettingsPopup import eu.wewox.pagecurl.components.SettingsPopup
import eu.wewox.pagecurl.config.CurlConfig
import eu.wewox.pagecurl.config.InteractionConfig import eu.wewox.pagecurl.config.InteractionConfig
import eu.wewox.pagecurl.config.PageCurlConfig import eu.wewox.pagecurl.config.PageCurlConfig
import eu.wewox.pagecurl.config.ShadowConfig
import eu.wewox.pagecurl.config.copy import eu.wewox.pagecurl.config.copy
import eu.wewox.pagecurl.page.PageCurl import eu.wewox.pagecurl.page.PageCurl
import eu.wewox.pagecurl.page.PageCurlState import eu.wewox.pagecurl.page.PageCurlState
@@ -63,9 +66,23 @@ class MainActivity : ComponentActivity() {
) )
} }
val curlConfig by derivedStateOf {
val offset = if (state.progress > 0f) 0f else 1f
val shadowAlpha = (offset + state.progress) * 0.5f
CurlConfig(
shadow = ShadowConfig(
alpha = shadowAlpha
)
)
}
PageCurl( PageCurl(
state = state, state = state,
config = PageCurlConfig(interaction = interaction) config = PageCurlConfig(
curl = curlConfig,
interaction = interaction
)
) { index -> ) { index ->
Box( Box(
modifier = Modifier modifier = Modifier

View File

@@ -6,6 +6,7 @@ import androidx.compose.animation.core.TwoWayConverter
import androidx.compose.animation.core.VisibilityThreshold import androidx.compose.animation.core.VisibilityThreshold
import androidx.compose.animation.core.keyframes import androidx.compose.animation.core.keyframes
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver import androidx.compose.runtime.saveable.Saver
@@ -53,6 +54,8 @@ public class PageCurlState(
public var current: Int by mutableStateOf(initialCurrent) public var current: Int by mutableStateOf(initialCurrent)
internal set internal set
public val progress: Float get() = internalState?.progress ?: 0f
internal var internalState: InternalState? by mutableStateOf(null) internal var internalState: InternalState? by mutableStateOf(null)
internal fun setup(constraints: Constraints) { internal fun setup(constraints: Constraints) {
@@ -101,6 +104,16 @@ public class PageCurlState(
var animateJob: Job? = null var animateJob: Job? = null
val progress: Float by derivedStateOf {
if (forward.value != rightEdge) {
1f - forward.value.centerX / constraints.maxWidth
} else if (backward.value != leftEdge) {
-backward.value.centerX / constraints.maxWidth
} else {
0f
}
}
suspend fun reset() { suspend fun reset() {
forward.snapTo(rightEdge) forward.snapTo(rightEdge)
backward.snapTo(leftEdge) backward.snapTo(leftEdge)
@@ -134,6 +147,9 @@ public class PageCurlState(
} }
public data class Edge(val top: Offset, val bottom: Offset) { public data class Edge(val top: Offset, val bottom: Offset) {
internal val centerX: Float = (top.x + bottom.x) * 0.5f
internal companion object { internal companion object {
val VectorConverter: TwoWayConverter<Edge, AnimationVector4D> = val VectorConverter: TwoWayConverter<Edge, AnimationVector4D> =
TwoWayConverter( TwoWayConverter(