Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: separate landscape grid columns pref + more impacted adapters #5359

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ object PreferenceKeys {
const val THEME_MODE = "theme_toggle"
const val PURE_THEME = "pure_theme"
const val ACCENT_COLOR = "accent_color"
const val GRID_COLUMNS = "grid"
const val GRID_COLUMNS_PORTRAIT = "grid"
const val GRID_COLUMNS_LANDSCAPE = "grid_landscape"
const val LABEL_VISIBILITY = "label_visibility"
const val APP_ICON = "icon_change"
const val LEGACY_SUBSCRIPTIONS = "legacy_subscriptions"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/github/libretube/extensions/Int.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.libretube.extensions

import kotlin.math.ceil

fun Int.ceilHalf() = ceil((toDouble() / 2)).toInt()
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.core.os.bundleOf
import androidx.core.view.isGone
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.github.libretube.R
Expand All @@ -19,6 +18,7 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.AllCaughtUpRowBinding
import com.github.libretube.databinding.TrendingRowBinding
import com.github.libretube.databinding.VideoRowBinding
import com.github.libretube.extensions.ceilHalf
import com.github.libretube.extensions.dpToPx
import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.toID
Expand Down Expand Up @@ -219,21 +219,15 @@ class VideosAdapter(
RELATED_COLUMN
}

fun getLayout(context: Context): LayoutManager {
fun getLayout(context: Context, gridItems: Int): LayoutManager {
return if (PreferenceHelper.getBoolean(
PreferenceKeys.ALTERNATIVE_VIDEOS_LAYOUT,
false
)
) {
LinearLayoutManager(context)
GridLayoutManager(context, gridItems.ceilHalf())
} else {
GridLayoutManager(
context,
PreferenceHelper.getString(
PreferenceKeys.GRID_COLUMNS,
context.resources.getInteger(R.integer.grid_items).toString()
).toInt()
)
GridLayoutManager(context, gridItems)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.libretube.ui.base

import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.helpers.PreferenceHelper
import kotlin.math.min

abstract class DynamicLayoutManagerFragment: Fragment() {
abstract fun setLayoutManagers(gridItems: Int)

private fun getGridItemsCount(orientation: Int): Int {
val (prefKey, defaultValueRes) = when (orientation) {
Configuration.ORIENTATION_PORTRAIT -> PreferenceKeys.GRID_COLUMNS_PORTRAIT to R.integer.grid_items
else -> PreferenceKeys.GRID_COLUMNS_LANDSCAPE to R.integer.grid_items_landscape
}

val defaultValue = resources.getInteger(defaultValueRes).toString()
return PreferenceHelper.getString(prefKey, defaultValue).toInt()
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)

setLayoutManagers(getGridItemsCount(newConfig.orientation))
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setLayoutManagers(getGridItemsCount(resources.configuration.orientation))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import androidx.core.os.bundleOf
import androidx.core.view.children
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.GridLayoutManager
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.SubscriptionHelper
Expand All @@ -28,6 +27,7 @@ import com.github.libretube.obj.ChannelTabs
import com.github.libretube.obj.ShareData
import com.github.libretube.ui.adapters.SearchAdapter
import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.extensions.setupSubscriptionButton
import com.github.libretube.ui.sheets.AddChannelToGroupSheet
Expand All @@ -37,8 +37,9 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.HttpException
import java.io.IOException
import kotlin.math.ceil

class ChannelFragment : Fragment() {
class ChannelFragment : DynamicLayoutManagerFragment() {
private var _binding: FragmentChannelBinding? = null
private val binding get() = _binding!!

Expand Down Expand Up @@ -77,11 +78,15 @@ class ChannelFragment : Fragment() {
return binding.root
}

override fun setLayoutManagers(gridItems: Int) {
_binding?.channelRecView?.layoutManager = GridLayoutManager(context,
ceil((gridItems.toDouble() / 2)).toInt()
)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.channelRecView.layoutManager = LinearLayoutManager(context)

binding.channelRefresh.setOnRefreshListener {
fetchChannel()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.databinding.FragmentDownloadsBinding
import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.DownloadWithItems
import com.github.libretube.extensions.ceilHalf
import com.github.libretube.extensions.formatAsFileSize
import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.obj.DownloadStatus
import com.github.libretube.receivers.DownloadReceiver
import com.github.libretube.services.DownloadService
import com.github.libretube.ui.adapters.DownloadsAdapter
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
import com.github.libretube.ui.viewholders.DownloadsViewHolder
import kotlin.io.path.fileSize
import kotlinx.coroutines.Dispatchers
Expand All @@ -34,7 +35,7 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

class DownloadsFragment : Fragment() {
class DownloadsFragment : DynamicLayoutManagerFragment() {
private var _binding: FragmentDownloadsBinding? = null
private val binding get() = _binding!!

Expand Down Expand Up @@ -72,6 +73,10 @@ class DownloadsFragment : Fragment() {
return binding.root
}

override fun setLayoutManagers(gridItems: Int) {
_binding?.downloads?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

Expand All @@ -85,8 +90,6 @@ class DownloadsFragment : Fragment() {
binding.downloadsEmpty.isGone = true
binding.downloads.isVisible = true

binding.downloads.layoutManager = LinearLayoutManager(context)

val adapter = DownloadsAdapter(requireContext(), downloads) {
var isDownloading = false
val ids = it.downloadItems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import android.widget.Toast
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
Expand All @@ -26,11 +25,13 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentLibraryBinding
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.ceilHalf
import com.github.libretube.extensions.dpToPx
import com.github.libretube.helpers.NavBarHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter
import com.github.libretube.ui.adapters.PlaylistsAdapter
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
import com.github.libretube.ui.dialogs.CreatePlaylistDialog
import com.github.libretube.ui.dialogs.CreatePlaylistDialog.Companion.CREATE_PLAYLIST_DIALOG_REQUEST_KEY
import com.github.libretube.ui.models.PlayerViewModel
Expand All @@ -39,7 +40,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LibraryFragment : Fragment() {
class LibraryFragment : DynamicLayoutManagerFragment() {
private var _binding: FragmentLibraryBinding? = null
private val binding get() = _binding!!

Expand All @@ -54,13 +55,14 @@ class LibraryFragment : Fragment() {
return binding.root
}

override fun setLayoutManagers(gridItems: Int) {
_binding?.bookmarksRecView?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
_binding?.playlistRecView?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// initialize the layout managers
binding.bookmarksRecView.layoutManager = LinearLayoutManager(context)
binding.playlistRecView.layoutManager = LinearLayoutManager(context)

// listen for the mini player state changing
playerViewModel.isMiniPlayerVisible.observe(viewLifecycleOwner) {
updateFABMargin(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import androidx.core.text.parseAsHtml
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
Expand All @@ -28,6 +27,7 @@ import com.github.libretube.databinding.FragmentPlaylistBinding
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.ceilHalf
import com.github.libretube.extensions.dpToPx
import com.github.libretube.extensions.serializable
import com.github.libretube.extensions.toID
Expand All @@ -38,6 +38,7 @@ import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.adapters.PlaylistAdapter
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.BaseBottomSheet
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
Expand All @@ -47,7 +48,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

class PlaylistFragment : Fragment() {
class PlaylistFragment : DynamicLayoutManagerFragment() {
private var _binding: FragmentPlaylistBinding? = null
private val binding get() = _binding!!

Expand Down Expand Up @@ -89,10 +90,13 @@ class PlaylistFragment : Fragment() {
return binding.root
}

override fun setLayoutManagers(gridItems: Int) {
_binding?.playlistRecView?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.playlistRecView.layoutManager = LinearLayoutManager(context)
binding.playlistProgress.isVisible = true

isBookmarked = runBlocking(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.GridLayoutManager
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.IntentData
Expand All @@ -18,11 +17,13 @@ import com.github.libretube.databinding.FragmentSearchResultBinding
import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.obj.SearchHistoryItem
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.ceilHalf
import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.adapters.SearchAdapter
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.util.TextUtils
import com.github.libretube.util.TextUtils.toTimeInSeconds
Expand All @@ -31,8 +32,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import kotlin.math.ceil

class SearchResultFragment : Fragment() {
class SearchResultFragment : DynamicLayoutManagerFragment() {
private var _binding: FragmentSearchResultBinding? = null
private val binding get() = _binding!!

Expand All @@ -56,15 +58,17 @@ class SearchResultFragment : Fragment() {
return binding.root
}

override fun setLayoutManagers(gridItems: Int) {
_binding?.searchRecycler?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// fixes a bug that the search query will stay the old one when searching for multiple
// different queries in a row and navigating to the previous ones through back presses
(context as MainActivity).setQuerySilent(query)

binding.searchRecycler.layoutManager = LinearLayoutManager(requireContext())

// add the query to the history
addToHistory(query)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentSearchBinding
import com.github.libretube.databinding.FragmentSearchSuggestionsBinding
import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.extensions.TAG
import com.github.libretube.helpers.PreferenceHelper
Expand All @@ -26,8 +26,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class SearchFragment : Fragment() {
private var _binding: FragmentSearchBinding? = null
class SearchSuggestionsFragment : Fragment() {
private var _binding: FragmentSearchSuggestionsBinding? = null
private val binding get() = _binding!!
private val viewModel: SearchViewModel by activityViewModels()

Expand All @@ -41,7 +41,7 @@ class SearchFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentSearchBinding.inflate(inflater)
_binding = FragmentSearchSuggestionsBinding.inflate(inflater)
return binding.root
}

Expand Down
Loading
Loading