2020-08-15 22:05:07 +08:00
|
|
|
package com.huanchengfly.tieba.post
|
|
|
|
|
|
2020-11-14 19:14:24 +08:00
|
|
|
import android.animation.LayoutTransition
|
2020-08-15 22:05:07 +08:00
|
|
|
import android.app.Activity
|
2022-06-10 10:38:18 +08:00
|
|
|
import android.app.PendingIntent
|
2020-08-15 22:05:07 +08:00
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
2021-01-25 21:20:49 +08:00
|
|
|
import android.content.res.ColorStateList
|
2022-04-09 20:52:33 +08:00
|
|
|
import android.content.res.Configuration
|
2022-04-09 21:24:46 +08:00
|
|
|
import android.content.res.Configuration.SCREENLAYOUT_SIZE_MASK
|
2022-06-10 10:38:18 +08:00
|
|
|
import android.os.Build
|
2021-02-18 19:20:00 +08:00
|
|
|
import android.view.View
|
2020-11-14 19:14:24 +08:00
|
|
|
import android.view.ViewGroup
|
2020-08-15 22:05:07 +08:00
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.annotation.ColorRes
|
2021-08-19 16:59:13 +08:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources
|
|
|
|
|
import androidx.core.content.ContextCompat
|
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
|
2021-08-19 16:59:13 +08:00
|
|
|
import com.google.gson.reflect.TypeToken
|
2021-01-25 21:20:49 +08:00
|
|
|
import com.huanchengfly.tieba.post.utils.GsonUtil
|
2020-08-15 22:05:07 +08:00
|
|
|
import com.huanchengfly.tieba.post.utils.MD5Util
|
|
|
|
|
|
2021-08-19 16:59:13 +08:00
|
|
|
|
2020-08-15 22:05:07 +08:00
|
|
|
fun Float.dpToPx(): Int =
|
2022-06-16 12:30:01 +08:00
|
|
|
dpToPxFloat().toInt()
|
2020-09-19 21:38:41 +08:00
|
|
|
|
|
|
|
|
fun Float.dpToPxFloat(): Float =
|
2022-06-16 12:30:01 +08:00
|
|
|
this * BaseApplication.ScreenInfo.DENSITY + 0.5f
|
2020-08-15 22:05:07 +08:00
|
|
|
|
|
|
|
|
fun Float.spToPx(): Int =
|
2022-08-02 16:20:05 +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 =
|
2022-06-16 12:30:01 +08:00
|
|
|
(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 =
|
2022-08-02 16:20:05 +08:00
|
|
|
(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()
|
|
|
|
|
|
2021-08-19 16:59:13 +08:00
|
|
|
inline fun <reified Data> String.fromJson(): Data {
|
|
|
|
|
val type = object : TypeToken<Data>() {}.type
|
|
|
|
|
return GsonUtil.getGson().fromJson(this, type)
|
|
|
|
|
}
|
2021-01-25 21:20:49 +08:00
|
|
|
|
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 {
|
2021-08-19 16:59:13 +08:00
|
|
|
return ContextCompat.getColor(this, id)
|
2020-08-15 22:05:07 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-25 21:20:49 +08:00
|
|
|
fun Context.getColorStateListCompat(id: Int): ColorStateList {
|
2021-08-19 16:59:13 +08:00
|
|
|
return AppCompatResources.getColorStateList(this, id)
|
2021-01-25 21:20:49 +08:00
|
|
|
}
|
|
|
|
|
|
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-11-14 19:14:24 +08:00
|
|
|
fun Context.toastShort(resId: Int, vararg args: Any) {
|
|
|
|
|
Toast.makeText(this, getString(resId, *args), Toast.LENGTH_SHORT).show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun ViewGroup.enableChangingLayoutTransition() {
|
|
|
|
|
this.layoutTransition = LayoutTransition()
|
|
|
|
|
this.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
|
2020-08-22 11:16:42 +08:00
|
|
|
}
|
2021-02-18 19:20:00 +08:00
|
|
|
|
|
|
|
|
fun View.getLocationInWindow(): IntArray {
|
|
|
|
|
return IntArray(2).apply { getLocationInWindow(this) }
|
2022-04-09 20:52:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val Configuration.isPortrait: Boolean
|
|
|
|
|
get() = orientation == Configuration.ORIENTATION_PORTRAIT
|
|
|
|
|
|
|
|
|
|
val Configuration.isLandscape: Boolean
|
2022-04-09 21:24:46 +08:00
|
|
|
get() = orientation == Configuration.ORIENTATION_LANDSCAPE
|
|
|
|
|
|
|
|
|
|
val Configuration.isTablet: Boolean
|
|
|
|
|
get() = (screenLayout and SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
|
|
|
|
|
|
|
|
|
|
val Context.isTablet: Boolean
|
2022-06-10 10:38:18 +08:00
|
|
|
get() = resources.configuration.isTablet
|
|
|
|
|
|
|
|
|
|
fun pendingIntentFlagMutable(): Int {
|
|
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
|
|
|
PendingIntent.FLAG_MUTABLE
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun pendingIntentFlagImmutable(): Int {
|
|
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
|
PendingIntent.FLAG_IMMUTABLE
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
}
|