Is it safe to use `Resources.getSystem()` to obtai...
# android
r
Is it safe to use
Resources.getSystem()
to obtain the density for dp to px conversions? Could density “config change”?
Copy code
val Int.dp: Int
    get() = (this / Resources.getSystem().displayMetrics.density).toInt()

val Int.px: Int
    get() = (this * Resources.getSystem().displayMetrics.density).toInt()
r
You are better off passing in resources or context.
that way you ensure this is called in the appropriate place
also I would do this: TypedValue.applyDimension
r
Back to the old ways then. Just found out about
Resources.getSystem()
, it makes the extensions so clean…
r
realistically you are fine
m
imho, if you use
Resources.getSystem()
inside a class the risk is that you can’t create unit test on it, because you are using a resource statically. This is my opinion in terms of testability.
🙌 1