wada811 / ViewBinding-ktx

ViewBinding-ktx make easy to use ViewBinding.
https://medium.com/databinding-ktx
Apache License 2.0
38 stars 3 forks source link

Possible base class + api enhancements request #2

Closed AlexTrotsenko closed 3 years ago

AlexTrotsenko commented 3 years ago

I see, that https://github.com/wada811/DataBinding-ktx shows example of base class implementation in the Readme.

I believe following example might be helpful to add to the readme of viewBinding as well:

Another option would be adding such base classes to the lib itself and let be removed by the R8/Dexguard if it's not used. It could help to reduce the amount of the boilerplate code even further.

P.S. @wada811 do you think it might be possible to change the api in order to avoid this reified-related work-around? As far as I see, reified generic type is only used for no-argument method invocation and absolutely not need for the case when bind producer is explicitly passed as an argument. Thus splitting this method in two different methods should solve an issue.

wada811 commented 3 years ago

@AlexTrotsenko I updated the library's code. In ViewBinding-ktx v1.2.1, you can define base class like below.

open class BaseViewBindingFragment<T : ViewBinding>(
    @LayoutRes contentLayoutId: Int,
    bind: (View) -> T
) : Fragment(contentLayoutId) {
    protected val binding: T by viewBinding(bind)
}

Thank you.