Merge pull request #2873 from Isira-Seneviratne/updateLayoutParams

Use View.updateLayoutParams() extensions.
This commit is contained in:
Bnyro 2023-01-26 13:44:06 +01:00 committed by GitHub
commit bf7f855c7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 42 deletions

View File

@ -6,6 +6,7 @@ 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.core.view.updateLayoutParams
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -117,13 +118,13 @@ class VideosAdapter(
// Trending layout // Trending layout
holder.trendingRowBinding?.apply { holder.trendingRowBinding?.apply {
// set a fixed width for better visuals // set a fixed width for better visuals
val params = root.layoutParams root.updateLayoutParams {
when (forceMode) { when (forceMode) {
ForceMode.RELATED -> params.width = (210).dpToPx().toInt() ForceMode.RELATED -> width = 210.dpToPx().toInt()
ForceMode.HOME -> params.width = (250).dpToPx().toInt() ForceMode.HOME -> width = 250.dpToPx().toInt()
else -> {} else -> {}
}
} }
root.layoutParams = params
textViewTitle.text = video.title textViewTitle.text = video.title
textViewChannel.text = textViewChannel.text =

View File

@ -3,6 +3,7 @@ package com.github.libretube.ui.extensions
import android.view.View import android.view.View
import android.view.ViewTreeObserver import android.view.ViewTreeObserver
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.core.view.updateLayoutParams
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.awaitQuery import com.github.libretube.extensions.awaitQuery
@ -35,9 +36,9 @@ fun View?.setWatchProgressLength(videoId: String, duration: Long): Boolean {
this@setWatchProgressLength.viewTreeObserver.removeOnGlobalLayoutListener(this) this@setWatchProgressLength.viewTreeObserver.removeOnGlobalLayoutListener(this)
val fullWidth = (parent as LinearLayout).width val fullWidth = (parent as LinearLayout).width
val newWidth = fullWidth * (progress / duration.toFloat()) val newWidth = fullWidth * (progress / duration.toFloat())
val lp = view.layoutParams view.updateLayoutParams {
lp.width = newWidth.toInt() width = newWidth.toInt()
view.layoutParams = lp }
view.visibility = View.VISIBLE view.visibility = View.VISIBLE
} }
}) })

View File

@ -5,7 +5,9 @@ import android.util.Log
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 android.view.ViewGroup.MarginLayoutParams
import android.widget.Toast import android.widget.Toast
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
@ -102,9 +104,9 @@ class LibraryFragment : BaseFragment() {
private fun updateFABMargin(isMiniPlayerVisible: Boolean) { private fun updateFABMargin(isMiniPlayerVisible: Boolean) {
// optimize CreatePlaylistFab bottom margin if miniPlayer active // optimize CreatePlaylistFab bottom margin if miniPlayer active
val bottomMargin = if (isMiniPlayerVisible) 64 else 16 val bottomMargin = if (isMiniPlayerVisible) 64 else 16
val layoutParams = binding.createPlaylist.layoutParams as ViewGroup.MarginLayoutParams binding.createPlaylist.updateLayoutParams<MarginLayoutParams> {
layoutParams.bottomMargin = bottomMargin.dpToPx().toInt() this.bottomMargin = bottomMargin.dpToPx().toInt()
binding.createPlaylist.layoutParams = layoutParams }
} }
private fun fetchPlaylists() { private fun fetchPlaylists() {

View File

@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewTreeObserver import android.view.ViewTreeObserver
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R import com.github.libretube.R
@ -36,8 +37,8 @@ class CommentsSheet : ExpandedBottomSheet() {
override fun onGlobalLayout() { override fun onGlobalLayout() {
binding.dragHandle.viewTreeObserver.removeOnGlobalLayoutListener(this) binding.dragHandle.viewTreeObserver.removeOnGlobalLayoutListener(this)
// limit the recyclerview height to not cover the video // limit the recyclerview height to not cover the video
binding.commentsRV.layoutParams = binding.commentsRV.layoutParams.apply { binding.commentsRV.updateLayoutParams {
height = viewModel.maxHeight - (binding.dragHandle.height + (20).dpToPx().toInt()) height = viewModel.maxHeight - (binding.dragHandle.height + 20.dpToPx().toInt())
} }
} }
}) })

View File

@ -14,6 +14,7 @@ import android.widget.FrameLayout
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DoubleTapOverlayBinding import com.github.libretube.databinding.DoubleTapOverlayBinding
@ -577,10 +578,8 @@ internal class CustomExoPlayerView(
else -> 10.dpToPx() else -> 10.dpToPx()
} }
binding.progressBar.let { binding.progressBar.updateLayoutParams<MarginLayoutParams> {
val params = it.layoutParams as MarginLayoutParams bottomMargin = offset.toInt()
params.bottomMargin = offset.toInt()
it.layoutParams = params
} }
updateTopBarMargin() updateTopBarMargin()
@ -595,7 +594,7 @@ internal class CustomExoPlayerView(
} }
listOf(binding.topBar, binding.bottomBar).forEach { listOf(binding.topBar, binding.bottomBar).forEach {
it.layoutParams = (it.layoutParams as MarginLayoutParams).apply { it.updateLayoutParams<MarginLayoutParams> {
marginStart = newMargin marginStart = newMargin
marginEnd = newMargin marginEnd = newMargin
} }
@ -622,10 +621,8 @@ internal class CustomExoPlayerView(
private fun updateTopBarMargin() { private fun updateTopBarMargin() {
val isFullscreen = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE || val isFullscreen = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE ||
playerViewModel?.isFullscreen?.value == true playerViewModel?.isFullscreen?.value == true
binding.topBar.let { binding.topBar.updateLayoutParams<MarginLayoutParams> {
it.layoutParams = (it.layoutParams as MarginLayoutParams).apply { topMargin = (if (isFullscreen) 10 else 0).dpToPx().toInt()
topMargin = (if (isFullscreen) 10 else 0).dpToPx().toInt()
}
} }
} }

View File

@ -5,6 +5,8 @@ import android.view.View
import android.view.ViewGroup.MarginLayoutParams import android.view.ViewGroup.MarginLayoutParams
import android.widget.ImageView import android.widget.ImageView
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import androidx.core.math.MathUtils
import androidx.core.view.updateLayoutParams
import coil.request.ImageRequest import coil.request.ImageRequest
import com.github.libretube.api.obj.PreviewFrames import com.github.libretube.api.obj.PreviewFrames
import com.github.libretube.obj.PreviewFrame import com.github.libretube.obj.PreviewFrame
@ -113,25 +115,15 @@ class SeekbarPreviewListener(
* Update the offset of the preview image to fit the current scrubber position * Update the offset of the preview image to fit the current scrubber position
*/ */
private fun updatePreviewX(position: Long) { private fun updatePreviewX(position: Long) {
val params = previewIv.layoutParams as MarginLayoutParams previewIv.updateLayoutParams<MarginLayoutParams> {
val parentWidth = (previewIv.parent as View).width val parentWidth = (previewIv.parent as View).width
// calculate the center-offset of the preview image view // calculate the center-offset of the preview image view
val offset = parentWidth * (position.toFloat() / duration.toFloat()) - previewIv.width / 2 val offset = parentWidth * (position.toFloat() / duration.toFloat()) -
// normalize the offset to keep a minimum distance at left and right previewIv.width / 2
params.marginStart = normalizeOffset( // normalize the offset to keep a minimum distance at left and right
offset.toInt(), val maxPadding = parentWidth - MIN_PADDING - previewIv.width
MIN_PADDING, marginStart = MathUtils.clamp(offset.toInt(), MIN_PADDING, maxPadding)
parentWidth - MIN_PADDING - previewIv.width }
)
previewIv.layoutParams = params
}
/**
* Normalize the offset to not overflow the screen
*/
@Suppress("SameParameterValue")
private fun normalizeOffset(offset: Int, min: Int, max: Int): Int {
return maxOf(min, minOf(max, offset))
} }
companion object { companion object {