Skip to content

Instantly share code, notes, and snippets.

@Bradleycorn
Last active January 22, 2019 01:39
Show Gist options
  • Save Bradleycorn/695540ce54a134241f5d803fff58bc76 to your computer and use it in GitHub Desktop.
Save Bradleycorn/695540ce54a134241f5d803fff58bc76 to your computer and use it in GitHub Desktop.
MotionLayout - ViewModel
class MainActivityViewModel: ViewModel() {
//...
private fun setOrientation(requestedOrientation: Int = -1) {
val newOrientation = requestedOrientation.takeUnless { it == -1 } ?: currentOrientation
_layoutState.value = when (newOrientation) {
Configuration.ORIENTATION_LANDSCAPE -> VideoLayoutState.FULLSCREEN
else -> {
if (showInPipMode) {
VideoLayoutState.PIP
} else {
VideoLayoutState.EMBEDDED
}
}
}
}
fun onOrientationForced(orientation: Int) {
setOrientation(orientation)
}
fun onVideoToggled() {
val newState = when (_playbackState.value) {
VideoPlaybackState.STOPPED -> VideoPlaybackState.PLAYING
else -> VideoPlaybackState.STOPPED
}
_playbackState.value = newState
}
fun onVideoClosed() {
if (_layoutState.value == VideoLayoutState.FULLSCREEN) {
_layoutState.value = if (showInPipMode) VideoLayoutState.PIP else VideoLayoutState.EMBEDDED
}
_playbackState.value = VideoPlaybackState.STOPPED
}
fun onPipToggled() {
_layoutState.value = when (_layoutState.value) {
VideoLayoutState.PIP -> VideoLayoutState.EMBEDDED
else -> VideoLayoutState.PIP
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment