Is it possible to simplify this code? ```private f...
# android
i
Is it possible to simplify this code?
Copy code
private fun setActiveLabel(textView: TextView) {
    binding.apply {
        accounts.isSelected = binding.accounts == textView
        cards.isSelected = binding.cards == textView
        loans.isSelected = binding.loans == textView
        funds.isSelected = binding.funds == textView

        accounts.isActivated = binding.accounts == textView
        cards.isActivated = binding.cards == textView
        loans.isActivated = binding.loans == textView
        funds.isActivated = binding.funds == textView
    }

    when (textView) {
        binding.accounts -> { binding.accounts.setFocusForAccessibility() }
        binding.cards -> { binding.cards.setFocusForAccessibility() }
        binding.loans -> { binding.loans.setFocusForAccessibility() }
        binding.funds -> { binding.funds.setFocusForAccessibility() }
    }
}
b
at least you can get rid of last
when
by switching to
textView.setFocusForAccessibility()
.
❤️ 1
👍 1
if
setActiveLabel
is rare and you don't care about allocating 4 element list, you can use:
Copy code
binding.apply {
    listOf(accounts, cards, loans, funds).forEach { view -> 
        view.isSelected = view === textView
        view.isActivated = view === textView
    }
}
☝️ 2
i
Wow, thank you! Looks great