Kotlin专题Kotlin禅与计算机程序设计艺术

AndroidKt - 写了一个用于获取Intent数据的类

2019-06-23  本文已影响0人  Cosecant

背景说明
为了便于书写复杂的intent说句获取,因此编写了一个用户取参数的函数,当前谷歌官方已经提供了传参的函数bundleOf,但是并未提供相关取参数的类,因此花了一定时间对这个功能进行了整合并实验。若有不足,请各位大佬多多指教,谢谢!

“ 人狠话不多,社会我余哥”,先上代码

声明的几个方法:

/**
 * 获取Intent中的参数
 * @param clazz 参数所属类型,如果是ArrayList, List, MutableList, Collection,则需要填写componentClazz参数
 * @param defaultValue 默认值,部分定义的数据类型需要必传默认参数:Boolean, Double, Float, Long, Int, Short, Char, Byte
 * @param componentClazz 数据类型中子项的数据类型,如果clazz为释义中提及的类型,则此参数为必选项
 */
@Suppress("unchecked_cast")
fun <T> Intent.getArgument(
    clazz: Class<T>,
    argumentName: String,
    defaultValue: Any? = null,
    componentClazz: Class<*>? = null
): Any? = argumentIsExists(argumentName).run {
    when {
        clazz == Bundle::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getBundleExtra(argumentName)
                is Bundle -> extras?.getBundle(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Bundle

        clazz == CharSequence::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getCharSequenceExtra(argumentName)
                is Bundle -> extras?.getCharSequence(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? CharSequence

        clazz == String::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getStringExtra(argumentName)
                is Bundle -> extras?.getString(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? String

        clazz == Boolean::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getBooleanExtra(argumentName, (defaultValue as? Boolean) ?: false)
                is Bundle -> extras?.getBoolean(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Boolean

        clazz == Double::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getDoubleExtra(argumentName, (defaultValue as? Double) ?: 0.0)
                is Bundle -> extras?.getDouble(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Double

        clazz == Float::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getFloatExtra(argumentName, (defaultValue as? Float) ?: 0.0f)
                is Bundle -> extras?.getFloat(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Float

        clazz == Long::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getLongExtra(argumentName, (defaultValue as? Long) ?: 0L)
                is Bundle -> extras?.getLong(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Long

        clazz == Int::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getIntExtra(argumentName, (defaultValue as? Int) ?: 0)
                is Bundle -> extras?.getInt(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Int

        clazz == Short::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getShortExtra(argumentName, (defaultValue as? Short) ?: 0)
                is Bundle -> extras?.getShort(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Short

        clazz == Char::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getCharExtra(argumentName, (defaultValue as? Char) ?: 0.toChar())
                is Bundle -> extras?.getChar(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Char

        clazz == Byte::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getByteExtra(argumentName, (defaultValue as? Byte) ?: 0.toByte())
                is Bundle -> extras?.getByte(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Byte

        clazz == DoubleArray::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getDoubleArrayExtra(argumentName)
                is Bundle -> extras?.getDoubleArray(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? DoubleArray

        clazz == FloatArray::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getFloatArrayExtra(argumentName)
                is Bundle -> extras?.getFloatArray(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? FloatArray

        clazz == LongArray::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getLongArrayExtra(argumentName)
                is Bundle -> extras?.getLongArray(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? LongArray

        clazz == IntArray::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getIntArrayExtra(argumentName)
                is Bundle -> extras?.getIntArray(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? IntArray

        clazz == ShortArray::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getShortArrayExtra(argumentName)
                is Bundle -> extras?.getShortArray(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? ShortArray

        clazz == CharArray::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getCharArrayExtra(argumentName)
                is Bundle -> extras?.getCharArray(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? CharArray

        clazz == ByteArray::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getByteArrayExtra(argumentName)
                is Bundle -> extras?.getByteArray(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? ByteArray

        clazz == BooleanArray::class.java -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getBooleanArrayExtra(argumentName)
                is Bundle -> extras?.getBooleanArray(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? BooleanArray

        Serializable::class.java.isAssignableFrom(clazz) -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getSerializableExtra(argumentName)
                is Bundle -> extras?.getSerializable(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Serializable

        Parcelable::class.java.isAssignableFrom(clazz) -> (when (isExists) {
            true -> when (relativeObj) {
                is Intent -> getParcelableExtra<Parcelable>(argumentName)
                is Bundle -> extras?.getParcelable<Parcelable>(argumentName)
                else -> null
            }
            else -> null
        }) ?: defaultValue as? Parcelable

        clazz == ArrayList::class.java || clazz == List::class.java || clazz == MutableList::class.java || clazz == Collection::class.java -> when (isExists) {
            true -> when {
                componentClazz == Int::class.java -> (when (relativeObj) {
                    is Intent -> getIntegerArrayListExtra(argumentName)
                    is Bundle -> extras?.getIntegerArrayList(argumentName)
                    else -> null
                }) ?: defaultValue as? ArrayList<Int>
                componentClazz == String::class.java -> (when (relativeObj) {
                    is Intent -> getStringArrayListExtra(argumentName)
                    is Bundle -> extras?.getStringArrayList(argumentName)
                    else -> null
                }) ?: defaultValue as? ArrayList<String>
                componentClazz == CharSequence::class.java -> (when (relativeObj) {
                    is Intent -> getCharSequenceArrayListExtra(argumentName)
                    is Bundle -> extras?.getCharSequenceArrayList(argumentName)
                    else -> null
                }) ?: defaultValue as? ArrayList<CharSequence>
                componentClazz != null && Parcelable::class.java.isAssignableFrom(componentClazz) -> (
                        when (relativeObj) {
                            is Intent -> getParcelableArrayListExtra<Parcelable>(argumentName)
                            is Bundle -> extras?.getParcelableArrayList(argumentName)
                            else -> null
                        }) ?: defaultValue as? ArrayList<Parcelable>
                else -> null
            }
            else -> throw Throwable("错误:获取参数[$argumentName]失败, 原因:参数clazz[${clazz.simpleName}],componentClazz[${componentClazz?.name}]为不支持的数据类型")
        }
        else -> throw Throwable("错误:获取参数[$argumentName]失败,原因:参数clazz[${clazz.simpleName}]为不支持的数据类型")
    }
}

/**
 * 判断参数是否存在
 * @param argumentName 参数名称
 */
private fun Intent.argumentIsExists(argumentName: String): DataInfoEntity = when {
    hasExtra(argumentName) -> DataInfoEntity(true, this)
    else -> when {
        extras?.containsKey(argumentName) == true -> DataInfoEntity(true, extras)
        else -> DataInfoEntity(false, null)
    }
}

/**
 * 数据信息实体类
 * @param isExists 是否存在
 * @param relativeObj 相关联的对象
 */
private class DataInfoEntity(
    var isExists: Boolean,
    var relativeObj: Any?
)

使用示例,列出部分

intent?.getArgument(String::class.java, "title") as? String
intent?.getArgument(Boolean::class.java, "isMultiSpecification", false) as? Boolean ?: false
 @Suppress("unchecked_cast")
        intent?.getArgument(
            ArrayList::class.java,
            "categoryList",
            componentClazz = GoodsCategoryInfoEntity::class.java
        ) as? ArrayList<GoodsCategoryInfoEntity>

使用声明
项目中若使用请备注来源地址,谢谢!
文章转载请声明来源地址,谢谢!

上一篇 下一篇

猜你喜欢

热点阅读