viewbind playerfragment

This commit is contained in:
Bnyro 2022-06-30 21:25:40 +02:00
parent 6806f4fe6e
commit 0498e52f5d
3 changed files with 54 additions and 78 deletions

View File

@ -22,14 +22,10 @@ import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.ImageButton import android.widget.ImageButton
import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.ScrollView
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.constraintlayout.motion.widget.MotionLayout import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.view.isVisible import androidx.core.view.isVisible
@ -37,7 +33,6 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.MainActivity import com.github.libretube.MainActivity
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.adapters.ChaptersAdapter import com.github.libretube.adapters.ChaptersAdapter
@ -117,14 +112,11 @@ class PlayerFragment : Fragment() {
private var isSubscribed: Boolean = false private var isSubscribed: Boolean = false
private lateinit var relatedRecView: RecyclerView
private lateinit var commentsRecView: RecyclerView
private var commentsAdapter: CommentsAdapter? = null private var commentsAdapter: CommentsAdapter? = null
private var commentsLoaded: Boolean? = false private var commentsLoaded: Boolean? = false
private var nextPage: String? = null private var nextPage: String? = null
private var isLoading = true private var isLoading = true
private lateinit var exoPlayerView: StyledPlayerView private lateinit var exoPlayerView: StyledPlayerView
private lateinit var motionLayout: MotionLayout
private lateinit var exoPlayer: ExoPlayer private lateinit var exoPlayer: ExoPlayer
private lateinit var segmentData: Segments private lateinit var segmentData: Segments
private var relatedStreamsEnabled = true private var relatedStreamsEnabled = true
@ -136,8 +128,6 @@ class PlayerFragment : Fragment() {
private var isPlayerLocked: Boolean = false private var isPlayerLocked: Boolean = false
private lateinit var relDownloadVideo: LinearLayout
private lateinit var mediaSession: MediaSessionCompat private lateinit var mediaSession: MediaSessionCompat
private lateinit var mediaSessionConnector: MediaSessionConnector private lateinit var mediaSessionConnector: MediaSessionConnector
private lateinit var playerNotification: PlayerNotificationManager private lateinit var playerNotification: PlayerNotificationManager
@ -159,7 +149,7 @@ class PlayerFragment : Fragment() {
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View {
binding = FragmentPlayerBinding.inflate(layoutInflater, container, false) binding = FragmentPlayerBinding.inflate(layoutInflater, container, false)
// Inflate the layout for this fragment // Inflate the layout for this fragment
return binding.root return binding.root
@ -181,11 +171,9 @@ class PlayerFragment : Fragment() {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.binding.container.visibility = View.VISIBLE mainActivity.binding.container.visibility = View.VISIBLE
motionLayout = binding.playerMotionLayout
exoPlayerView = binding.player exoPlayerView = binding.player
view.findViewById<TextView>(R.id.player_description).text = videoId binding.playerMotionLayout.addTransitionListener(object : MotionLayout.TransitionListener {
motionLayout.addTransitionListener(object : MotionLayout.TransitionListener {
override fun onTransitionStarted( override fun onTransitionStarted(
motionLayout: MotionLayout?, motionLayout: MotionLayout?,
startId: Int, startId: Int,
@ -225,7 +213,7 @@ class PlayerFragment : Fragment() {
} }
override fun onTransitionTrigger( override fun onTransitionTrigger(
motionLayout: MotionLayout?, MotionLayout: MotionLayout?,
triggerId: Int, triggerId: Int,
positive: Boolean, positive: Boolean,
progress: Float progress: Float
@ -233,12 +221,12 @@ class PlayerFragment : Fragment() {
} }
}) })
motionLayout.progress = 1.toFloat() binding.playerMotionLayout.progress = 1.toFloat()
motionLayout.transitionToStart() binding.playerMotionLayout.transitionToStart()
binding.closeImageView.setOnClickListener { binding.closeImageView.setOnClickListener {
isMiniPlayerVisible = false isMiniPlayerVisible = false
motionLayout.transitionToEnd() binding.playerMotionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
mainActivity.supportFragmentManager.beginTransaction() mainActivity.supportFragmentManager.beginTransaction()
@ -247,7 +235,7 @@ class PlayerFragment : Fragment() {
} }
view.findViewById<ImageButton>(R.id.close_imageButton).setOnClickListener { view.findViewById<ImageButton>(R.id.close_imageButton).setOnClickListener {
isMiniPlayerVisible = false isMiniPlayerVisible = false
motionLayout.transitionToEnd() binding.playerMotionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
mainActivity.supportFragmentManager.beginTransaction() mainActivity.supportFragmentManager.beginTransaction()
@ -285,7 +273,7 @@ class PlayerFragment : Fragment() {
fullScreenButton.setOnClickListener { fullScreenButton.setOnClickListener {
exoPlayerView.hideController() exoPlayerView.hideController()
if (!isFullScreen) { if (!isFullScreen) {
with(motionLayout) { with(binding.playerMotionLayout) {
getConstraintSet(R.id.start).constrainHeight(R.id.player, -1) getConstraintSet(R.id.start).constrainHeight(R.id.player, -1)
enableTransition(R.id.yt_transition, false) enableTransition(R.id.yt_transition, false)
} }
@ -298,7 +286,7 @@ class PlayerFragment : Fragment() {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
} else { } else {
with(motionLayout) { with(binding.playerMotionLayout) {
getConstraintSet(R.id.start).constrainHeight(R.id.player, 0) getConstraintSet(R.id.start).constrainHeight(R.id.player, 0)
enableTransition(R.id.yt_transition, true) enableTransition(R.id.yt_transition, true)
} }
@ -342,32 +330,28 @@ class PlayerFragment : Fragment() {
isPlayerLocked = !isPlayerLocked isPlayerLocked = !isPlayerLocked
} }
val scrollView = view.findViewById<ScrollView>(R.id.player_scrollView) binding.playerScrollView.viewTreeObserver
scrollView.viewTreeObserver
.addOnScrollChangedListener { .addOnScrollChangedListener {
if (scrollView.getChildAt(0).bottom if (binding.playerScrollView.getChildAt(0).bottom
== (scrollView.height + scrollView.scrollY) && == (binding.playerScrollView.height + binding.playerScrollView.scrollY) &&
nextPage != null nextPage != null
) { ) {
fetchNextComments() fetchNextComments()
} }
} }
commentsRecView = view.findViewById(R.id.comments_recView) binding.commentsRecView.layoutManager = LinearLayoutManager(view.context)
commentsRecView.layoutManager = LinearLayoutManager(view.context) binding.commentsRecView.setItemViewCacheSize(20)
commentsRecView.setItemViewCacheSize(20) binding.relatedRecView.layoutManager =
relatedRecView = view.findViewById(R.id.player_recView)
relatedRecView.layoutManager =
GridLayoutManager(view.context, resources.getInteger(R.integer.grid_items)) GridLayoutManager(view.context, resources.getInteger(R.integer.grid_items))
} }
private fun toggleComments() { private fun toggleComments() {
commentsRecView.visibility = binding.commentsRecView.visibility =
if (commentsRecView.isVisible) View.GONE else View.VISIBLE if (binding.commentsRecView.isVisible) View.GONE else View.VISIBLE
relatedRecView.visibility = binding.relatedRecView.visibility =
if (relatedRecView.isVisible) View.GONE else View.VISIBLE if (binding.relatedRecView.isVisible) View.GONE else View.VISIBLE
if (!commentsLoaded!!) fetchComments() if (!commentsLoaded!!) fetchComments()
} }
@ -637,18 +621,17 @@ class PlayerFragment : Fragment() {
} }
private fun initializePlayerView(view: View, response: Streams) { private fun initializePlayerView(view: View, response: Streams) {
view.findViewById<TextView>(R.id.player_views_info).text = binding.playerViewsInfo.text =
context?.getString(R.string.views, response.views.formatShort()) + context?.getString(R.string.views, response.views.formatShort()) +
"" + response.uploadDate "" + response.uploadDate
view.findViewById<TextView>(R.id.textLike).text = response.likes.formatShort() binding.textLike.text = response.likes.formatShort()
view.findViewById<TextView>(R.id.textDislike).text = response.dislikes.formatShort() binding.textDislike.text = response.dislikes.formatShort()
val channelImage = view.findViewById<ImageView>(R.id.player_channelImage) Picasso.get().load(response.uploaderAvatar).into(binding.playerChannelImage)
Picasso.get().load(response.uploaderAvatar).into(channelImage) binding.playerChannelName.text = response.uploader
view.findViewById<TextView>(R.id.player_channelName).text = response.uploader
view.findViewById<TextView>(R.id.title_textView).text = response.title binding.titleTextView.text = response.title
view.findViewById<TextView>(R.id.player_title).text = response.title binding.playerTitle.text = response.title
view.findViewById<TextView>(R.id.player_description).text = response.description binding.playerDescription.text = response.description
view.findViewById<TextView>(R.id.exo_title).text = response.title view.findViewById<TextView>(R.id.exo_title).text = response.title
@ -689,24 +672,21 @@ class PlayerFragment : Fragment() {
if (playWhenReady && playbackState == Player.STATE_READY) { if (playWhenReady && playbackState == Player.STATE_READY) {
// media actually playing // media actually playing
transitioning = false transitioning = false
view.findViewById<ImageView>(R.id.play_imageView) binding.playImageView.setImageResource(R.drawable.ic_pause)
.setImageResource(R.drawable.ic_pause)
} else if (playWhenReady) { } else if (playWhenReady) {
// might be idle (plays after prepare()), // might be idle (plays after prepare()),
// buffering (plays when data available) // buffering (plays when data available)
// or ended (plays when seek away from end) // or ended (plays when seek away from end)
view.findViewById<ImageView>(R.id.play_imageView) binding.playImageView.setImageResource(R.drawable.ic_play)
.setImageResource(R.drawable.ic_play)
} else { } else {
// player paused in any state // player paused in any state
view.findViewById<ImageView>(R.id.play_imageView) binding.playImageView.setImageResource(R.drawable.ic_play)
.setImageResource(R.drawable.ic_play)
} }
} }
}) })
// share button // share button
view.findViewById<LinearLayout>(R.id.relPlayer_share).setOnClickListener { binding.relPlayerShare.setOnClickListener {
val shareDialog = ShareDialog(videoId!!, false) val shareDialog = ShareDialog(videoId!!, false)
shareDialog.show(childFragmentManager, "ShareDialog") shareDialog.show(childFragmentManager, "ShareDialog")
} }
@ -731,7 +711,7 @@ class PlayerFragment : Fragment() {
} }
if (response.hls != null) { if (response.hls != null) {
view.findViewById<LinearLayout>(R.id.relPlayer_vlc).setOnClickListener { binding.relPlayerVlc.setOnClickListener {
// start an intent with video as mimetype using the hls stream // start an intent with video as mimetype using the hls stream
val uri: Uri = Uri.parse(response.hls) val uri: Uri = Uri.parse(response.hls)
val intent = Intent() val intent = Intent()
@ -748,14 +728,14 @@ class PlayerFragment : Fragment() {
} }
if (relatedStreamsEnabled) { if (relatedStreamsEnabled) {
// only show related streams if enabled // only show related streams if enabled
relatedRecView.adapter = TrendingAdapter( binding.relatedRecView.adapter = TrendingAdapter(
response.relatedStreams!!, response.relatedStreams!!,
childFragmentManager childFragmentManager
) )
} }
// set video description // set video description
val description = response.description!! val description = response.description!!
view.findViewById<TextView>(R.id.player_description).text = binding.playerDescription.text =
// detect whether the description is html formatted // detect whether the description is html formatted
if (description.contains("<") && description.contains(">")) { if (description.contains("<") && description.contains(">")) {
if (SDK_INT >= Build.VERSION_CODES.N) { if (SDK_INT >= Build.VERSION_CODES.N) {
@ -768,19 +748,18 @@ class PlayerFragment : Fragment() {
description description
} }
view.findViewById<RelativeLayout>(R.id.player_channel).setOnClickListener { binding.playerChannel.setOnClickListener {
val activity = view.context as MainActivity val activity = view.context as MainActivity
val bundle = bundleOf("channel_id" to response.uploaderUrl) val bundle = bundleOf("channel_id" to response.uploaderUrl)
activity.navController.navigate(R.id.channel, bundle) activity.navController.navigate(R.id.channel, bundle)
activity.findViewById<MotionLayout>(R.id.mainMotionLayout).transitionToEnd() activity.binding.mainMotionLayout.transitionToEnd()
view.findViewById<MotionLayout>(R.id.playerMotionLayout).transitionToEnd() binding.playerMotionLayout.transitionToEnd()
} }
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
if (token != "") { if (token != "") {
val channelId = response.uploaderUrl?.replace("/channel/", "") val channelId = response.uploaderUrl?.replace("/channel/", "")
val subButton = view.findViewById<MaterialButton>(R.id.player_subscribe) isSubscribed(binding.playerSubscribe, channelId!!)
isSubscribed(subButton, channelId!!) binding.save.setOnClickListener {
view.findViewById<LinearLayout>(R.id.save).setOnClickListener {
val newFragment = AddtoPlaylistDialog() val newFragment = AddtoPlaylistDialog()
val bundle = Bundle() val bundle = Bundle()
bundle.putString("videoId", videoId) bundle.putString("videoId", videoId)
@ -791,13 +770,11 @@ class PlayerFragment : Fragment() {
} }
private fun initializeChapters(chapters: List<ChapterSegment>) { private fun initializeChapters(chapters: List<ChapterSegment>) {
val chaptersRecView = view?.findViewById<RecyclerView>(R.id.chapters_recView)
if (chapters.isNotEmpty()) { if (chapters.isNotEmpty()) {
chaptersRecView?.layoutManager = binding.chaptersRecView.layoutManager =
LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false) LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)
chaptersRecView?.adapter = ChaptersAdapter(chapters, exoPlayer) binding.chaptersRecView.adapter = ChaptersAdapter(chapters, exoPlayer)
chaptersRecView?.visibility = View.VISIBLE binding.chaptersRecView.visibility = View.VISIBLE
} }
} }
@ -1142,7 +1119,7 @@ class PlayerFragment : Fragment() {
return@launchWhenCreated return@launchWhenCreated
} }
commentsAdapter = CommentsAdapter(videoId!!, commentsResponse.comments) commentsAdapter = CommentsAdapter(videoId!!, commentsResponse.comments)
commentsRecView.adapter = commentsAdapter binding.commentsRecView.adapter = commentsAdapter
nextPage = commentsResponse.nextpage nextPage = commentsResponse.nextpage
commentsLoaded = true commentsLoaded = true
isLoading = false isLoading = false
@ -1175,35 +1152,34 @@ class PlayerFragment : Fragment() {
if (isInPictureInPictureMode) { if (isInPictureInPictureMode) {
exoPlayerView.hideController() exoPlayerView.hideController()
exoPlayerView.useController = false exoPlayerView.useController = false
with(motionLayout) { with(binding.playerMotionLayout) {
getConstraintSet(R.id.start).constrainHeight(R.id.player, -1) getConstraintSet(R.id.start).constrainHeight(R.id.player, -1)
enableTransition(R.id.yt_transition, false) enableTransition(R.id.yt_transition, false)
} }
view?.findViewById<ConstraintLayout>(R.id.main_container)?.isClickable = true binding.mainContainer.isClickable = true
view?.findViewById<LinearLayout>(R.id.top_bar)?.visibility = View.GONE view?.findViewById<LinearLayout>(R.id.exo_top_bar)?.visibility = View.GONE
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
isFullScreen = false isFullScreen = false
} else { } else {
with(motionLayout) { with(binding.playerMotionLayout) {
getConstraintSet(R.id.start).constrainHeight(R.id.player, 0) getConstraintSet(R.id.start).constrainHeight(R.id.player, 0)
enableTransition(R.id.yt_transition, true) enableTransition(R.id.yt_transition, true)
} }
exoPlayerView.showController() exoPlayerView.showController()
exoPlayerView.useController = true exoPlayerView.useController = true
view?.findViewById<ConstraintLayout>(R.id.main_container)?.isClickable = false binding.mainContainer.isClickable = false
view?.findViewById<LinearLayout>(R.id.top_bar)?.visibility = View.VISIBLE view?.findViewById<LinearLayout>(R.id.exo_top_bar)?.visibility = View.VISIBLE
} }
} }
fun onUserLeaveHint() { fun onUserLeaveHint() {
val bounds = Rect() val bounds = Rect()
val scrollView = view?.findViewById<ScrollView>(R.id.player_scrollView) binding.playerScrollView.getHitRect(bounds)
scrollView?.getHitRect(bounds)
if (SDK_INT >= Build.VERSION_CODES.O && if (SDK_INT >= Build.VERSION_CODES.O &&
exoPlayer.isPlaying && ( exoPlayer.isPlaying && (
scrollView?.getLocalVisibleRect(bounds) == true || binding.playerScrollView.getLocalVisibleRect(bounds) == true ||
isFullScreen isFullScreen
) )
) { ) {

View File

@ -28,7 +28,7 @@
android:background="@color/exo_black_opacity_60" /> android:background="@color/exo_black_opacity_60" />
<LinearLayout <LinearLayout
android:id="@+id/top_bar" android:id="@+id/exo_top_bar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/exo_styled_bottom_bar_height" android:layout_height="@dimen/exo_styled_bottom_bar_height"
android:layout_gravity="top" android:layout_gravity="top"

View File

@ -326,7 +326,7 @@
android:visibility="gone" /> android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/player_recView" android:id="@+id/related_rec_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/comments_recView" android:layout_below="@id/comments_recView"