2020-08-15 22:05:07 +08:00
|
|
|
package com.huanchengfly.tieba.post
|
|
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.os.Build
|
|
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.annotation.ColorRes
|
2020-09-19 21:38:41 +08:00
|
|
|
import androidx.fragment.app.Fragment
|
2020-08-15 22:05:07 +08:00
|
|
|
import com.google.gson.Gson
|
|
|
|
|
import com.huanchengfly.tieba.post.utils.MD5Util
|
|
|
|
|
|
|
|
|
|
fun Float.dpToPx(): Int =
|
2020-09-19 21:38:41 +08:00
|
|
|
dpToPxFloat().toInt()
|
|
|
|
|
|
|
|
|
|
fun Float.dpToPxFloat(): Float =
|
|
|
|
|
this * BaseApplication.ScreenInfo.DENSITY + 0.5f
|
2020-08-15 22:05:07 +08:00
|
|
|
|
|
|
|
|
fun Float.spToPx(): Int =
|
2020-08-22 22:22:06 +08:00
|
|
|
(this * BaseApplication.instance.resources.displayMetrics.scaledDensity + 0.5f).toInt()
|
2020-08-15 22:05:07 +08:00
|
|
|
|
2020-08-22 22:22:06 +08:00
|
|
|
fun Float.pxToDp(): Int =
|
|
|
|
|
(this / BaseApplication.ScreenInfo.DENSITY + 0.5f).toInt()
|
2020-08-15 22:05:07 +08:00
|
|
|
|
2020-08-22 22:22:06 +08:00
|
|
|
fun Float.pxToSp(): Int =
|
|
|
|
|
(this / BaseApplication.instance.resources.displayMetrics.scaledDensity + 0.5f).toInt()
|
2020-08-15 22:05:07 +08:00
|
|
|
|
|
|
|
|
fun Int.dpToPx(): Int = this.toFloat().dpToPx()
|
|
|
|
|
|
|
|
|
|
fun Int.spToPx(): Int = this.toFloat().spToPx()
|
|
|
|
|
|
2020-08-22 22:22:06 +08:00
|
|
|
fun Int.pxToDp(): Int = this.toFloat().pxToDp()
|
|
|
|
|
|
|
|
|
|
fun Int.pxToSp(): Int = this.toFloat().pxToSp()
|
|
|
|
|
|
2020-08-15 22:05:07 +08:00
|
|
|
fun Any.toJson(): String = Gson().toJson(this)
|
|
|
|
|
|
|
|
|
|
fun String.toMD5(): String = MD5Util.toMd5(this)
|
|
|
|
|
|
|
|
|
|
fun Context.getColorCompat(@ColorRes id: Int): Int {
|
|
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
|
resources.getColor(id, theme)
|
|
|
|
|
} else {
|
|
|
|
|
resources.getColor(id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 23:21:31 +08:00
|
|
|
inline fun <reified T : Activity> Context.goToActivity() {
|
2020-08-15 22:05:07 +08:00
|
|
|
startActivity(Intent(this, T::class.java))
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 21:38:41 +08:00
|
|
|
inline fun <reified T : Activity> Context.goToActivity(pre: Intent.() -> Unit) {
|
|
|
|
|
startActivity(Intent(this, T::class.java).apply(pre))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline fun <reified T : Activity> Fragment.goToActivity() {
|
|
|
|
|
startActivity(Intent(requireContext(), T::class.java))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline fun <reified T : Activity> Fragment.goToActivity(pre: Intent.() -> Unit) {
|
|
|
|
|
startActivity(Intent(requireContext(), T::class.java).apply(pre))
|
2020-08-31 23:21:31 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-15 22:05:07 +08:00
|
|
|
fun Context.toastShort(text: String) {
|
|
|
|
|
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 11:16:42 +08:00
|
|
|
fun Context.toastShort(resId: Int) {
|
|
|
|
|
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show()
|
|
|
|
|
}
|