diff --git a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt index 89ba284da..326edefd5 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt @@ -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 = diff --git a/app/src/main/java/com/github/libretube/ui/extensions/SetWatchProgressLength.kt b/app/src/main/java/com/github/libretube/ui/extensions/SetWatchProgressLength.kt index 276f1545f..d11f979ed 100644 --- a/app/src/main/java/com/github/libretube/ui/extensions/SetWatchProgressLength.kt +++ b/app/src/main/java/com/github/libretube/ui/extensions/SetWatchProgressLength.kt @@ -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 } }) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt index 832253e9d..d61693c03 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt @@ -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 { + this.bottomMargin = bottomMargin.dpToPx().toInt() + } } private fun fetchPlaylists() { diff --git a/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt index 000a4d407..d6f35d7bb 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt @@ -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()) } } }) diff --git a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt index 074a79520..54377e6ff 100644 --- a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt @@ -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 { + 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 { 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 { + topMargin = (if (isFullscreen) 10 else 0).dpToPx().toInt() } } diff --git a/app/src/main/java/com/github/libretube/util/SeekbarPreviewListener.kt b/app/src/main/java/com/github/libretube/util/SeekbarPreviewListener.kt index f7ca39572..cad92c8a6 100644 --- a/app/src/main/java/com/github/libretube/util/SeekbarPreviewListener.kt +++ b/app/src/main/java/com/github/libretube/util/SeekbarPreviewListener.kt @@ -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 { + 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 {