Merge pull request #1816 from Bnyro/master

apply grid columns for recommended videos
This commit is contained in:
Bnyro 2022-11-12 10:17:04 +01:00 committed by GitHub
commit f838ad0d1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 41 deletions

View File

@ -1,12 +1,16 @@
package com.github.libretube.ui.adapters package com.github.libretube.ui.adapters
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context
import android.text.format.DateUtils import android.text.format.DateUtils
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
@ -27,7 +31,7 @@ class VideosAdapter(
private val streamItems: MutableList<StreamItem>, private val streamItems: MutableList<StreamItem>,
private val childFragmentManager: FragmentManager, private val childFragmentManager: FragmentManager,
private val showAllAtOnce: Boolean = true, private val showAllAtOnce: Boolean = true,
private val forceType: Int = FORCE_NONE private val forceMode: ForceMode = ForceMode.NONE
) : RecyclerView.Adapter<VideosViewHolder>() { ) : RecyclerView.Adapter<VideosViewHolder>() {
var index = 10 var index = 10
@ -55,8 +59,8 @@ class VideosAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideosViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideosViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
return when { return when {
forceType == FORCE_TRENDING -> VideosViewHolder(TrendingRowBinding.inflate(layoutInflater, parent, false)) forceMode == ForceMode.TRENDING -> VideosViewHolder(TrendingRowBinding.inflate(layoutInflater, parent, false))
forceType == FORCE_CHANNEL -> VideosViewHolder(VideoRowBinding.inflate(layoutInflater, parent, false)) forceMode == ForceMode.CHANNEL -> VideosViewHolder(VideoRowBinding.inflate(layoutInflater, parent, false))
PreferenceHelper.getBoolean( PreferenceHelper.getBoolean(
PreferenceKeys.ALTERNATIVE_VIDEOS_LAYOUT, PreferenceKeys.ALTERNATIVE_VIDEOS_LAYOUT,
false false
@ -122,7 +126,7 @@ class VideosAdapter(
ImageHelper.loadImage(video.thumbnail, thumbnail) ImageHelper.loadImage(video.thumbnail, thumbnail)
if (forceType != FORCE_CHANNEL) { if (forceMode != ForceMode.CHANNEL) {
ImageHelper.loadImage(video.uploaderAvatar, channelImage) ImageHelper.loadImage(video.uploaderAvatar, channelImage)
channelName.text = video.uploaderName channelName.text = video.uploaderName
@ -152,9 +156,29 @@ class VideosAdapter(
} }
companion object { companion object {
const val FORCE_NONE = 0 enum class ForceMode {
const val FORCE_TRENDING = 1 NONE,
const val FORCE_NORMAL = 2 TRENDING,
const val FORCE_CHANNEL = 3 ROW,
CHANNEL
}
fun getLayout(context: Context): LayoutManager {
return if (PreferenceHelper.getBoolean(
PreferenceKeys.ALTERNATIVE_VIDEOS_LAYOUT,
false
)
) {
LinearLayoutManager(context)
} else {
GridLayoutManager(
context,
PreferenceHelper.getString(
PreferenceKeys.GRID_COLUMNS,
context.resources.getInteger(R.integer.grid_items).toString()
).toInt()
)
}
}
} }
} }

View File

@ -175,7 +175,7 @@ class ChannelFragment : BaseFragment() {
channelAdapter = VideosAdapter( channelAdapter = VideosAdapter(
response.relatedStreams.orEmpty().toMutableList(), response.relatedStreams.orEmpty().toMutableList(),
childFragmentManager, childFragmentManager,
forceType = VideosAdapter.FORCE_CHANNEL forceMode = VideosAdapter.Companion.ForceMode.CHANNEL
) )
binding.channelRecView.adapter = channelAdapter binding.channelRecView.adapter = channelAdapter
} }

View File

@ -16,7 +16,6 @@ import com.github.libretube.extensions.TAG
import com.github.libretube.ui.activities.SettingsActivity import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.adapters.VideosAdapter import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.base.BaseFragment import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.util.LayoutHelper
import com.github.libretube.util.LocaleHelper import com.github.libretube.util.LocaleHelper
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
@ -101,7 +100,7 @@ class HomeFragment : BaseFragment() {
childFragmentManager childFragmentManager
) )
binding.recview.layoutManager = LayoutHelper.getVideoLayout(requireContext()) binding.recview.layoutManager = VideosAdapter.getLayout(requireContext())
} }
} }
} }

View File

@ -31,7 +31,6 @@ import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.R import com.github.libretube.R
@ -331,7 +330,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// FullScreen button trigger // FullScreen button trigger
// hide fullscreen button if auto rotation enabled // hide fullscreen button if auto rotation enabled
playerBinding.fullscreen.visibility = playerBinding.fullscreen.visibility =
if (PlayerHelper.autoRotationEnabled) View.GONE else View.VISIBLE if (PlayerHelper.autoRotationEnabled) View.INVISIBLE else View.VISIBLE
playerBinding.fullscreen.setOnClickListener { playerBinding.fullscreen.setOnClickListener {
// hide player controller // hide player controller
exoPlayerView.hideController() exoPlayerView.hideController()
@ -382,8 +381,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
binding.commentsRecView.layoutManager = LinearLayoutManager(view?.context) binding.commentsRecView.layoutManager = LinearLayoutManager(view?.context)
binding.commentsRecView.setItemViewCacheSize(20) binding.commentsRecView.setItemViewCacheSize(20)
binding.relatedRecView.layoutManager = binding.relatedRecView.layoutManager = VideosAdapter.getLayout(requireContext())
GridLayoutManager(view?.context, resources.getInteger(R.integer.grid_items))
} }
private fun setFullscreen() { private fun setFullscreen() {

View File

@ -18,7 +18,6 @@ import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.base.BaseFragment import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.models.SubscriptionsViewModel import com.github.libretube.ui.models.SubscriptionsViewModel
import com.github.libretube.ui.sheets.BaseBottomSheet import com.github.libretube.ui.sheets.BaseBottomSheet
import com.github.libretube.util.LayoutHelper
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
class SubscriptionsFragment : BaseFragment() { class SubscriptionsFragment : BaseFragment() {
@ -49,7 +48,7 @@ class SubscriptionsFragment : BaseFragment() {
binding.subProgress.visibility = View.VISIBLE binding.subProgress.visibility = View.VISIBLE
binding.subFeed.layoutManager = LayoutHelper.getVideoLayout(requireContext()) binding.subFeed.layoutManager = VideosAdapter.getLayout(requireContext())
if (viewModel.videoFeed.value == null || !loadFeedInBackground) { if (viewModel.videoFeed.value == null || !loadFeedInBackground) {
viewModel.videoFeed.value = null viewModel.videoFeed.value = null

View File

@ -1,26 +1,3 @@
package com.github.libretube.util package com.github.libretube.util
import android.content.Context object LayoutHelper
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
object LayoutHelper {
fun getVideoLayout(context: Context) = if (
PreferenceHelper.getBoolean(
PreferenceKeys.ALTERNATIVE_VIDEOS_LAYOUT,
false
)
) {
LinearLayoutManager(context)
} else {
GridLayoutManager(
context,
PreferenceHelper.getString(
PreferenceKeys.GRID_COLUMNS,
context.resources.getInteger(R.integer.grid_items).toString()
).toInt()
)
}
}