Anki-Android
Anki-Android copied to clipboard
Add doOnTabSelected extension function for TabLayout
The current pattern for listening to tab selection events is verbose and requires implementing all three methods of TabLayout.OnTabSelectedListener even when only onTabSelected is needed.
Current Pattern
tabLayout.addOnTabSelectedListener(
object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
// actual logic
}
override fun onTabUnselected(tab: TabLayout.Tab) {
// do nothing
}
override fun onTabReselected(tab: TabLayout.Tab) {
// do nothing
}
},
)
Proposed Solution
Add a Kotlin extension function similar to AndroidX KTX patterns:
inline fun TabLayout.doOnTabSelected(crossinline action: (tab: TabLayout.Tab) -> Unit) {
addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) = action(tab)
override fun onTabUnselected(tab: TabLayout.Tab) {}
override fun onTabReselected(tab: TabLayout.Tab) {}
})
}
Usage
tabLayout.doOnTabSelected { tab ->
viewModel.onTabSelected(tab.position)
}
@Haz3-jolt This would make a good first issue, unless you want to take it up
@Haz3-jolt This would make a good first issue, unless you want to take it up
I agree, good first issue it is!
Hi, I would like to work on this. Could you please assign this issue to me?