2018-01-23 15:18:55 -05:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.text.SpannableStringBuilder
|
|
|
|
import android.util.AttributeSet
|
2020-11-28 14:56:57 -05:00
|
|
|
import android.view.LayoutInflater
|
2018-01-23 15:18:55 -05:00
|
|
|
import android.view.View
|
|
|
|
import android.widget.LinearLayout
|
2020-11-28 14:56:57 -05:00
|
|
|
import eu.kanade.tachiyomi.databinding.DownloadCustomAmountBinding
|
2018-01-23 15:18:55 -05:00
|
|
|
import timber.log.Timber
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom dialog to select how many chapters to download.
|
|
|
|
*/
|
|
|
|
class DialogCustomDownloadView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
|
2020-04-25 14:24:45 -04:00
|
|
|
LinearLayout(context, attrs) {
|
2018-01-23 15:18:55 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current amount of custom download chooser.
|
|
|
|
*/
|
|
|
|
var amount: Int = 0
|
|
|
|
private set
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimal value of custom download chooser.
|
|
|
|
*/
|
|
|
|
private var min = 0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximal value of custom download chooser.
|
|
|
|
*/
|
|
|
|
private var max = 0
|
|
|
|
|
2020-11-28 14:56:57 -05:00
|
|
|
private val binding: DownloadCustomAmountBinding
|
|
|
|
|
2018-01-23 15:18:55 -05:00
|
|
|
init {
|
2020-11-28 14:56:57 -05:00
|
|
|
binding = DownloadCustomAmountBinding.inflate(LayoutInflater.from(context), this, false)
|
|
|
|
addView(binding.root)
|
2018-01-23 15:18:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewAdded(child: View) {
|
|
|
|
super.onViewAdded(child)
|
|
|
|
|
|
|
|
// Set download count to 0.
|
2020-11-28 14:56:57 -05:00
|
|
|
binding.myNumber.text = SpannableStringBuilder(getAmount(0).toString())
|
2018-01-23 15:18:55 -05:00
|
|
|
|
|
|
|
// When user presses button decrease amount by 10.
|
2020-11-28 14:56:57 -05:00
|
|
|
binding.btnDecrease10.setOnClickListener {
|
|
|
|
binding.myNumber.text = SpannableStringBuilder(getAmount(amount - 10).toString())
|
2018-01-23 15:18:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// When user presses button increase amount by 10.
|
2020-11-28 14:56:57 -05:00
|
|
|
binding.btnIncrease10.setOnClickListener {
|
|
|
|
binding.myNumber.text = SpannableStringBuilder(getAmount(amount + 10).toString())
|
2018-01-23 15:18:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// When user presses button decrease amount by 1.
|
2020-11-28 14:56:57 -05:00
|
|
|
binding.btnDecrease.setOnClickListener {
|
|
|
|
binding.myNumber.text = SpannableStringBuilder(getAmount(amount - 1).toString())
|
2018-01-23 15:18:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// When user presses button increase amount by 1.
|
2020-11-28 14:56:57 -05:00
|
|
|
binding.btnIncrease.setOnClickListener {
|
|
|
|
binding.myNumber.text = SpannableStringBuilder(getAmount(amount + 1).toString())
|
2018-01-23 15:18:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// When user inputs custom number set amount equal to input.
|
2021-01-07 19:16:26 -05:00
|
|
|
binding.myNumber.addTextChangedListener(object : SimpleTextWatcher() {
|
|
|
|
override fun onTextChanged(text: CharSequence, start: Int, before: Int, count: Int) {
|
2018-01-23 15:18:55 -05:00
|
|
|
try {
|
2021-01-07 19:16:26 -05:00
|
|
|
amount = getAmount(text.toString().toInt())
|
2018-01-23 15:18:55 -05:00
|
|
|
} catch (error: NumberFormatException) {
|
|
|
|
// Catch NumberFormatException to prevent parse exception when input is empty.
|
|
|
|
Timber.e(error)
|
|
|
|
}
|
|
|
|
}
|
2021-01-07 19:16:26 -05:00
|
|
|
})
|
2018-01-23 15:18:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set min max of custom download amount chooser.
|
|
|
|
* @param min minimal downloads
|
|
|
|
* @param max maximal downloads
|
|
|
|
*/
|
|
|
|
fun setMinMax(min: Int, max: Int) {
|
|
|
|
this.min = min
|
|
|
|
this.max = max
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns amount to download.
|
|
|
|
* if minimal downloads is less than input return minimal downloads.
|
|
|
|
* if Maximal downloads is more than input return maximal downloads.
|
|
|
|
*
|
|
|
|
* @return amount to download.
|
|
|
|
*/
|
|
|
|
private fun getAmount(input: Int): Int {
|
|
|
|
return when {
|
|
|
|
input > max -> max
|
|
|
|
input < min -> min
|
|
|
|
else -> input
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|