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

View File

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

View File

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