Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Make inflateBinding as a default impl (#376)
Browse files Browse the repository at this point in the history
Follow up 90cb05d.
  • Loading branch information
Goooler authored Apr 13, 2023
1 parent 3c488be commit 224b8d1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.blankj.utilcode.util.AdaptScreenUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ScreenUtils
import io.goooler.demoapp.base.core.BaseActivity
import io.goooler.demoapp.common.base.binding.IBinding.Companion.inflateBinding

abstract class BaseBindingActivity<VB : ViewDataBinding> : BaseActivity(), IBinding<VB> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import io.goooler.demoapp.base.core.BaseDialogFragment
import io.goooler.demoapp.common.base.binding.IBinding.Companion.inflateBinding

abstract class BaseBindingDialogFragment<VB : ViewDataBinding> :
BaseDialogFragment(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import io.goooler.demoapp.base.core.BaseFragment
import io.goooler.demoapp.common.base.binding.IBinding.Companion.inflateBinding

abstract class BaseBindingFragment<VB : ViewDataBinding> :
BaseFragment(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,28 @@ import java.lang.reflect.ParameterizedType
internal sealed interface IBinding<VB : ViewDataBinding> {
val binding: VB

companion object {
@Suppress("UNCHECKED_CAST")
internal fun <T : ViewBinding> IBinding<*>.inflateBinding(inflater: LayoutInflater): T {
var method: Method?
var clazz: Class<*> = javaClass
while (clazz.superclass != null) {
method = clazz.filterBindingMethod()
if (method == null) {
clazz = clazz.superclass
} else {
return method.invoke(null, inflater) as T
}
fun <T : ViewBinding> inflateBinding(inflater: LayoutInflater): T {
var method: Method?
var clazz: Class<*> = javaClass
while (clazz.superclass != null) {
method = clazz.filterBindingMethod()
if (method == null) {
clazz = clazz.superclass
} else {
@Suppress("UNCHECKED_CAST")
return method.invoke(null, inflater) as T
}
error("No Binding type argument found.")
}
error("No Binding type argument found.")
}

private fun Class<*>.filterBindingMethod(): Method? {
return (genericSuperclass as? ParameterizedType)?.actualTypeArguments
?.asSequence()
?.filterIsInstance<Class<*>>()
?.firstOrNull { it.simpleName.endsWith("Binding") }
?.getDeclaredMethod("inflate", LayoutInflater::class.java)
?.also { it.isAccessible = true }
}
private fun Class<*>.filterBindingMethod(): Method? {
return (genericSuperclass as? ParameterizedType)?.actualTypeArguments
?.asSequence()
?.filterIsInstance<Class<*>>()
?.firstOrNull { it.simpleName.endsWith("Binding") }
?.getDeclaredMethod("inflate", LayoutInflater::class.java)
?.also { it.isAccessible = true }
}
}

Expand Down

0 comments on commit 224b8d1

Please sign in to comment.