Open MoonWolf125 opened 2 years ago
I'm in between on this. I've seen other libraries use viewBinding
in this situation, but the Java standard is to write method signatures as verbs or predicates, which I like. If I were to write this as a predicate, it would be bindingViewBinding
. I'm not a huge fan of this name, especially since the object the function is acting upon is given by the generic.
I think val binding by binding<MyViewBinding>()
is okay. However, it's mixing binding as a noun and verb
I was trying to think more along the lines of following a naming paradigm Google uses for similar extension functions/factories for consistency between internal and external code. (In all honesty, I'm surprised there's not already a Jetpack library containing bind<>()
/viewBinding<>()
)
Ex:
val viewModel by viewModels<MyViewModel>()
, and
val viewModel by activityViewModels<MyViewModel>()
from the Fragment KTX Module, AndroidX ComponentActivity, or
val args: MyActivityArgs by navArgs()
val viewmodel: MainViewModel by navGraphViewModels(R.id.main)
from Jetpack Navigation
And while the ViewBinding feature name is used as a proper noun, it doubles as a verb because it's the action taking place, binding views.
My main worry is that bind/binding are too generic and not nearly self-descriptive enough even with the type-inference (especially since the extension function will be highlighted by the linter and thus our eyes will be drawn to it and not the type-inference).
Didn't know those delegate functions existed! Alright, you've convinced me!
Current:
inline fun <reified V : ViewBinding> BaseFragment.bind(): ReadOnlyProperty<Fragment, V> = bind(V::class)
val binding by bind<MainFragmentBinding>()
Proposed:
inline fun <reified V : ViewBinding> BaseFragment.viewBinding(): ReadOnlyProperty<Fragment, V> = bind(V::class)
val binding by viewBinding<MainFragmentBinding>()
bind
is rather ambiguous. Renaming toviewBinding
would be more self-descriptive.