Skip to content

Instantly share code, notes, and snippets.

@halilozercan
Created August 4, 2020 13:38
Show Gist options
  • Save halilozercan/aafdf2a7eb4defc50de63778be1b02e3 to your computer and use it in GitHub Desktop.
Save halilozercan/aafdf2a7eb4defc50de63778be1b02e3 to your computer and use it in GitHub Desktop.
object MediaControlButtons {
private val HIDDEN = "hidden"
private val VISIBLE = "visible"
private val alpha = FloatPropKey()
private val transitionDef by lazy {
transitionDefinition {
state(HIDDEN) {
this[alpha] = 0f
}
state(VISIBLE) {
this[alpha] = 1f
}
transition(fromState = HIDDEN, toState = VISIBLE) {
alpha using tween(
durationMillis = 250,
easing = LinearEasing
)
}
transition(fromState = VISIBLE, toState = HIDDEN) {
alpha using tween(
durationMillis = 250,
easing = LinearEasing
)
}
}
}
@Composable
operator fun invoke(modifier: Modifier = Modifier) {
val controller = VideoPlayerControllerAmbient.current
val controlsEnabled by controller.controlsEnabled.collectAsState()
// Dictates the direction of appear animation.
// If controlsVisible is true, appear animation needs to be triggered.
val controlsVisible by controller.controlsVisible.collectAsState()
// When controls are not visible anymore we should remove them from UI tree
// Controls by default should always be on screen.
// Only when disappear animation finishes, controls can be freely cleared from the tree.
val (controlsExistOnUITree, setControlsExistOnUITree) = stateFor(controlsVisible) { true }
val appearTransition = transition(
transitionDef,
initState = HIDDEN,
toState = if(controlsVisible) VISIBLE else HIDDEN,
onStateChangeFinished = {
setControlsExistOnUITree(it == VISIBLE)
}
)
if (controlsEnabled && controlsExistOnUITree) {
Content(modifier = Modifier
.drawOpacity(appearTransition[alpha])
.drawBackground(Color.Black.copy(alpha = appearTransition[alpha]*0.6f))
+ modifier)
}
}
@Composable
fun Content(modifier: Modifier = Modifier) {
val controller = VideoPlayerControllerAmbient.current
Stack(modifier = Modifier + modifier) {
Box(modifier = Modifier.gravity(Alignment.Center).fillMaxSize().clickable(indication = null) {
controller.hideControls()
})
PositionAndDurationNumbers(modifier = Modifier.gravity(Alignment.BottomCenter))
PlayPauseButton(modifier = Modifier.gravity(Alignment.Center))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment