https://kotlinlang.org logo
#android
Title
# android
a

althaf

07/28/2022, 7:07 AM
Hi there, is there way to shorten this code
Copy code
var messageStyle: MessageStyle = MessageStyle.Large
    set(value) {
        var textStyle = 0
        var iconSize = 0
        var padding = 0

        when (value) {
            MessageStyle.Small -> {
                textStyle = R.style.TextAppearance_Sc_Components_Body2_Regular
                iconSize =  context.resources.getDimensionPixelSize(R.dimen.icon_size_small)
                padding = R.dimen.sc_space_xs
            }
            else -> {
                textStyle = R.style.TextAppearance_Sc_Components_Body1_Regular
                iconSize =  context.resources.getDimensionPixelSize(R.dimen.icon_size_regular)
                padding = R.dimen.sc_space_xs
            }
        }
        tvMessage.setTextAppearance(textStyle)
        ivMessageVariantIcon.layoutParams = ViewGroup.LayoutParams(iconSize,iconSize);
        setPaddingRelative(padding, padding, padding, padding)
        field = value
    }
m

Mert TUTSAK

07/28/2022, 9:05 AM
you can do like the below, there is might syntax error. i didn't run the code
Copy code
var messageStyle: MessageStyle = MessageStyle.Large
    set(value) {
        when (value) {
            MessageStyle.Small -> Triple(
                R.style.TextAppearance_Sc_Components_Body2_Regular,
                context.resources.getDimensionPixelSize(R.dimen.icon_size_small),
                R.dimen.sc_space_xs
            )
            else -> Triple(
                R.style.TextAppearance_Sc_Components_Body1_Regular,
                context.resources.getDimensionPixelSize(R.dimen.icon_size_regular),
                R.dimen.sc_space_xs
            )
        }.run{
            tvMessage.setTextAppearance(first)
            ivMessageVariantIcon.layoutParams = ViewGroup.LayoutParams(second, second);
            setPaddingRelative(third, third, third, third)
        }
        field = value
    }
1
2 Views