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

40 lines
1.1 KiB
Kotlin
Raw Normal View History

2021-07-09 05:18:48 +02:00
package org.thoughtcrime.securesms.util
2020-05-12 03:46:11 +02:00
import android.content.res.Resources
import android.os.Build
import androidx.annotation.ColorRes
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.max
2020-05-12 03:46:11 +02:00
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 {
2021-06-04 05:15:43 +02:00
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 {
2020-05-12 03:46:11 +02:00
val scale = resources.displayMetrics.density
2021-06-04 05:15:43 +02:00
return (px / scale)
2020-05-12 03:46:11 +02:00
}
val RecyclerView.isScrolledToBottom: Boolean
get() = computeVerticalScrollOffset().coerceAtLeast(0) +
computeVerticalScrollExtent() +
toPx(50, resources) >= computeVerticalScrollRange()