API of `VisibilityProvider` is confusing when used in Kotlin
joshfriend opened this issue · 0 comments
joshfriend commented
When using Java, the API is pretty much self explanatory:
ItemDecoration dividerDecoration = HorizontalDividerItemDecoration.Builder(this)
.sizeResId(R.dimen.item_divider_height)
.visibilityProvider(new VisibilityProvider() {
@Override
boolean shouldHideDivider(int position, RecyclerView parent) {
// With the context of having the method name shown above, this is
// obviously wrong.
return true;
}
}
.build();
However, because VisibilityProvider
is a SAM-type, Kotlin allows you to use a trailing closure, so the name of the method isn't shown anywhere. In this case, a user's first instinct might be to return true
to show the divider and false
to hide it (like I did), which is backwards:
val dividerDecoration = HorizontalDividerItemDecoration.Builder(this)
.sizeResId(R.dimen.item_divider_height)
.visibilityProvider { position, parent ->
// This looked right to me (visibility -> true), but is backwards.
true
}
.build()
I'd suggest inverting that API so it's more Kotlin-friendly:
interface VisibilityProvider {
boolean shouldShowDivider(int position, RecyclerView parent);
}
This issue may apply to other APIs, but I haven't run into any others yet, or had time to look.