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

Make inflateBinding as a default impl #376

Merged
merged 3 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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