Skip to content

Commit

Permalink
Use TypedValueCompat (#5524)
Browse files Browse the repository at this point in the history
* Use TypedValueCompat

* Use TypedValueCompat in ChangelogFragment

* Added Resources extensions

* Update pixel extensions to accept Resources instead of Context

* Added more Resources extensions

* Make Resources pixel extensions accept Number
  • Loading branch information
Isira-Seneviratne committed Mar 12, 2024
1 parent ace1fbd commit 4cfb2a5
Show file tree
Hide file tree
Showing 27 changed files with 79 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ class AddressNumberInputViewController(
val formWidth = ctx.resources.getDimension(R.dimen.quest_form_width).toInt().takeIf { it > 0 }
?: ctx.resources.displayMetrics.widthPixels
// form width minus 250dp, but max. 150dp
editText.maxWidth = (formWidth - ctx.dpToPx(250).toInt())
.coerceAtMost(ctx.dpToPx(150).toInt())
.coerceAtLeast(ctx.dpToPx(56).toInt())
editText.maxWidth = (formWidth - ctx.resources.dpToPx(250).toInt())
.coerceAtMost(ctx.resources.dpToPx(150).toInt())
.coerceAtLeast(ctx.resources.dpToPx(56).toInt())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import de.westnordost.streetcomplete.osm.street_parking.ParkingPosition.PAINTED_
import de.westnordost.streetcomplete.osm.street_parking.ParkingPosition.STAGGERED_HALF_ON_STREET
import de.westnordost.streetcomplete.osm.street_parking.ParkingPosition.STAGGERED_ON_STREET
import de.westnordost.streetcomplete.osm.street_parking.ParkingPosition.STREET_SIDE
import de.westnordost.streetcomplete.util.ktx.dpToPx
import de.westnordost.streetcomplete.util.ktx.isApril1st
import kotlin.math.ceil
import kotlin.math.round
import kotlin.math.roundToInt
import kotlin.random.Random

/** Drawable that displays parking cars in the given orientation and position in the given size */
Expand All @@ -40,8 +41,8 @@ class StreetParkingDrawable(
@Deprecated("Deprecated in Java")
override fun getOpacity() = PixelFormat.TRANSLUCENT

override fun getIntrinsicWidth(): Int = round(width * context.resources.displayMetrics.density).toInt()
override fun getIntrinsicHeight(): Int = round(height * context.resources.displayMetrics.density).toInt()
override fun getIntrinsicWidth() = context.resources.dpToPx(width).roundToInt()
override fun getIntrinsicHeight() = context.resources.dpToPx(height).roundToInt()

/** Y-Offset in percent (0..<1) */
var phase: Float = 0f
Expand Down Expand Up @@ -73,7 +74,8 @@ class StreetParkingDrawable(
// drawing the street background
if (backgroundResId != null) {
val background = context.getDrawable(backgroundResId)!!
val backgroundHeight = (background.intrinsicHeight.toDouble() / background.intrinsicWidth * width).toInt()
val backgroundHeight =
(background.intrinsicHeight.toDouble() / background.intrinsicWidth * width).toInt()
val offsetY = (phase * height).toInt()
for (i in 0 until repeats) {
val y = (i * height).toInt() + offsetY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ class AddressOverlayForm : AbstractOverlayForm(), IsMapPositionAware {
private fun checkCurrentCursorPosition() {
val buildings = buildings ?: return
val metersPerPixel = metersPerPixel ?: return
val maxDistance = metersPerPixel * requireContext().dpToPx(12)
val snapToVertexDistance = metersPerPixel * requireContext().dpToPx(8)
val maxDistance = metersPerPixel * resources.dpToPx(12)
val snapToVertexDistance = metersPerPixel * resources.dpToPx(8)
positionOnWay = geometry.center.getPositionOnWays(buildings, maxDistance, snapToVertexDistance)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ class LaneNarrowingTrafficCalmingForm :
private fun checkCurrentCursorPosition() {
val roads = roads ?: return
val metersPerPixel = metersPerPixel ?: return
val maxDistance = metersPerPixel * requireContext().dpToPx(24)
val snapToVertexDistance = metersPerPixel * requireContext().dpToPx(12)
val maxDistance = metersPerPixel * resources.dpToPx(24)
val snapToVertexDistance = metersPerPixel * resources.dpToPx(12)
positionOnWay = geometry.center.getPositionOnWays(roads, maxDistance, snapToVertexDistance)
checkIsFormComplete()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import de.westnordost.streetcomplete.screens.HasTitle
import de.westnordost.streetcomplete.screens.TwoPaneDetailFragment
import de.westnordost.streetcomplete.util.ktx.getRawTextFile
import de.westnordost.streetcomplete.util.ktx.indicesOf
import de.westnordost.streetcomplete.util.ktx.pxToDp
import de.westnordost.streetcomplete.util.ktx.pxToSp
import de.westnordost.streetcomplete.util.ktx.viewLifecycleScope
import de.westnordost.streetcomplete.util.viewBinding
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -82,11 +84,9 @@ private suspend fun readChangelog(resources: Resources): String = withContext(Di
private fun Resources.getHexColor(@ColorRes resId: Int) =
String.format("#%06X", 0xffffff and getColor(resId))

private fun Resources.getDimensionInSp(@DimenRes resId: Int) =
(getDimension(resId) / displayMetrics.scaledDensity).roundToInt()
private fun Resources.getDimensionInSp(@DimenRes resId: Int) = pxToSp(getDimension(resId)).roundToInt()

private fun Resources.getDimensionInDp(@DimenRes resId: Int) =
(getDimension(resId) / displayMetrics.density).roundToInt()
private fun Resources.getDimensionInDp(@DimenRes resId: Int) = pxToDp(getDimension(resId)).roundToInt()

private fun WebView.setHtmlFromString(body: String) {
val textColor = resources.getHexColor(R.color.text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ class MainFragment :
val offset = view?.getLocationInWindow() ?: return
val startPos = mapFragment?.getPointOf(position) ?: return

val size = ctx.dpToPx(42).toInt()
val size = ctx.resources.dpToPx(42).toInt()
startPos.x += offset.x - size / 2f
startPos.y += offset.y - size * 1.5f

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class PointerPinView @JvmOverloads constructor(
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val desiredSize = context.dpToPx(DEFAULT_SIZE).toInt()
val desiredSize = resources.dpToPx(DEFAULT_SIZE).toInt()
val width = reconcileSize(desiredSize, widthMeasureSpec)
val height = reconcileSize(desiredSize, heightMeasureSpec)
setMeasuredDimension(width, height)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.graphics.PixelFormat
import android.graphics.PointF
import android.graphics.drawable.Drawable
import androidx.core.graphics.minus
import de.westnordost.streetcomplete.util.ktx.dpToPx
import de.westnordost.streetcomplete.util.ktx.translate
import kotlin.math.PI
import kotlin.math.atan2
Expand All @@ -30,7 +31,7 @@ class ArrowDrawable(private val resources: Resources) : Drawable() {

private val paint = Paint().also {
it.isAntiAlias = true
it.strokeWidth = 6 * resources.displayMetrics.density
it.strokeWidth = resources.dpToPx(6)
it.strokeCap = Paint.Cap.ROUND
}

Expand All @@ -50,7 +51,7 @@ class ArrowDrawable(private val resources: Resources) : Drawable() {

canvas.drawLine(start.x, start.y, end.x, end.y, paint)
// draw arrow
val arrowHeadSize = 14 * resources.displayMetrics.density
val arrowHeadSize = resources.dpToPx(14)
val arrowHeadLineEnd1 = end.translate(arrowHeadSize, angle + PI * 3 / 4)
val arrowHeardLineEnd2 = end.translate(arrowHeadSize, angle - PI * 3 / 4)
canvas.drawLine(end.x, end.y, arrowHeadLineEnd1.x, arrowHeadLineEnd1.y, paint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class OverlaysButtonFragment : Fragment(R.layout.fragment_overlays_button) {
popupWindow.dismiss()
}
popupWindow.anchorView = view
popupWindow.width = requireContext().dpToPx(240).toInt()
popupWindow.width = resources.dpToPx(240).toInt()
popupWindow.show()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class MainMapFragment : LocationAwareMapFragment(), ShowsGeometryMarkers {

val clickPos = controller?.screenPositionToLatLon(PointF(x, y)) ?: return

val fingerRadius = context.dpToPx(CLICK_AREA_SIZE_IN_DP) / 2
val fingerRadius = context.resources.dpToPx(CLICK_AREA_SIZE_IN_DP) / 2
val fingerEdgeClickPos = controller?.screenPositionToLatLon(PointF(x + fingerRadius, y)) ?: return
val fingerRadiusInMeters = clickPos.distanceTo(fingerEdgeClickPos)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ class TangramIconsSpriteSheet(

private fun createSpritesheet(): String {
val iconResIds = icons.toSortedSet()
val iconSize = context.dpToPx(26).toInt()
val borderWidth = context.dpToPx(3).toInt()
val safePadding = context.dpToPx(2).toInt()
val iconSize = context.resources.dpToPx(26).toInt()
val borderWidth = context.resources.dpToPx(3).toInt()
val safePadding = context.resources.dpToPx(2).toInt()
val size = iconSize + borderWidth * 2 + safePadding

val spriteSheetEntries: MutableList<String> = ArrayList(iconResIds.size)
val sheetSideLength = ceil(sqrt(iconResIds.size.toDouble())).toInt()
val spriteSheet = Bitmap.createBitmap(size * sheetSideLength, size * sheetSideLength, Bitmap.Config.ARGB_8888)
val spriteSheet = Bitmap.createBitmap(
size * sheetSideLength,
size * sheetSideLength,
Bitmap.Config.ARGB_8888
)

val canvas = Canvas(spriteSheet)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ class CurrentLocationMapComponent(ctx: Context, private val ctrl: KtMapControlle
init {
val dotImg = ctx.resources.getBitmapDrawable(if (isApril1st()) R.drawable.location_nyan else R.drawable.location_dot)
val dotSize = PointF(
ctx.pxToDp(dotImg.bitmap.width),
ctx.pxToDp(dotImg.bitmap.height)
ctx.resources.pxToDp(dotImg.bitmap.width),
ctx.resources.pxToDp(dotImg.bitmap.height)
)

val directionImg = ctx.resources.getBitmapDrawable(R.drawable.location_direction)
directionMarkerSize = PointF(
ctx.pxToDp(directionImg.bitmap.width),
ctx.pxToDp(directionImg.bitmap.height)
ctx.resources.pxToDp(directionImg.bitmap.width),
ctx.resources.pxToDp(directionImg.bitmap.height)
)

val accuracyImg = ctx.resources.getBitmapDrawable(R.drawable.accuracy_circle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import de.westnordost.streetcomplete.screens.main.map.tangram.KtMapController
import de.westnordost.streetcomplete.screens.main.map.tangram.Marker
import de.westnordost.streetcomplete.screens.main.map.tangram.toTangramGeometry
import de.westnordost.streetcomplete.util.ktx.getBitmapDrawable
import de.westnordost.streetcomplete.util.ktx.pxToDp
import de.westnordost.streetcomplete.util.math.centerPointOfPolyline

/** Manages putting some generic geometry markers with an optional drawable on the map. I.e. to
Expand Down Expand Up @@ -42,7 +43,7 @@ class GeometryMarkersMapComponent(private val resources: Resources, private val
if (drawableResId != null) {
val drawable = getBitmapDrawable(drawableResId)
marker.setDrawable(drawable)
iconSize = (drawable.bitmap.width / resources.displayMetrics.density).toInt()
iconSize = resources.pxToDp(drawable.bitmap.width).toInt()
color = pointColorOpaque
} else {
iconSize = pointSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class OsmUnreadMessagesFragment : DialogFragment(R.layout.fragment_unread_osm_me
speechbubbleContentContainer.visibility = View.VISIBLE
speechbubbleContentContainer.scaleX = 0.8f
speechbubbleContentContainer.scaleY = 0.8f
speechbubbleContentContainer.translationY = ctx.dpToPx(140)
speechbubbleContentContainer.translationY = ctx.resources.dpToPx(140)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
(mailOpenImageView.drawable as? AnimatedVectorDrawable)?.reset()
Expand All @@ -77,8 +77,8 @@ class OsmUnreadMessagesFragment : DialogFragment(R.layout.fragment_unread_osm_me
binding.mailContainer.rotation = -40f
binding.mailContainer.rotationY = -45f
binding.mailContainer.alpha = 0.2f
binding.mailContainer.translationX = ctx.dpToPx(-400)
binding.mailContainer.translationY = ctx.dpToPx(60)
binding.mailContainer.translationX = ctx.resources.dpToPx(-400)
binding.mailContainer.translationY = ctx.resources.dpToPx(60)
binding.mailContainer.animate().run {
duration = 400
startDelay = 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import androidx.core.graphics.drawable.updateBounds
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.overlays.Overlay
import de.westnordost.streetcomplete.util.ktx.dpToPx
Expand All @@ -26,9 +27,10 @@ class OverlaySelectionAdapter(private val overlays: List<Overlay>) : BaseAdapter
val textView = view as TextView
textView.setText(overlay?.title ?: R.string.overlay_none)
val icon = context.getDrawable(overlay?.icon ?: R.drawable.space_24dp)
icon?.setBounds(0, 0, context.dpToPx(38).toInt(), context.dpToPx(38).toInt())
val bound = context.resources.dpToPx(38).toInt()
icon?.updateBounds(right = bound, bottom = bound)
textView.setCompoundDrawables(icon, null, null, null)
textView.compoundDrawablePadding = context.dpToPx(8).toInt()
textView.compoundDrawablePadding = context.resources.dpToPx(8).toInt()
return textView
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ class OverlaysTutorialFragment : Fragment(R.layout.fragment_overlays_tutorial) {
binding.paintRollerView.rotation = -15f
binding.paintRollerView.scaleX = 0.1f
binding.paintRollerView.scaleY = 0.1f
binding.paintRollerView.translationX = ctx.dpToPx(132)
binding.paintRollerView.translationY = ctx.dpToPx(66)
binding.paintRollerView.translationX = ctx.resources.dpToPx(132)
binding.paintRollerView.translationY = ctx.resources.dpToPx(66)

delay(300)

Expand Down Expand Up @@ -180,8 +180,8 @@ class OverlaysTutorialFragment : Fragment(R.layout.fragment_overlays_tutorial) {
.scaleX(2f)
.scaleY(2f)
.rotation(-15f)
.translationX(ctx.dpToPx(+60))
.translationY(ctx.dpToPx(-120))
.translationX(ctx.resources.dpToPx(+60))
.translationY(ctx.resources.dpToPx(-120))
.start()

binding.overlaySelectedImageView.isInvisible = false
Expand All @@ -200,7 +200,7 @@ class OverlaysTutorialFragment : Fragment(R.layout.fragment_overlays_tutorial) {
}

private fun appearText(view: View) {
view.translationY = view.context.pxToDp(-100)
view.translationY = view.resources.pxToDp(-100)
view.animate()
.withStartAction { view.visibility = View.VISIBLE }
.setDuration(300)
Expand All @@ -213,7 +213,7 @@ class OverlaysTutorialFragment : Fragment(R.layout.fragment_overlays_tutorial) {
view.animate()
.setDuration(300)
.alpha(0f)
.translationY(view.context.pxToDp(100))
.translationY(view.resources.pxToDp(100))
.withEndAction { view.visibility = View.GONE }
.start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TutorialFragment : Fragment(R.layout.fragment_tutorial) {
.start()

// map zooms in and tilts
val mapTranslate = ctx.dpToPx(-50)
val mapTranslate = ctx.resources.dpToPx(-50)
val mapRotate = 50f
val mapScale = 1.5f

Expand Down Expand Up @@ -138,10 +138,9 @@ class TutorialFragment : Fragment(R.layout.fragment_tutorial) {

// quest pins fall into place
listOf(binding.questPin1, binding.questPin2, binding.questPin3).forEach { pin ->

delay(400)

pin.translationY = ctx.pxToDp(-200)
pin.translationY = ctx.resources.pxToDp(-200)
pin.animate()
.setInterpolator(BounceInterpolator())
.setDuration(400)
Expand Down Expand Up @@ -188,7 +187,7 @@ class TutorialFragment : Fragment(R.layout.fragment_tutorial) {
}

private fun appearText(view: View) {
view.translationY = view.context.pxToDp(-100)
view.translationY = view.resources.pxToDp(-100)
view.animate()
.withStartAction { view.visibility = View.VISIBLE }
.setDuration(300)
Expand All @@ -201,7 +200,7 @@ class TutorialFragment : Fragment(R.layout.fragment_tutorial) {
view.animate()
.setDuration(300)
.alpha(0f)
.translationY(view.context.pxToDp(100))
.translationY(view.resources.pxToDp(100))
.withEndAction { view.visibility = View.GONE }
.start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AchievementsFragment : Fragment(R.layout.fragment_achievements) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val ctx = requireContext()
val minCellWidth = ctx.dpToPx(144)
val minCellWidth = ctx.resources.dpToPx(144)
val itemSpacing = ctx.resources.getDimensionPixelSize(R.dimen.achievements_item_margin)

viewLifecycleScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LinksFragment : Fragment(R.layout.fragment_links) {

binding.emptyText.visibility = View.GONE

val viewWidth = ctx.pxToDp(view.width)
val viewWidth = ctx.resources.pxToDp(view.width)
val spanCount = (viewWidth / minCellWidth).toInt()

val links = withContext(Dispatchers.IO) { achievementsSource.getLinks() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class ProfileFragment : Fragment(R.layout.fragment_profile) {
binding.datesActiveView.setImageDrawable(DatesActiveDrawable(
datesActive.toSet(),
range,
context.dpToPx(18),
context.dpToPx(2),
context.dpToPx(4),
context.resources.dpToPx(18),
context.resources.dpToPx(2),
context.resources.dpToPx(4),
context.resources.getColor(R.color.hint_text)
))
}
Expand Down Expand Up @@ -211,7 +211,7 @@ class ProfileFragment : Fragment(R.layout.fragment_profile) {
anim.duration = 3000
anim.addUpdateListener { block(it.animatedValue as Int) }
val p = view.getLocationInWindow()
anim.startDelay = max(0, view.context.pxToDp(p.y).toLong() * 12 - 2000)
anim.startDelay = max(0, view.resources.pxToDp(p.y).toLong() * 12 - 2000)
anim.interpolator = AccelerateDecelerateInterpolator()
anim.start()
animations.add(anim)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class StatisticsByCountryFragment : Fragment(R.layout.fragment_statistics_ball_p
countryBubbleView.id = View.generateViewId()
countryBubbleView.layoutParams = ViewGroup.LayoutParams(240, 240)
countryBubbleView.countryCode = countryCode
countryBubbleView.elevation = ctx.dpToPx(6)
countryBubbleView.elevation = ctx.resources.dpToPx(6)
countryBubbleView.setOnClickListener { v ->
listener?.onClickedCountryFlag(countryCode, solvedCount, rank, v)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class StatisticsByEditTypeFragment : Fragment(R.layout.fragment_statistics_ball_
val clickableContainer = FrameLayout(ctx)
clickableContainer.layoutParams = ViewGroup.LayoutParams(256, 256)
clickableContainer.foreground = requireContext().getDrawable(R.drawable.round_pressed)
clickableContainer.elevation = ctx.dpToPx(6)
clickableContainer.elevation = ctx.resources.dpToPx(6)
clickableContainer.outlineProvider = CircularOutlineProvider
clickableContainer.addView(questView)
clickableContainer.setOnClickListener { v ->
Expand Down
Loading

0 comments on commit 4cfb2a5

Please sign in to comment.