Clear listeners, hide icons when appropriate (#3870)

Preferences are shown using view holders.

The previous code did not clear the listeners or hide the icons if
necessary.

The practical upshot of this was that if you had two or more slider
preferences, *and* they were situated more than a screen's height apart,
the viewholder from the first one would get reused.

And if the first one enabled icons then the second one would show them.
And clicking on the second one would also call the listeners for the
first one.
This commit is contained in:
Nik Clayton 2023-07-26 17:00:08 +02:00 committed by GitHub
parent 0b01efed5e
commit 406152d5b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 6 deletions

View File

@ -4,7 +4,6 @@ import android.content.Context
import android.content.res.TypedArray import android.content.res.TypedArray
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View.VISIBLE
import androidx.appcompat.content.res.AppCompatResources import androidx.appcompat.content.res.AppCompatResources
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
@ -12,6 +11,8 @@ import com.google.android.material.slider.LabelFormatter.LABEL_GONE
import com.google.android.material.slider.Slider import com.google.android.material.slider.Slider
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.databinding.PrefSliderBinding import com.keylesspalace.tusky.databinding.PrefSliderBinding
import com.keylesspalace.tusky.util.hide
import com.keylesspalace.tusky.util.show
import java.lang.Float.max import java.lang.Float.max
import java.lang.Float.min import java.lang.Float.min
@ -130,6 +131,8 @@ class SliderPreference @JvmOverloads constructor(
binding.root.isClickable = false binding.root.isClickable = false
binding.slider.clearOnChangeListeners()
binding.slider.clearOnSliderTouchListeners()
binding.slider.addOnChangeListener(this) binding.slider.addOnChangeListener(this)
binding.slider.addOnSliderTouchListener(this) binding.slider.addOnSliderTouchListener(this)
binding.slider.value = value // sliderValue binding.slider.value = value // sliderValue
@ -141,24 +144,24 @@ class SliderPreference @JvmOverloads constructor(
binding.slider.labelBehavior = LABEL_GONE binding.slider.labelBehavior = LABEL_GONE
binding.slider.isEnabled = isEnabled binding.slider.isEnabled = isEnabled
binding.summary.visibility = VISIBLE binding.summary.show()
binding.summary.text = formatter(value) binding.summary.text = formatter(value)
decrementIcon?.let { icon -> decrementIcon?.let { icon ->
binding.decrement.icon = icon binding.decrement.icon = icon
binding.decrement.visibility = VISIBLE binding.decrement.show()
binding.decrement.setOnClickListener { binding.decrement.setOnClickListener {
value -= stepSize value -= stepSize
} }
} } ?: binding.decrement.hide()
incrementIcon?.let { icon -> incrementIcon?.let { icon ->
binding.increment.icon = icon binding.increment.icon = icon
binding.increment.visibility = VISIBLE binding.increment.show()
binding.increment.setOnClickListener { binding.increment.setOnClickListener {
value += stepSize value += stepSize
} }
} } ?: binding.increment.hide()
} }
override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) { override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) {