Skip to content

Commit

Permalink
fix: #377 修复基础数据类型支持
Browse files Browse the repository at this point in the history
  • Loading branch information
liangjingkanji committed Jul 17, 2023
1 parent 9b96fd4 commit a38db67
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
6 changes: 3 additions & 3 deletions brv/src/main/java/com/drake/brv/BindingAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import com.drake.brv.animation.*
import com.drake.brv.annotaion.AnimationType
import com.drake.brv.item.*
import com.drake.brv.listener.*
import com.drake.brv.reflect.isAssignableFrom
import com.drake.brv.reflect.equalInstance
import com.drake.brv.reflect.isInstance
import com.drake.brv.utils.BRV
import com.drake.brv.utils.setDifferModels
Expand Down Expand Up @@ -189,10 +189,10 @@ open class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolde
override fun getItemViewType(position: Int): Int {
val model = getModel<Any>(position)
return typePool.firstNotNullOfOrNull {
if (it.key.isInstance(model)) it.value else null
if (it.key.equalInstance(model)) it.value else null
}?.invoke(model, position)
?: interfacePool.firstNotNullOfOrNull {
if (it.key.isAssignableFrom(model)) it.value else null
if (it.key.isInstance(model)) it.value else null
}?.invoke(model, position)
?: throw NoSuchPropertyException("Please add item model type : addType<${model.javaClass.name}>(R.layout.item)")
}
Expand Down
28 changes: 9 additions & 19 deletions brv/src/main/java/com/drake/brv/reflect/TypeList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
@file:OptIn(ExperimentalStdlibApi::class)

package com.drake.brv.reflect

import java.lang.reflect.ParameterizedType
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.javaType
import kotlin.reflect.typeOf


Expand Down Expand Up @@ -59,32 +57,24 @@ class TypeList<T> : ArrayList<T> {
* 判断类型是否相同
* 如果[other]为[TypeList]则会判断其嵌套泛型是否也相同
*/
internal fun KType.isInstance(other: Any): Boolean {
val thisClass = if (javaType is ParameterizedType) {
(javaType as ParameterizedType).rawType as Class<*>
} else {
javaType as Class<*>
}
internal fun KType.equalInstance(other: Any): Boolean {
val javaObjectType = (classifier as KClass<*>).javaObjectType
return if (other is TypeList<*>) {
thisClass.isAssignableFrom(other.javaClass) && this.arguments == other.type.arguments
javaObjectType == other::class.java && this.arguments == other.type.arguments
} else {
thisClass.isInstance(other)
javaObjectType == other::class.java
}
}

/**
* 判断类型是否相同或为其子类
* 如果[other]为[TypeList]则会判断其嵌套泛型是否也相同
*/
internal fun KType.isAssignableFrom(other: Any): Boolean {
val thisClass = if (javaType is ParameterizedType) {
(javaType as ParameterizedType).rawType as Class<*>
} else {
javaType as Class<*>
}
internal fun KType.isInstance(other: Any): Boolean {
val javaObjectType = (classifier as KClass<*>).javaObjectType
return if (other is TypeList<*>) {
thisClass.isAssignableFrom(other.javaClass) && this.arguments == other.type.arguments
javaObjectType.isInstance(other) && this.arguments == other.type.arguments
} else {
thisClass.isAssignableFrom(other.javaClass)
javaObjectType.isInstance(other)
}
}

0 comments on commit a38db67

Please sign in to comment.