2016-09-21 15:26:08 -04:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
|
|
|
import android.content.Context
|
2017-02-19 17:39:53 -05:00
|
|
|
import android.os.Parcelable
|
2016-09-21 15:26:08 -04:00
|
|
|
import android.util.AttributeSet
|
|
|
|
import android.widget.SeekBar
|
2020-03-04 22:37:15 -05:00
|
|
|
import androidx.appcompat.widget.AppCompatSeekBar
|
2016-09-21 15:26:08 -04:00
|
|
|
import eu.kanade.tachiyomi.R
|
2020-01-07 19:20:08 -05:00
|
|
|
import kotlin.math.abs
|
2016-09-21 15:26:08 -04:00
|
|
|
|
|
|
|
class NegativeSeekBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
|
2020-04-25 14:24:45 -04:00
|
|
|
AppCompatSeekBar(context, attrs) {
|
2016-09-21 15:26:08 -04:00
|
|
|
|
|
|
|
private var minValue: Int = 0
|
|
|
|
private var maxValue: Int = 0
|
|
|
|
private var listener: OnSeekBarChangeListener? = null
|
|
|
|
|
|
|
|
init {
|
|
|
|
val styledAttributes = context.obtainStyledAttributes(
|
2020-04-25 14:24:45 -04:00
|
|
|
attrs,
|
|
|
|
R.styleable.NegativeSeekBar, 0, 0
|
|
|
|
)
|
2016-09-21 15:26:08 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
setMinSeek(styledAttributes.getInt(R.styleable.NegativeSeekBar_min_seek, 0))
|
|
|
|
setMaxSeek(styledAttributes.getInt(R.styleable.NegativeSeekBar_max_seek, 0))
|
|
|
|
} finally {
|
|
|
|
styledAttributes.recycle()
|
|
|
|
}
|
|
|
|
|
|
|
|
super.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
|
|
|
|
override fun onProgressChanged(seekBar: SeekBar?, value: Int, fromUser: Boolean) {
|
2020-01-07 19:20:08 -05:00
|
|
|
listener?.onProgressChanged(seekBar, minValue + value, fromUser)
|
2016-09-21 15:26:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStartTrackingTouch(p0: SeekBar?) {
|
2020-01-07 19:20:08 -05:00
|
|
|
listener?.onStartTrackingTouch(p0)
|
2016-09-21 15:26:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStopTrackingTouch(p0: SeekBar?) {
|
2020-01-07 19:20:08 -05:00
|
|
|
listener?.onStopTrackingTouch(p0)
|
2016-09-21 15:26:08 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun setProgress(progress: Int) {
|
2020-01-07 19:20:08 -05:00
|
|
|
super.setProgress(abs(minValue) + progress)
|
2016-09-21 15:26:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fun setMinSeek(minValue: Int) {
|
|
|
|
this.minValue = minValue
|
|
|
|
max = (this.maxValue - this.minValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setMaxSeek(maxValue: Int) {
|
|
|
|
this.maxValue = maxValue
|
|
|
|
max = (this.maxValue - this.minValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun setOnSeekBarChangeListener(listener: OnSeekBarChangeListener?) {
|
|
|
|
this.listener = listener
|
|
|
|
}
|
|
|
|
|
2017-02-19 17:39:53 -05:00
|
|
|
override fun onRestoreInstanceState(state: Parcelable?) {
|
|
|
|
// We can't restore the progress from the saved state because it gets shifted.
|
|
|
|
val origProgress = progress
|
|
|
|
super.onRestoreInstanceState(state)
|
|
|
|
super.setProgress(origProgress)
|
|
|
|
}
|
2020-01-07 19:20:08 -05:00
|
|
|
}
|