androidbroadcast / ViewBindingPropertyDelegate

Make work with Android View Binding simpler
https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719
Apache License 2.0
1.43k stars 102 forks source link

Better way to associate ViewBinding with Fragment without reflection #7

Closed shostko closed 4 years ago

shostko commented 4 years ago

I'm using very similar way to associate ViewBinding with Fragment but have slightly different way to do that without reflection.

This PR adds same possibilities to ViewBindingPropertyDelegate:

class ProfileFragment : Fragment(R.layout.profile) {
    private val viewBinding by viewBinding(ProfileBinding::bind)
}

It also contains a possibility to bind not only on root fragment view (see ProfileFragment2.kt):

class ProfileFragment : ComplexFragment(R.layout.profile) {
    private val viewBinding by viewBinding(ProfileBinding::bind, ComplexFragment::requireInternalView)
}
kirich1409 commented 4 years ago

I thought about that improvement 2 days ago. I've just checked your proposal and my own solution. The second one is better and require less code. The library already has ability to create a ViewBinding without reflection using

fun <F : Fragment, T : ViewBinding> F.viewBinding(viewBinder: (F) -> T): ViewBindingProperty<F, T>

but your proposal demonstrate that possibility to send method references will be better than lambda

shostko commented 4 years ago

Could you please share your solution? And describe the difference? (Which does not include implementing (F) -> T lambda on each fragment)

BTW of course it is possible to reduce amount of code and leave only one additional extension method:

fun <F : Fragment, T : ViewBinding> F.viewBinding(viewBinder: (View) -> T, viewFinder: (F) -> View = Fragment::requireView): ViewBindingProperty<F, T> {
    return viewBinding { viewBinder(viewFinder(this)) }
}

I just tried to follow style of code already exists in the project.

kirich1409 commented 4 years ago

You are right about my solution. I've added inline for function type to reduce number of created objects

shostko commented 4 years ago

Yeah. it is also a good idea. When are you going to publish new version with this update?

kirich1409 commented 4 years ago

On this week. I am working on improvements right now

kirich1409 commented 4 years ago

The changes are in develop branch

shostko commented 4 years ago

Thanks for pushing this code.