MCPcopy Create free account
hub / github.com/GrapheneOS/Camera / NumInputFilter

Class NumInputFilter

app/src/main/java/app/grapheneos/camera/NumInputFilter.kt:7–42  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import app.grapheneos.camera.ui.activities.MoreSettings
6
7class 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 }

Callers 1

onCreateMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected