in app/src/main/java/com/amazonaws/ivs/player/scrollablefeed/ui/MainActivity.kt [33:106]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.streamCenter.run {
muteButton.setOnClickListener {
viewModel.toggleMute()
}
titleView.share.setOnClickListener {
val stream = viewModel.currentSteam
startShareIntent(title = stream.metadata.streamTitle, url = stream.stream.playbackUrl)
}
titleView.favorite.setOnClickListener {
titleView.heartView.addHeart(HEART_COLORS.random())
}
}
binding.motionLayout.setOnTouchListener { _, event ->
val duration = event.eventTime - event.downTime
val isTransitioning = binding.motionLayout.progress > 0 && binding.motionLayout.progress < 1
if (event.action == MotionEvent.ACTION_UP
&& duration < CLICK_THRESHOLD
&& !isTransitioning
) {
Timber.d("Motion layout clicked")
viewModel.togglePause()
true
} else {
false
}
}
binding.motionLayout.doOnLayout { root ->
updateViewHeight(root.height)
}
launchUI {
viewModel.streams.collect { streams ->
// Lets assume that the ViewModel will always return 3 streams!
val topStream = streams[0]
val centerStream = streams[1]
val bottomStream = streams[2]
binding.itemTop = topStream
binding.itemCenter = centerStream
binding.itemBottom = bottomStream
initStream(binding.streamTop, topStream)
initStream(binding.streamBottom, bottomStream)
initStream(binding.streamCenter, centerStream)
}
}
binding.motionLayout.setTransitionListener(object : TransitionAdapter() {
override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
super.onTransitionCompleted(motionLayout, currentId)
val currentTime = Date().time
val transitionDelay = currentTime - lastTransitionTime
lastTransitionTime = currentTime
// Workaround for a Bug in ConstraintLayout that causes multiple onTransitionCallbacks for no reason
if (transitionDelay < TRANSITION_THRESHOLD) return
Timber.d("Transition completed: $currentId, ${R.id.state_top}, ${R.id.state_center}, ${R.id.state_bottom}")
when (currentId) {
R.id.state_top -> {
viewModel.scrollStreams(ScrollDirection.UP)
resetCenterStream()
motionLayout?.jumpToState(R.id.state_center)
}
R.id.state_bottom -> {
viewModel.scrollStreams(ScrollDirection.DOWN)
resetCenterStream()
motionLayout?.jumpToState(R.id.state_center)
}
}
}
})
}