| 5 | import app.grapheneos.camera.ui.activities.MoreSettings |
| 6 | |
| 7 | class NumInputFilter(private val settings: MoreSettings) : InputFilter { |
| 8 | |
| 9 | override fun filter( |
| 10 | source: CharSequence, |
| 11 | start: Int, |
| 12 | end: Int, |
| 13 | dest: Spanned, |
| 14 | dstart: Int, |
| 15 | dend: Int |
| 16 | ): CharSequence? { |
| 17 | try { |
| 18 | val input = (dest.subSequence(0, dstart).toString() + source + dest.subSequence( |
| 19 | dend, |
| 20 | dest.length |
| 21 | )).toInt() |
| 22 | if (isInRange(input)) { |
| 23 | return null |
| 24 | } else { |
| 25 | settings.showMessage(settings.getString( |
| 26 | R.string.photo_quality_number_limit, min, max)) |
| 27 | } |
| 28 | } catch (_: NumberFormatException) { |
| 29 | // Nothing numeric to range-check: either the field is being emptied (deleting the |
| 30 | // last digit is a legitimate edit that produces "") or the edit is not a number at |
| 31 | // all. Both end up rejected below, which for a deletion is a no-op because the |
| 32 | // replacement text is already empty. MoreSettings.dumpData() restores the stored |
| 33 | // value if the field is left empty. |
| 34 | } |
| 35 | return "" |
| 36 | } |
| 37 | |
| 38 | private fun isInRange(value: Int): Boolean { |
| 39 | return value in min..max |
| 40 | } |
| 41 | |
| 42 | companion object { |
| 43 | const val min = 1 |
| 44 | const val max = 100 |
| 45 | } |