From 0498e52f5d8376e40eba0332c85353335bfdf0c4 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 30 Jun 2022 21:25:40 +0200 Subject: [PATCH] viewbind playerfragment --- .../libretube/fragments/PlayerFragment.kt | 128 +++++++----------- .../layout/exo_styled_player_control_view.xml | 2 +- app/src/main/res/layout/fragment_player.xml | 2 +- 3 files changed, 54 insertions(+), 78 deletions(-) diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index a6762f05d..a928122dc 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -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(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(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(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(R.id.player_views_info).text = + binding.playerViewsInfo.text = context?.getString(R.string.views, response.views.formatShort()) + " • " + response.uploadDate - view.findViewById(R.id.textLike).text = response.likes.formatShort() - view.findViewById(R.id.textDislike).text = response.dislikes.formatShort() - val channelImage = view.findViewById(R.id.player_channelImage) - Picasso.get().load(response.uploaderAvatar).into(channelImage) - view.findViewById(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(R.id.title_textView).text = response.title - view.findViewById(R.id.player_title).text = response.title - view.findViewById(R.id.player_description).text = response.description + binding.titleTextView.text = response.title + binding.playerTitle.text = response.title + binding.playerDescription.text = response.description view.findViewById(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(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(R.id.play_imageView) - .setImageResource(R.drawable.ic_play) + binding.playImageView.setImageResource(R.drawable.ic_play) } else { // player paused in any state - view.findViewById(R.id.play_imageView) - .setImageResource(R.drawable.ic_play) + binding.playImageView.setImageResource(R.drawable.ic_play) } } }) // share button - view.findViewById(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(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(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(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(R.id.mainMotionLayout).transitionToEnd() - view.findViewById(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(R.id.player_subscribe) - isSubscribed(subButton, channelId!!) - view.findViewById(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) { - val chaptersRecView = view?.findViewById(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(R.id.main_container)?.isClickable = true - view?.findViewById(R.id.top_bar)?.visibility = View.GONE + binding.mainContainer.isClickable = true + view?.findViewById(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(R.id.main_container)?.isClickable = false - view?.findViewById(R.id.top_bar)?.visibility = View.VISIBLE + binding.mainContainer.isClickable = false + view?.findViewById(R.id.exo_top_bar)?.visibility = View.VISIBLE } } fun onUserLeaveHint() { val bounds = Rect() - val scrollView = view?.findViewById(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 ) ) { diff --git a/app/src/main/res/layout/exo_styled_player_control_view.xml b/app/src/main/res/layout/exo_styled_player_control_view.xml index 02d9d5655..d0f2f93f2 100644 --- a/app/src/main/res/layout/exo_styled_player_control_view.xml +++ b/app/src/main/res/layout/exo_styled_player_control_view.xml @@ -28,7 +28,7 @@ android:background="@color/exo_black_opacity_60" />