Use View.updateLayoutParams() extensions.

This commit is contained in:
Isira Seneviratne 2023-01-21 06:48:24 +05:30
parent 2e0c121f58
commit 738f943a13
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.View
import android.view.ViewGroup
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -117,13 +118,13 @@ class VideosAdapter(
// Trending layout
holder.trendingRowBinding?.apply {
// set a fixed width for better visuals
val params = root.layoutParams
when (forceMode) {
ForceMode.RELATED -> params.width = (210).dpToPx().toInt()
ForceMode.HOME -> params.width = (250).dpToPx().toInt()
else -> {}
root.updateLayoutParams {
when (forceMode) {
ForceMode.RELATED -> width = 210.dpToPx().toInt()
ForceMode.HOME -> width = 250.dpToPx().toInt()
else -> {}
}
}
root.layoutParams = params
textViewTitle.text = video.title
textViewChannel.text =

View File

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

View File

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

View File

@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
@ -36,8 +37,8 @@ class CommentsSheet : ExpandedBottomSheet() {
override fun onGlobalLayout() {
binding.dragHandle.viewTreeObserver.removeOnGlobalLayoutListener(this)
// limit the recyclerview height to not cover the video
binding.commentsRV.layoutParams = binding.commentsRV.layoutParams.apply {
height = viewModel.maxHeight - (binding.dragHandle.height + (20).dpToPx().toInt())
binding.commentsRV.updateLayoutParams {
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.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleOwner
import com.github.libretube.R
import com.github.libretube.databinding.DoubleTapOverlayBinding
@ -577,10 +578,8 @@ internal class CustomExoPlayerView(
else -> 10.dpToPx()
}
binding.progressBar.let {
val params = it.layoutParams as MarginLayoutParams
params.bottomMargin = offset.toInt()
it.layoutParams = params
binding.progressBar.updateLayoutParams<MarginLayoutParams> {
bottomMargin = offset.toInt()
}
updateTopBarMargin()
@ -595,7 +594,7 @@ internal class CustomExoPlayerView(
}
listOf(binding.topBar, binding.bottomBar).forEach {
it.layoutParams = (it.layoutParams as MarginLayoutParams).apply {
it.updateLayoutParams<MarginLayoutParams> {
marginStart = newMargin
marginEnd = newMargin
}
@ -622,10 +621,8 @@ internal class CustomExoPlayerView(
private fun updateTopBarMargin() {
val isFullscreen = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE ||
playerViewModel?.isFullscreen?.value == true
binding.topBar.let {
it.layoutParams = (it.layoutParams as MarginLayoutParams).apply {
topMargin = (if (isFullscreen) 10 else 0).dpToPx().toInt()
}
binding.topBar.updateLayoutParams<MarginLayoutParams> {
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.widget.ImageView
import androidx.core.graphics.drawable.toBitmap
import androidx.core.math.MathUtils
import androidx.core.view.updateLayoutParams
import coil.request.ImageRequest
import com.github.libretube.api.obj.PreviewFrames
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
*/
private fun updatePreviewX(position: Long) {
val params = previewIv.layoutParams as MarginLayoutParams
val parentWidth = (previewIv.parent as View).width
// calculate the center-offset of the preview image view
val offset = parentWidth * (position.toFloat() / duration.toFloat()) - previewIv.width / 2
// normalize the offset to keep a minimum distance at left and right
params.marginStart = normalizeOffset(
offset.toInt(),
MIN_PADDING,
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))
previewIv.updateLayoutParams<MarginLayoutParams> {
val parentWidth = (previewIv.parent as View).width
// calculate the center-offset of the preview image view
val offset = parentWidth * (position.toFloat() / duration.toFloat()) -
previewIv.width / 2
// normalize the offset to keep a minimum distance at left and right
val maxPadding = parentWidth - MIN_PADDING - previewIv.width
marginStart = MathUtils.clamp(offset.toInt(), MIN_PADDING, maxPadding)
}
}
companion object {