From 75054780ff01d1eeeb512e4ec552048a492c1b1c Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 30 Oct 2023 04:48:55 +0530 Subject: [PATCH] refactor: Use TypedValueCompat --- .../java/com/github/libretube/extensions/DpToPx.kt | 5 +++-- .../github/libretube/ui/adapters/PlaylistAdapter.kt | 2 +- .../com/github/libretube/ui/adapters/VideosAdapter.kt | 4 ++-- .../github/libretube/ui/fragments/LibraryFragment.kt | 3 +-- .../github/libretube/ui/fragments/PlaylistFragment.kt | 4 +--- .../libretube/ui/fragments/SubscriptionsFragment.kt | 3 +-- .../libretube/ui/fragments/WatchHistoryFragment.kt | 4 +--- .../com/github/libretube/ui/views/AppNameTextView.kt | 9 ++------- .../github/libretube/ui/views/CustomExoPlayerView.kt | 11 +++-------- .../com/github/libretube/ui/views/MarkableTimeBar.kt | 2 +- 10 files changed, 16 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/github/libretube/extensions/DpToPx.kt b/app/src/main/java/com/github/libretube/extensions/DpToPx.kt index c37e29fe9..9e548b978 100644 --- a/app/src/main/java/com/github/libretube/extensions/DpToPx.kt +++ b/app/src/main/java/com/github/libretube/extensions/DpToPx.kt @@ -1,10 +1,11 @@ package com.github.libretube.extensions import android.content.res.Resources +import androidx.core.util.TypedValueCompat /** * Convert dp to pixels */ -fun Int.dpToPx(): Float { - return this * Resources.getSystem().displayMetrics.density + 0.5f +fun Float.dpToPx(): Int { + return (TypedValueCompat.dpToPx(this, Resources.getSystem().displayMetrics) + 0.5f).toInt() } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt index 3343df08a..e88df8e25 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt @@ -108,7 +108,7 @@ class PlaylistAdapter( NavigationHelper.navigateChannel(root.context, streamItem.uploaderUrl.toID()) } // add some extra padding to make it easier to click - val extraPadding = (3).dpToPx().toInt() + val extraPadding = 3f.dpToPx() videoInfo.updatePadding(top = extraPadding, bottom = extraPadding) } 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 4500248c1..c2d5ceb32 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 @@ -128,8 +128,8 @@ class VideosAdapter( // set a fixed width for better visuals root.updateLayoutParams { when (forceMode) { - ForceMode.RELATED -> width = 210.dpToPx().toInt() - ForceMode.HOME -> width = 250.dpToPx().toInt() + ForceMode.RELATED -> width = 210f.dpToPx() + ForceMode.HOME -> width = 250f.dpToPx() else -> {} } } 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 bcb78fa59..d7ebcb6aa 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 @@ -152,9 +152,8 @@ class LibraryFragment : Fragment() { private fun updateFABMargin(isMiniPlayerVisible: Boolean) { // optimize CreatePlaylistFab bottom margin if miniPlayer active - val bottomMargin = if (isMiniPlayerVisible) 64 else 16 binding.createPlaylist.updateLayoutParams { - this.bottomMargin = bottomMargin.dpToPx().toInt() + bottomMargin = (if (isMiniPlayerVisible) 64f else 16f).dpToPx() } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt index 626373e18..3d6318c3e 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt @@ -101,9 +101,7 @@ class PlaylistFragment : Fragment() { updateBookmarkRes() playerViewModel.isMiniPlayerVisible.observe(viewLifecycleOwner) { - binding.playlistRecView.updatePadding( - bottom = if (it) (64).dpToPx().toInt() else 0 - ) + binding.playlistRecView.updatePadding(bottom = if (it) 64f.dpToPx() else 0) } fetchPlaylist() diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt index 462d9ca52..9b801c72e 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt @@ -170,8 +170,7 @@ class SubscriptionsFragment : Fragment() { // otherwise the last channel would be invisible playerModel.isMiniPlayerVisible.observe(viewLifecycleOwner) { binding.subChannelsContainer.updateLayoutParams { - val newMargin = if (it) 64 else 0 - bottomMargin = newMargin.dpToPx().toInt() + bottomMargin = (if (it) 64f else 0f).dpToPx() } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt index cca4c4ac5..41b1b317d 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt @@ -52,9 +52,7 @@ class WatchHistoryFragment : Fragment() { super.onViewCreated(view, savedInstanceState) playerViewModel.isMiniPlayerVisible.observe(viewLifecycleOwner) { - _binding?.watchHistoryRecView?.updatePadding( - bottom = if (it) (64).dpToPx().toInt() else 0 - ) + _binding?.watchHistoryRecView?.updatePadding(bottom = if (it) 64f.dpToPx() else 0) } val watchHistory = runBlocking(Dispatchers.IO) { diff --git a/app/src/main/java/com/github/libretube/ui/views/AppNameTextView.kt b/app/src/main/java/com/github/libretube/ui/views/AppNameTextView.kt index 848b62c42..0ca7946f5 100644 --- a/app/src/main/java/com/github/libretube/ui/views/AppNameTextView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/AppNameTextView.kt @@ -2,8 +2,8 @@ package com.github.libretube.ui.views import android.content.Context import android.util.AttributeSet -import android.util.TypedValue import androidx.appcompat.widget.AppCompatTextView +import androidx.core.util.TypedValueCompat import com.github.libretube.helpers.ThemeHelper class AppNameTextView : AppCompatTextView { @@ -13,11 +13,6 @@ class AppNameTextView : AppCompatTextView { init { text = ThemeHelper.getStyledAppName(context) - textSize = spToPixel(10f) - } - - @Suppress("SameParameterValue") - private fun spToPixel(sp: Float): Float { - return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, resources.displayMetrics) + textSize = TypedValueCompat.spToPx(10f, resources.displayMetrics) } } 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 353e18f31..204f27439 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 @@ -551,13 +551,8 @@ open class CustomExoPlayerView( super.onConfigurationChanged(newConfig) // add a larger bottom margin to the time bar in landscape mode - val offset = when { - isFullscreen() -> 20.dpToPx() - else -> 10.dpToPx() - } - binding.progressBar.updateLayoutParams { - bottomMargin = offset.toInt() + bottomMargin = (if (isFullscreen()) 20f else 10f).dpToPx() } updateTopBarMargin() @@ -599,7 +594,7 @@ open class CustomExoPlayerView( */ fun updateTopBarMargin() { binding.topBar.updateLayoutParams { - topMargin = getTopBarMarginDp().dpToPx().toInt() + topMargin = getTopBarMarginDp().toFloat().dpToPx() } } @@ -731,6 +726,6 @@ open class CustomExoPlayerView( private const val SUBTITLE_BOTTOM_PADDING_FRACTION = 0.158f private const val ANIMATION_DURATION = 100L private const val AUTO_HIDE_CONTROLLER_DELAY = 2000L - private val LANDSCAPE_MARGIN_HORIZONTAL = (20).dpToPx().toInt() + private val LANDSCAPE_MARGIN_HORIZONTAL = 20f.dpToPx() } } diff --git a/app/src/main/java/com/github/libretube/ui/views/MarkableTimeBar.kt b/app/src/main/java/com/github/libretube/ui/views/MarkableTimeBar.kt index 2fad96046..66d34eade 100644 --- a/app/src/main/java/com/github/libretube/ui/views/MarkableTimeBar.kt +++ b/app/src/main/java/com/github/libretube/ui/views/MarkableTimeBar.kt @@ -29,7 +29,7 @@ class MarkableTimeBar( private var player: Player? = null private var length: Int = 0 - private val progressBarHeight = (2).dpToPx().toInt() + private val progressBarHeight = 2f.dpToPx() override fun onDraw(canvas: Canvas) { super.onDraw(canvas)