/** * 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) } }