AndroidUtilCodeKTX icon indicating copy to clipboard operation
AndroidUtilCodeKTX copied to clipboard

我用的一些扩展

Open imtianx opened this issue 6 years ago • 3 comments

1、Intent 获取参数,以 String 为例:

fun Activity.extraString(key: String, default: String = ""): Lazy<String> = lazy {
    intent?.extras?.getString(key) ?: default
}

示例:

 private val vinCode: String by extraString(DATA_VIN_CODE)

2、Boolean 的扩展,链式调用:

object Otherwise : BooleanExt<Nothing>()
class TransferData<T>(val data: T) : BooleanExt<T>()

inline fun <T> Boolean.yes(block: () -> T): BooleanExt<T> = when {
    this -> TransferData(block.invoke())
    else -> Otherwise
}

inline fun <T> BooleanExt<T>.otherwise(block: () -> T): T = when (this) {
    is Otherwise -> block.invoke()
    is TransferData -> this.data
}

示例:

isNotEmpty().yes {

}.otherwise {

}

这样嵌套用多了代码又丑又难阅读😂

3、view 防止多次点击

  • 通过时间判断
fun View.singleClick(clickEventFun: () -> Unit) {
    this.setOnClickListener {
        if (Utils.isFastClick()) {
            clickEventFun.invoke()
        }
    }
}
  • 使用 rxjava 去重(目前已废弃),
@SuppressLint("CheckResult")
@Deprecated("memory leak")
fun View.singleClick(
    intervalDuration: Long = 1,
    unit: TimeUnit = TimeUnit.SECONDS,
    eventFun: () -> Unit
) {
    Observable
        .create(object : ObservableOnSubscribe<View> {
            lateinit var mEmitter: ObservableEmitter<View>

            init {
                [email protected] {
                    mEmitter.onNext(it)
                }
            }

            override fun subscribe(emitter: ObservableEmitter<View>) {
                mEmitter = emitter
            }
        })
        .throttleLast(intervalDuration, unit)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe {
            eventFun()
        }
}

imtianx avatar Sep 17 '19 14:09 imtianx

非常感谢,下个版本会考虑添加

lulululbj avatar Sep 19 '19 14:09 lulululbj

inline fun <reified T : Activity> Activity.startActivityExt( vararg extras: Pair<String, Any?>) {
    startActivity(Intent(this, T::class.java).apply {
        putParams(*extras)
    })
}
fun Intent.putParams(
    vararg extras: Pair<String, Any?>
): Intent {
    if (extras.isEmpty()) return this
    extras.forEach { (key, value) ->
        value ?: let {
            it.putExtra(key, it.toString())
            return@forEach
        }
        when (value) {
            is Bundle -> this.putExtra(key, value)
            is Boolean -> this.putExtra(key, value)
            is BooleanArray -> this.putExtra(key, value)
            is Byte -> this.putExtra(key, value)
            is ByteArray -> this.putExtra(key, value)
            is Char -> this.putExtra(key, value)
            is CharArray -> this.putExtra(key, value)
            is String -> this.putExtra(key, value)
            is CharSequence -> this.putExtra(key, value)
            is Double -> this.putExtra(key, value)
            is DoubleArray -> this.putExtra(key, value)
            is Float -> this.putExtra(key, value)
            is FloatArray -> this.putExtra(key, value)
            is Int -> this.putExtra(key, value)
            is IntArray -> this.putExtra(key, value)
            is Long -> this.putExtra(key, value)
            is LongArray -> this.putExtra(key, value)
            is Short -> this.putExtra(key, value)
            is ShortArray -> this.putExtra(key, value)
            is Parcelable -> this.putExtra(key, value)
            is Serializable -> this.putExtra(key, value)
            else -> {
                throw IllegalArgumentException("Not support $value type ${value.javaClass}..")
            }
        }
    }
    return this
}

demo

startActivityExt<TestActivity>(
            "1" to 2,
            "2" to "3"
        )

DaveBoy avatar Oct 31 '19 03:10 DaveBoy

@DaveBoy S我在使用过程中也发现了使用可变参数更加方便一些,谢谢反馈。

lulululbj avatar Oct 31 '19 03:10 lulululbj