package com.base.pdfviewerscannerwhite.ui.view

import android.text.InputFilter
import android.text.Spanned

class NumberRangeFilter(private val min: Int, private val max: Int) : InputFilter {
    override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
        try {
            val input = dest.subSequence(0, dstart).toString() + source.toString() + dest.subSequence(dend, dest.length)
            val numericValue = input.toIntOrNull()
            if (numericValue != null && numericValue in min..max) {
                return null // 允许输入
            }
        } catch (nfe: NumberFormatException) {
            // 如果输入不是数字，则忽略
        }
        return "" // 拒绝输入
    }
}