You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.4 KiB
Kotlin

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* Kotlin扩展属性和扩展函数
*/
package com.common.commonlib.utils
import android.app.Activity
import android.content.res.Resources
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import java.text.DecimalFormat
/**
* Kt扩展属性判断Activity是否存活
*/
val Activity?.isAlive: Boolean
get() = !(this?.isDestroyed ?: true || this?.isFinishing ?: true)
/**
* Boolean转Visibility
*/
fun Boolean.toVisibility() = if (this) View.VISIBLE else View.GONE
/**
* Float dp2px
*/
val Float.dp: Float
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this,
Resources.getSystem().displayMetrics
)
/**
* Int dp2px
*/
val Int.dp: Float
get() = this.toFloat().dp
val decimalFormat = DecimalFormat("#0.0")
fun Float.forShowValue(): Float {
return this.forShowStr().toFloat()
}
fun Float.forShowStr(): String {
return decimalFormat.format(this)
}
fun String.getGasShowName(): String {
return getGasNickName(this)
}
private fun getGasNickName(gasType: String): String {
val string = MMKVUtils.getString("${gasType}_gas_nickname")
if (!TextUtils.isEmpty(string)) {
return string!!
}
return gasType
}
fun saveGasNickName(gasType: String, name: String) {
if (!TextUtils.isEmpty(name)) {
MMKVUtils.put("${gasType}_gas_nickname", name)
}
}