From 0786e8f87cb73e6ab60dfbe58f5ed06ce67f985a Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 12 Nov 2022 19:04:40 +0100 Subject: [PATCH] add option for it --- .../libretube/constants/PreferenceKeys.kt | 1 + .../libretube/services/BackgroundMode.kt | 17 ++-- .../libretube/ui/fragments/PlayerFragment.kt | 80 ++++++++++--------- .../com/github/libretube/util/PlayerHelper.kt | 6 ++ app/src/main/res/layout/fragment_player.xml | 3 +- app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/appearance_settings.xml | 7 ++ 7 files changed, 67 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index fd840c68f..9f0f69781 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -80,6 +80,7 @@ object PreferenceKeys { const val SB_SHOW_MARKERS = "sb_show_markers" const val LIMIT_HLS = "limit_hls" const val PROGRESSIVE_LOADING_INTERVAL_SIZE = "progressive_loading_interval" + const val ALTERNATIVE_PLAYER_LAYOUT = "alternative_player_layout" /** * Background mode diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index 0d26ffc45..ddf0249a5 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -307,16 +307,15 @@ class BackgroundMode : Service() { */ private fun fetchSponsorBlockSegments() { CoroutineScope(Dispatchers.IO).launch { - kotlin.runCatching { + runCatching { val categories = PlayerHelper.getSponsorBlockCategories() - if (categories.size > 0) { - segmentData = - RetrofitInstance.api.getSegments( - videoId, - ObjectMapper().writeValueAsString(categories) - ) - checkForSegments() - } + if (categories.isEmpty()) return@runCatching + segmentData = + RetrofitInstance.api.getSegments( + videoId, + ObjectMapper().writeValueAsString(categories) + ) + checkForSegments() } } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index f56d7dcc5..da5be8e6f 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -27,6 +27,7 @@ import android.widget.Toast import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.net.toUri import androidx.core.os.bundleOf +import androidx.core.view.isEmpty import androidx.core.view.isVisible import androidx.fragment.app.activityViewModels import androidx.lifecycle.Lifecycle @@ -38,6 +39,7 @@ import com.github.libretube.api.CronetHelper import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.obj.ChapterSegment import com.github.libretube.api.obj.SegmentData +import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.Streams import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys @@ -134,7 +136,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { * for the comments */ private var commentsAdapter: CommentsAdapter? = null - private var commentsLoaded: Boolean? = false private var nextPage: String? = null private var isLoading = true @@ -457,11 +458,14 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } private fun toggleComments() { - 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() + if (binding.commentsRecView.isVisible) { + binding.commentsRecView.visibility = View.GONE + binding.relatedRecView.visibility = View.VISIBLE + } else { + binding.commentsRecView.visibility = View.VISIBLE + binding.relatedRecView.visibility = View.GONE + if (binding.commentsRecView.isEmpty()) fetchComments() + } } override fun onStart() { @@ -617,15 +621,13 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { // show the player notification initializePlayerNotification() if (PlayerHelper.sponsorBlockEnabled) fetchSponsorBlockSegments() - // show comments if related streams disabled - if (!PlayerHelper.relatedStreamsEnabled) toggleComments() + + // show comments if related streams are disabled or the alternative related streams layout is chosen + if (!PlayerHelper.relatedStreamsEnabled || PlayerHelper.alternativeVideoLayout) toggleComments() // add the video to the watch history if (PlayerHelper.watchHistoryEnabled) { - DatabaseHelper.addToWatchHistory( - videoId!!, - streams - ) + DatabaseHelper.addToWatchHistory(videoId!!, streams) } } } @@ -636,16 +638,15 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { */ private fun fetchSponsorBlockSegments() { CoroutineScope(Dispatchers.IO).launch { - kotlin.runCatching { + runCatching { val categories = PlayerHelper.getSponsorBlockCategories() - if (categories.size > 0) { - segmentData = - RetrofitInstance.api.getSegments( - videoId!!, - ObjectMapper().writeValueAsString(categories) - ) - playerBinding.exoProgress.setSegments(segmentData.segments) - } + if (categories.isEmpty()) return@runCatching + segmentData = + RetrofitInstance.api.getSegments( + videoId!!, + ObjectMapper().writeValueAsString(categories) + ) + playerBinding.exoProgress.setSegments(segmentData.segments) } } } @@ -706,7 +707,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { videoId = nextVideoId // forces the comments to reload for the new video - commentsLoaded = false binding.commentsRecView.adapter = null playVideo() } @@ -749,7 +749,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } @SuppressLint("SetTextI18n") - private fun initializePlayerView(response: com.github.libretube.api.obj.Streams) { + private fun initializePlayerView(response: Streams) { // initialize the player view actions binding.player.initialize( childFragmentManager, @@ -898,19 +898,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } } } - if (PlayerHelper.relatedStreamsEnabled) { - // only show related streams if enabled - binding.relatedRecView.adapter = VideosAdapter( - response.relatedStreams.orEmpty().toMutableList(), - childFragmentManager - ) - - binding.alternativeTrendingRec.adapter = VideosAdapter( - response.relatedStreams.orEmpty().toMutableList(), - childFragmentManager, - forceMode = VideosAdapter.Companion.ForceMode.RELATED - ) - } + initializeRelatedVideos(response.relatedStreams) // set video description val description = response.description!! binding.playerDescription.text = @@ -973,6 +961,23 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } } + private fun initializeRelatedVideos(relatedStreams: List?) { + if (!PlayerHelper.relatedStreamsEnabled) return + + if (PlayerHelper.alternativeVideoLayout) { + binding.alternativeTrendingRec.adapter = VideosAdapter( + relatedStreams.orEmpty().toMutableList(), + childFragmentManager, + forceMode = VideosAdapter.Companion.ForceMode.RELATED + ) + } else { + binding.relatedRecView.adapter = VideosAdapter( + relatedStreams.orEmpty().toMutableList(), + childFragmentManager + ) + } + } + private fun initializeChapters() { if (chapters.isEmpty()) { binding.chaptersRecView.visibility = View.GONE @@ -1171,7 +1176,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } private fun setStreamSource( - streams: com.github.libretube.api.obj.Streams, + streams: Streams, videosNameArray: Array, videosUrlArray: Array ) { @@ -1282,7 +1287,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { commentsAdapter = CommentsAdapter(videoId!!, commentsResponse.comments) binding.commentsRecView.adapter = commentsAdapter nextPage = commentsResponse.nextpage - commentsLoaded = true isLoading = false } } diff --git a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt index 1821de4ae..b8d7281fc 100644 --- a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt @@ -287,6 +287,12 @@ object PlayerHelper { "fit" ) + val alternativeVideoLayout: Boolean + get() = PreferenceHelper.getBoolean( + PreferenceKeys.ALTERNATIVE_PLAYER_LAYOUT, + false + ) + fun getDefaultResolution(context: Context): String { return if (NetworkHelper.isNetworkMobile(context)) { PreferenceHelper.getString( diff --git a/app/src/main/res/layout/fragment_player.xml b/app/src/main/res/layout/fragment_player.xml index c323e8c75..775e8af8c 100644 --- a/app/src/main/res/layout/fragment_player.xml +++ b/app/src/main/res/layout/fragment_player.xml @@ -303,8 +303,7 @@ android:id="@+id/alternativeTrendingRec" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginHorizontal="15dp" - android:visibility="gone" /> + android:layout_marginHorizontal="12dp" /> Alternative trending layout Order Layout + Alternative player layout + Show the related videos as a row above the comments instead of below. Download Service diff --git a/app/src/main/res/xml/appearance_settings.xml b/app/src/main/res/xml/appearance_settings.xml index 19f3533a5..c89505e52 100644 --- a/app/src/main/res/xml/appearance_settings.xml +++ b/app/src/main/res/xml/appearance_settings.xml @@ -81,6 +81,13 @@ app:key="alternative_videos_layout" app:title="@string/alternative_videos_layout" /> + +