package com.bonait.bnframework.common.helper;

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

public class RangeInputFilter implements InputFilter {
    private int min;
    private int max;

    public RangeInputFilter(int min, int max) {
        this.min = min;
        this.max = max;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            int input = Integer.parseInt(dest.toString() + source.toString());
            if (isInRange(input)) {
                return null;
            }
        } catch (NumberFormatException e) {
            return null;
        }
        return "";
    }

    private boolean isInRange(int value) {
        return value >= min && value <= max;
    }
}