Merge pull request #1826 from Bnyro/master

[Option] New Player UI
This commit is contained in:
Bnyro 2022-11-12 19:05:30 +01:00 committed by GitHub
commit ca84eadc18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 173 additions and 120 deletions

View File

@ -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

View File

@ -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()
}
}
}

View File

@ -17,6 +17,7 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.TrendingRowBinding
import com.github.libretube.databinding.VideoRowBinding
import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.toDp
import com.github.libretube.extensions.toID
import com.github.libretube.ui.extensions.setFormattedDuration
import com.github.libretube.ui.extensions.setWatchProgressLength
@ -59,7 +60,7 @@ class VideosAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideosViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return when {
forceMode == ForceMode.TRENDING -> VideosViewHolder(TrendingRowBinding.inflate(layoutInflater, parent, false))
forceMode in listOf(ForceMode.TRENDING, ForceMode.RELATED) -> VideosViewHolder(TrendingRowBinding.inflate(layoutInflater, parent, false))
forceMode == ForceMode.CHANNEL -> VideosViewHolder(VideoRowBinding.inflate(layoutInflater, parent, false))
PreferenceHelper.getBoolean(
PreferenceKeys.ALTERNATIVE_VIDEOS_LAYOUT,
@ -82,6 +83,12 @@ class VideosAdapter(
// Trending layout
holder.trendingRowBinding?.apply {
if (forceMode == ForceMode.RELATED) {
val params = root.layoutParams
params.width = (180).toDp(root.context.resources).toInt()
root.layoutParams = params
}
textViewTitle.text = video.title
textViewChannel.text =
video.uploaderName + TextUtils.SEPARATOR +
@ -160,7 +167,8 @@ class VideosAdapter(
NONE,
TRENDING,
ROW,
CHANNEL
CHANNEL,
RELATED
}
fun getLayout(context: Context): LayoutManager {

View File

@ -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,8 @@ 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
import com.github.libretube.databinding.DoubleTapOverlayBinding
@ -120,7 +123,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private var videoId: String? = null
private var playlistId: String? = null
private var isLive = false
private lateinit var streams: com.github.libretube.api.obj.Streams
private lateinit var streams: Streams
/**
* for the transition
@ -133,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
@ -383,6 +385,12 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
binding.commentsRecView.setItemViewCacheSize(20)
binding.relatedRecView.layoutManager = VideosAdapter.getLayout(requireContext())
binding.alternativeTrendingRec.layoutManager = LinearLayoutManager(
context,
LinearLayoutManager.HORIZONTAL,
false
)
}
private fun setFullscreen() {
@ -450,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() {
@ -610,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)
}
}
}
@ -629,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)
}
}
}
@ -699,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()
}
@ -742,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,
@ -891,13 +898,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
}
}
if (PlayerHelper.relatedStreamsEnabled) {
// only show related streams if enabled
binding.relatedRecView.adapter = VideosAdapter(
response.relatedStreams.orEmpty().toMutableList(),
childFragmentManager
)
}
initializeRelatedVideos(response.relatedStreams)
// set video description
val description = response.description!!
binding.playerDescription.text =
@ -960,6 +961,23 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
}
private fun initializeRelatedVideos(relatedStreams: List<StreamItem>?) {
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
@ -1158,7 +1176,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
private fun setStreamSource(
streams: com.github.libretube.api.obj.Streams,
streams: Streams,
videosNameArray: Array<String>,
videosUrlArray: Array<Uri>
) {
@ -1269,7 +1287,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
commentsAdapter = CommentsAdapter(videoId!!, commentsResponse.comments)
binding.commentsRecView.adapter = commentsAdapter
nextPage = commentsResponse.nextpage
commentsLoaded = true
isLoading = false
}
}

View File

@ -1,10 +1,6 @@
package com.github.libretube.ui.preferences
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.SwitchPreferenceCompat
@ -14,7 +10,6 @@ import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.base.BasePreferenceFragment
import com.github.libretube.ui.dialogs.NavBarOptionsDialog
import com.github.libretube.ui.dialogs.RequireRestartDialog
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ThemeHelper
import com.google.android.material.color.DynamicColors
@ -69,27 +64,6 @@ class AppearanceSettings : BasePreferenceFragment() {
true
}
val systemCaptionStyle =
findPreference<SwitchPreferenceCompat>(PreferenceKeys.SYSTEM_CAPTION_STYLE)
val captionSettings = findPreference<Preference>(PreferenceKeys.CAPTION_SETTINGS)
captionSettings?.isVisible =
PreferenceHelper.getBoolean(PreferenceKeys.SYSTEM_CAPTION_STYLE, true)
systemCaptionStyle?.setOnPreferenceChangeListener { _, newValue ->
captionSettings?.isVisible = newValue as Boolean
true
}
captionSettings?.setOnPreferenceClickListener {
try {
val captionSettingsIntent = Intent(Settings.ACTION_CAPTIONING_SETTINGS)
startActivity(captionSettingsIntent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(activity, R.string.error, Toast.LENGTH_SHORT).show()
}
true
}
val legacySubscriptionView =
findPreference<SwitchPreferenceCompat>(PreferenceKeys.LEGACY_SUBSCRIPTIONS)
val legacySubscriptionColumns =

View File

@ -1,6 +1,10 @@
package com.github.libretube.ui.preferences
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.SwitchPreferenceCompat
@ -37,6 +41,27 @@ class PlayerSettings : BasePreferenceFragment() {
val defaultSubtitle = findPreference<ListPreference>(PreferenceKeys.DEFAULT_SUBTITLE)
defaultSubtitle?.let { setupSubtitlePref(it) }
val systemCaptionStyle =
findPreference<SwitchPreferenceCompat>(PreferenceKeys.SYSTEM_CAPTION_STYLE)
val captionSettings = findPreference<Preference>(PreferenceKeys.CAPTION_SETTINGS)
captionSettings?.isVisible =
PreferenceHelper.getBoolean(PreferenceKeys.SYSTEM_CAPTION_STYLE, true)
systemCaptionStyle?.setOnPreferenceChangeListener { _, newValue ->
captionSettings?.isVisible = newValue as Boolean
true
}
captionSettings?.setOnPreferenceClickListener {
try {
val captionSettingsIntent = Intent(Settings.ACTION_CAPTIONING_SETTINGS)
startActivity(captionSettingsIntent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(activity, R.string.error, Toast.LENGTH_SHORT).show()
}
true
}
}
private fun setupSubtitlePref(preference: ListPreference) {

View File

@ -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(

View File

@ -5,8 +5,7 @@
android:id="@+id/playerMotionLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/player_scene"
tools:context=".ui.fragments.PlayerFragment">
app:layoutDescription="@xml/player_scene">
<ScrollView
android:id="@+id/player_scrollView"
@ -147,13 +146,13 @@
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:weightSum="5"
android:orientation="horizontal">
android:orientation="horizontal"
android:weightSum="5">
<LinearLayout
android:id="@+id/relPlayer_share"
android:layout_weight="1"
style="@style/PlayerActionsLayout">
style="@style/PlayerActionsLayout"
android:layout_weight="1">
<ImageView
android:layout_width="24dp"
@ -161,16 +160,16 @@
android:src="@drawable/ic_share" />
<TextView
style="@style/PlayerActionsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/PlayerActionsText"
android:text="@string/share" />
</LinearLayout>
<LinearLayout
android:id="@+id/relPlayer_download"
android:layout_weight="1"
style="@style/PlayerActionsLayout">
style="@style/PlayerActionsLayout"
android:layout_weight="1">
<ImageView
android:layout_width="24dp"
@ -178,16 +177,16 @@
android:src="@drawable/ic_download" />
<TextView
style="@style/PlayerActionsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/PlayerActionsText"
android:text="@string/download" />
</LinearLayout>
<LinearLayout
android:id="@+id/relPlayer_open"
android:layout_weight="1"
style="@style/PlayerActionsLayout">
style="@style/PlayerActionsLayout"
android:layout_weight="1">
<ImageView
android:layout_width="24dp"
@ -196,16 +195,16 @@
android:src="@drawable/ic_open" />
<TextView
style="@style/PlayerActionsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/PlayerActionsText"
android:text="@string/open" />
</LinearLayout>
<LinearLayout
android:id="@+id/relPlayer_background"
android:layout_weight="1"
style="@style/PlayerActionsLayout">
style="@style/PlayerActionsLayout"
android:layout_weight="1">
<ImageView
android:layout_width="24dp"
@ -213,16 +212,16 @@
android:src="@drawable/ic_headphones" />
<TextView
style="@style/PlayerActionsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/PlayerActionsText"
android:text="@string/audio" />
</LinearLayout>
<LinearLayout
android:id="@+id/relPlayer_save"
android:layout_weight="1"
style="@style/PlayerActionsLayout">
style="@style/PlayerActionsLayout"
android:layout_weight="1">
<ImageView
android:layout_width="24dp"
@ -230,9 +229,9 @@
android:src="@drawable/ic_save" />
<TextView
style="@style/PlayerActionsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/PlayerActionsText"
android:text="@string/save" />
</LinearLayout>
@ -300,6 +299,12 @@
app:cornerRadius="11dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/alternativeTrendingRec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp" />
<com.google.android.material.card.MaterialCardView
android:id="@+id/comments_toggle"
style="@style/Widget.Material3.CardView.Elevated"

View File

@ -82,6 +82,8 @@
android:id="@+id/textView_channel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:paddingBottom="16dp"
app:layout_constraintEnd_toEndOf="@+id/textView_title"
app:layout_constraintStart_toStartOf="@+id/textView_title"

View File

@ -228,7 +228,7 @@
<string name="change_playback_speed">Playback speed</string>
<string name="require_restart">App restart required</string>
<string name="require_restart_message">This change requires the app to be restarted. Press \'Ok\' to restart now.</string>
<string name="navLabelVisibility">Navbar label visibility</string>
<string name="navLabelVisibility">Label visibility</string>
<string name="always">Always</string>
<string name="selected">Selected</string>
<string name="never">Never</string>
@ -365,6 +365,11 @@
<string name="end_time">End time</string>
<string name="notification_time">Notification time</string>
<string name="notification_time_summary">Time span in which notifications are allowed to show.</string>
<string name="alternative_trending_layout">Alternative trending layout</string>
<string name="navbar_order">Order</string>
<string name="layout">Layout</string>
<string name="alternative_player_layout">Alternative player layout</string>
<string name="alternative_player_layout_summary">Show the related videos as a row above the comments instead of below.</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>

View File

@ -38,14 +38,14 @@
app:title="@string/app_icon"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/navigation_bar">
<Preference
android:icon="@drawable/ic_home"
app:key="navbar_items"
app:title="@string/navigation_bar" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/app_behavior">
app:title="@string/navbar_order" />
<ListPreference
android:icon="@drawable/ic_label"
@ -56,33 +56,9 @@
app:title="@string/navLabelVisibility"
app:useSimpleSummaryProvider="true" />
<ListPreference
android:icon="@drawable/ic_grid"
app:defaultValue="@integer/grid_items"
app:entries="@array/grid"
app:entryValues="@array/grid"
app:key="grid"
app:title="@string/grid"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/player">
<SwitchPreferenceCompat
android:icon="@drawable/ic_caption"
app:defaultValue="true"
app:key="system_caption_style"
app:title="@string/system_caption_style" />
<Preference
android:icon="@drawable/ic_settings"
app:key="caption_settings"
app:title="@string/caption_settings" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/misc">
<PreferenceCategory app:title="@string/layout">
<SwitchPreferenceCompat
app:defaultValue="false"
@ -105,6 +81,26 @@
app:key="alternative_videos_layout"
app:title="@string/alternative_videos_layout" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_list"
app:defaultValue="false"
app:key="alternative_player_layout"
app:summary="@string/alternative_player_layout_summary"
app:title="@string/alternative_player_layout" />
<ListPreference
android:icon="@drawable/ic_grid"
app:defaultValue="@integer/grid_items"
app:entries="@array/grid"
app:entryValues="@array/grid"
app:key="grid"
app:title="@string/grid"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/misc">
<ListPreference
android:defaultValue="recent"
android:entries="@array/playlistSortingOptions"

View File

@ -55,6 +55,21 @@
</PreferenceCategory>
<PreferenceCategory app:title="@string/appearance">
<SwitchPreferenceCompat
android:icon="@drawable/ic_caption"
app:defaultValue="true"
app:key="system_caption_style"
app:title="@string/system_caption_style" />
<Preference
android:icon="@drawable/ic_settings"
app:key="caption_settings"
app:title="@string/caption_settings" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/behavior">
<SwitchPreferenceCompat