Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f31a5c43 authored by Haijie Hong's avatar Haijie Hong
Browse files

Update SegmentedButtonPreference

1. Use map instead of list for buttonSetupData, buttonVisibilityData and buttonEnableData.
2. Use notifyChanged when update UI.
3. Move applyCheckIndex(checkedIndex) after clearOnButtonCheckedListeners, so the listener won't be triggered when we call "setCheckIndex".

Bug: 399532172
Test: local tested
Flag: EXEMPT ui fix
Change-Id: Ia8d4e2859ae21d57dabce44813ce91277fdc7f0c
parent 36bb1d84
Loading
Loading
Loading
Loading
+28 −60
Original line number Diff line number Diff line
@@ -41,9 +41,9 @@ class SegmentedButtonPreference @JvmOverloads constructor(

    // Data to be applied during onBindViewHolder
    private val buttonSetupData =
        mutableListOf<Triple<Int, String, SegmentedButtonIcon>>() // (index, text, icon)
    private val buttonVisibilityData = mutableListOf<Pair<Int, Boolean>>() // (index, visibility)
    private val buttonEnableData = mutableListOf<Pair<Int, Boolean>>() // (index, enable)
        mutableMapOf<Int, Pair<String, SegmentedButtonIcon>>() // (index, text, icon)
    private val buttonVisibilityData = mutableMapOf<Int, Boolean>() // (index, visibility)
    private val buttonEnableData = mutableMapOf<Int, Boolean>() // (index, enable)

    // Default checked button
    private var checkedIndex: Int = -1
@@ -67,9 +67,9 @@ class SegmentedButtonPreference @JvmOverloads constructor(
        applyButtonSetupData()
        applyButtonVisibilityData()
        applyButtonEnableData()
        applyCheckIndex(checkedIndex)
        buttonGroup?.apply {
            clearOnButtonCheckedListeners()
            applyCheckIndex(checkedIndex)
            buttonCheckedListener?.let { listener ->
                addOnButtonCheckedListener(listener)
            }
@@ -77,53 +77,28 @@ class SegmentedButtonPreference @JvmOverloads constructor(
    }

    fun setUpButton(index: Int, text: String, @DrawableRes icon: Int) {
        if (buttonGroup == null) {
            // Store data for later application
            buttonSetupData.add(Triple(index, text, SegmentedButtonIcon.ResourceIcon(icon)))
        } else {
            // Apply data
            applyButtonSetupData(index, text, SegmentedButtonIcon.ResourceIcon(icon))
        }
        buttonSetupData.put(index, Pair(text, SegmentedButtonIcon.ResourceIcon(icon)))
        notifyChanged()
    }

    fun setUpButton(index: Int, text: String, icon: Drawable) {
        if (buttonGroup == null) {
            // Store data for later application
            buttonSetupData.add(Triple(index, text, SegmentedButtonIcon.DrawableIcon(icon)))
        } else {
            // Apply data
            applyButtonSetupData(index, text, SegmentedButtonIcon.DrawableIcon(icon))
        }
        buttonSetupData.put(index, Pair(text, SegmentedButtonIcon.DrawableIcon(icon)))
        notifyChanged()
    }

    fun setButtonVisibility(index: Int, visible: Boolean) {
        if (buttonGroup == null) {
            // Store data for later application
            buttonVisibilityData.add(Pair(index, visible))
        } else {
            // Apply data
            applyButtonVisibilityData(index, visible)
        }
        buttonVisibilityData.put(index, visible)
        notifyChanged()
    }

    fun setButtonEnabled(index: Int, enabled: Boolean) {
        if (buttonGroup == null) {
            // Store data for later application
            buttonEnableData.add(Pair(index, enabled))
        } else {
            // Apply data
            applyButtonEnableData(index, enabled)
        }
        buttonEnableData.put(index, enabled)
        notifyChanged()
    }

    fun setCheckedIndex(index: Int) {
        if (buttonGroup == null) {
            // Store data for later application
        checkedIndex = index
        } else {
            // Apply data
            applyCheckIndex(index)
        }
        notifyChanged()
    }

    fun getCheckedIndex(): Int {
@@ -142,28 +117,24 @@ class SegmentedButtonPreference @JvmOverloads constructor(
    }

    private fun applyButtonSetupData() {
        for (config in buttonSetupData) {
            applyButtonSetupData(config.first, config.second, config.third)
        for ((index, config) in buttonSetupData) {
            applyButtonSetupData(index, config.first, config.second)
        }
        buttonSetupData.clear() // Clear data after applying
    }

    private fun applyButtonVisibilityData() {
        for (config in buttonVisibilityData) {
            applyButtonVisibilityData(config.first, config.second)
        for ((index, visible) in buttonVisibilityData) {
            applyButtonVisibilityData(index, visible)
        }
        buttonVisibilityData.clear() // Clear data after applying
    }

    private fun applyButtonEnableData() {
        for (config in buttonEnableData) {
            applyButtonEnableData(config.first, config.second)
        for ((index, enable) in buttonEnableData) {
            applyButtonEnableData(index, enable)
        }
        buttonEnableData.clear() // Clear data after applying
    }

    private fun applyButtonSetupData(index: Int, text: String, icon: SegmentedButtonIcon) {
        if (index in 0 until buttonLabels.size) {
        when (icon) {
            is SegmentedButtonIcon.ResourceIcon ->
                (buttonGroup?.getChildAt(index) as? MaterialButton)?.setIconResource(icon.resId)
@@ -173,14 +144,11 @@ class SegmentedButtonPreference @JvmOverloads constructor(
        }
        buttonLabels[index].text = text
    }
    }

    private fun applyButtonVisibilityData(index: Int, visible: Boolean) {
        if (index in 0 until buttonLabels.size) {
        buttonGroup?.getChildAt(index)?.isGone = !visible
        buttonLabels[index].isGone = !visible
    }
    }

    private fun applyButtonEnableData(index: Int, enabled: Boolean) {
        buttonGroup?.getChildAt(index)?.isEnabled = enabled