session-android/app/src/main/java/org/thoughtcrime/securesms/util/GeneralUtilities.kt

40 lines
1.1 KiB
Kotlin

package org.thoughtcrime.securesms.util
import android.content.res.Resources
import android.os.Build
import androidx.annotation.ColorRes
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.max
import kotlin.math.roundToInt
fun Resources.getColorWithID(@ColorRes id: Int, theme: Resources.Theme?): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getColor(id, theme)
} else {
@Suppress("DEPRECATION") getColor(id)
}
}
fun toPx(dp: Int, resources: Resources): Int {
return toPx(dp.toFloat(), resources).roundToInt()
}
fun toPx(dp: Float, resources: Resources): Float {
val scale = resources.displayMetrics.density
return (dp * scale)
}
fun toDp(px: Int, resources: Resources): Int {
return toDp(px.toFloat(), resources).roundToInt()
}
fun toDp(px: Float, resources: Resources): Float {
val scale = resources.displayMetrics.density
return (px / scale)
}
val RecyclerView.isScrolledToBottom: Boolean
get() = computeVerticalScrollOffset().coerceAtLeast(0) +
computeVerticalScrollExtent() +
toPx(50, resources) >= computeVerticalScrollRange()