refactor: Use TypedValueCompat

This commit is contained in:
Isira Seneviratne 2023-10-30 04:48:55 +05:30
parent 841a4c9cc6
commit 75054780ff
10 changed files with 16 additions and 31 deletions

View File

@ -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()
}

View File

@ -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)
}

View File

@ -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 -> {}
}
}

View File

@ -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<MarginLayoutParams> {
this.bottomMargin = bottomMargin.dpToPx().toInt()
bottomMargin = (if (isMiniPlayerVisible) 64f else 16f).dpToPx()
}
}

View File

@ -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()

View File

@ -170,8 +170,7 @@ class SubscriptionsFragment : Fragment() {
// otherwise the last channel would be invisible
playerModel.isMiniPlayerVisible.observe(viewLifecycleOwner) {
binding.subChannelsContainer.updateLayoutParams<MarginLayoutParams> {
val newMargin = if (it) 64 else 0
bottomMargin = newMargin.dpToPx().toInt()
bottomMargin = (if (it) 64f else 0f).dpToPx()
}
}

View File

@ -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) {

View File

@ -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)
}
}

View File

@ -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<MarginLayoutParams> {
bottomMargin = offset.toInt()
bottomMargin = (if (isFullscreen()) 20f else 10f).dpToPx()
}
updateTopBarMargin()
@ -599,7 +594,7 @@ open class CustomExoPlayerView(
*/
fun updateTopBarMargin() {
binding.topBar.updateLayoutParams<MarginLayoutParams> {
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()
}
}

View File

@ -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)