-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated ComposableDestinationOwner to use a separate ComposableDestin…
…ationAnimations class for managing animation behaviour, based on SeekableTransitionState
- Loading branch information
Showing
7 changed files
with
133 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...src/main/java/dev/enro/destination/compose/destination/ComposableDestinationAnimations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package dev.enro.destination.compose.destination | ||
|
||
import androidx.compose.animation.EnterExitState | ||
import androidx.compose.animation.EnterTransition | ||
import androidx.compose.animation.ExitTransition | ||
import androidx.compose.animation.core.SeekableTransitionState | ||
import androidx.compose.animation.core.Transition | ||
import androidx.compose.animation.core.rememberTransition | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.animation.fadeOut | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.derivedStateOf | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import dev.enro.animation.NavigationAnimation | ||
import dev.enro.core.compose.destination.ComposableDestinationOwner | ||
import dev.enro.core.compose.dialog.BottomSheetDestination | ||
import dev.enro.core.compose.dialog.DialogDestination | ||
import dev.enro.core.container.getAnimationsForEntering | ||
import dev.enro.core.container.getAnimationsForExiting | ||
|
||
internal sealed class AnimationEvent { | ||
data class AnimateTo(val visible: Boolean) : AnimationEvent() | ||
data class SnapTo(val visible: Boolean) : AnimationEvent() | ||
data class Seek(val progress: Float, val visible: Boolean) : AnimationEvent() | ||
} | ||
|
||
internal class ComposableDestinationAnimations( | ||
private val owner: ComposableDestinationOwner, | ||
) { | ||
private var currentAnimationEvent by mutableStateOf<AnimationEvent>(AnimationEvent.SnapTo(false)) | ||
private val visibilityState = SeekableTransitionState(false) | ||
|
||
internal var animationOverride by mutableStateOf<NavigationAnimation.Composable?>(null) | ||
|
||
internal lateinit var enterExitTransition: Transition<EnterExitState> | ||
|
||
val isAnimating by derivedStateOf { | ||
when(currentAnimationEvent) { | ||
is AnimationEvent.AnimateTo -> visibilityState.targetState != visibilityState.currentState | ||
is AnimationEvent.SnapTo -> false | ||
is AnimationEvent.Seek -> true | ||
} | ||
} | ||
|
||
internal fun setAnimationEvent(event: AnimationEvent) { | ||
currentAnimationEvent = event | ||
} | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
@Composable | ||
fun Animate(content: @Composable () -> Unit) { | ||
val targetState = visibilityState.targetState | ||
val instruction = owner.instruction | ||
val parentContainer = owner.parentContainer | ||
|
||
val animation = remember( | ||
instruction, | ||
targetState, | ||
parentContainer, | ||
animationOverride | ||
) { | ||
animationOverride ?: when (owner.destination) { | ||
is DialogDestination -> NavigationAnimation.Composable(EnterTransition.None, ExitTransition.None) | ||
is BottomSheetDestination -> NavigationAnimation.Composable( | ||
enter = EnterTransition.None, | ||
exit = fadeOut(tween(75, 150)), | ||
) | ||
else -> when { | ||
visibilityState.targetState >= visibilityState.currentState -> parentContainer.getAnimationsForEntering(instruction).asComposable() | ||
else -> parentContainer.getAnimationsForExiting(instruction).asComposable() | ||
} | ||
} | ||
} | ||
|
||
LaunchedEffect(currentAnimationEvent) { | ||
val event = currentAnimationEvent | ||
when(event) { | ||
is AnimationEvent.AnimateTo -> visibilityState.animateTo(event.visible) | ||
is AnimationEvent.SnapTo -> visibilityState.snapTo(event.visible) | ||
is AnimationEvent.Seek -> visibilityState.seekTo(event.progress, event.visible) | ||
} | ||
} | ||
|
||
animation.Animate( | ||
visible = rememberTransition(visibilityState, "ComposableDestination Visibility"), | ||
) { | ||
enterExitTransition = it | ||
content() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.