Skip to content

Commit 5cee439

Browse files
Improve Kotlin code
1 parent d33f690 commit 5cee439

File tree

1 file changed

+43
-60
lines changed

1 file changed

+43
-60
lines changed

core/src/main/java/com/github/stephenvinouze/materialnumberpickercore/MaterialNumberPicker.kt

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package com.github.stephenvinouze.materialnumberpickercore
22

33
import android.content.Context
4-
import android.graphics.Color
5-
import android.graphics.Paint
6-
import android.graphics.Typeface
7-
import android.graphics.drawable.ColorDrawable
4+
import android.graphics.*
5+
import android.graphics.drawable.Drawable
86
import android.text.InputType
97
import android.util.AttributeSet
108
import android.util.TypedValue
119
import android.view.ViewGroup
1210
import android.widget.EditText
1311
import android.widget.NumberPicker
14-
import java.lang.reflect.Field
1512

1613
/**
1714
* Created by stephenvinouze on 25/09/2017.
@@ -32,13 +29,7 @@ class MaterialNumberPicker : NumberPicker {
3229
var separatorColor: Int = Color.TRANSPARENT
3330
set(value) {
3431
field = value
35-
try {
36-
dividerField?.set(this, ColorDrawable(separatorColor))
37-
} catch (e: IllegalAccessException) {
38-
e.printStackTrace()
39-
} catch (e: IllegalArgumentException) {
40-
e.printStackTrace()
41-
}
32+
divider?.colorFilter = PorterDuffColorFilter(separatorColor, PorterDuff.Mode.SRC_IN)
4233
}
4334

4435
var textColor: Int = DEFAULT_TEXT_COLOR
@@ -71,23 +62,36 @@ class MaterialNumberPicker : NumberPicker {
7162
updateTextAttributes()
7263
}
7364

74-
private val wheelField: Field by lazy {
75-
val selectorWheelPaintField = NumberPicker::class.java.getDeclaredField("mSelectorWheelPaint")
76-
selectorWheelPaintField.isAccessible = true
77-
selectorWheelPaintField
65+
private val inputEditText: EditText? by lazy {
66+
try {
67+
val f = NumberPicker::class.java.getDeclaredField("mInputText")
68+
f.isAccessible = true
69+
f.get(this) as EditText
70+
} catch (e: Exception) {
71+
null
72+
}
73+
}
74+
75+
private val wheelPaint: Paint? by lazy {
76+
try {
77+
val selectorWheelPaintField = NumberPicker::class.java.getDeclaredField("mSelectorWheelPaint")
78+
selectorWheelPaintField.isAccessible = true
79+
selectorWheelPaintField.get(this) as Paint
80+
} catch (e: Exception) {
81+
null
82+
}
7883
}
7984

80-
private val dividerField: Field? by lazy {
81-
var field: Field? = null
82-
val fields = NumberPicker::class.java.declaredFields
83-
for (f in fields) {
84-
if (f.name == "mSelectionDivider") {
85-
f.isAccessible = true
86-
field = f
87-
break
85+
private val divider: Drawable? by lazy {
86+
val dividerField = NumberPicker::class.java.declaredFields.firstOrNull { it.name == "mSelectionDivider" }
87+
dividerField?.let {
88+
try {
89+
it.isAccessible = true
90+
it.get(this) as Drawable
91+
} catch (e: Exception) {
92+
null
8893
}
8994
}
90-
field
9195
}
9296

9397
@JvmOverloads
@@ -144,49 +148,28 @@ class MaterialNumberPicker : NumberPicker {
144148
* This is still an open Google @see <a href="https://code.google.com/p/android/issues/detail?id=35482#c9">issue</a> from 2012
145149
*/
146150
private fun disableFocusability() {
147-
try {
148-
val f = NumberPicker::class.java.getDeclaredField("mInputText")
149-
f.isAccessible = true
150-
val inputText = f.get(this) as EditText
151-
inputText.filters = arrayOfNulls(0)
152-
} catch (e: NoSuchFieldException) {
153-
// nothing to do, ignoring
154-
} catch (e: IllegalAccessException) {
155-
// nothing to do, ignoring
156-
} catch (e: IllegalArgumentException) {
157-
// nothing to do, ignoring
158-
}
151+
inputEditText?.filters = arrayOfNulls(0)
159152
}
160153

161154
/**
162155
* Uses reflection to access text size private attribute for both wheel and edit text inside the number picker.
163156
*/
164157
private fun updateTextAttributes() {
165158
val typeface = if (fontName != null) Typeface.createFromAsset(context.assets, "fonts/$fontName") else Typeface.create(Typeface.DEFAULT, textStyle)
166-
try {
167-
val wheelPaint = wheelField.get(this) as Paint
168-
wheelPaint.color = textColor
169-
wheelPaint.textSize = textSize.toFloat()
170-
wheelPaint.typeface = typeface
171-
172-
for (i in 0 until childCount) {
173-
val child = getChildAt(i)
174-
if (child is EditText) {
175-
child.setTextColor(textColor)
176-
child.setTextSize(TypedValue.COMPLEX_UNIT_SP, pixelsToSp(context, textSize.toFloat()))
177-
child.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_NORMAL
178-
child.typeface = typeface
179-
180-
invalidate()
181-
break
182-
}
159+
wheelPaint?.let {
160+
it.color = textColor
161+
it.textSize = textSize.toFloat()
162+
it.typeface = typeface
163+
164+
val childEditText = (0 until childCount).map { getChildAt(it) as? EditText }.firstOrNull()
165+
childEditText?.let {
166+
it.setTextColor(textColor)
167+
it.setTextSize(TypedValue.COMPLEX_UNIT_SP, pixelsToSp(context, textSize.toFloat()))
168+
it.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_NORMAL
169+
it.typeface = typeface
170+
171+
invalidate()
183172
}
184-
} catch (e: NoSuchFieldException) {
185-
// nothing to do, ignoring
186-
} catch (e: IllegalAccessException) {
187-
// nothing to do, ignoring
188-
} catch (e: IllegalArgumentException) {
189-
// nothing to do, ignoring
190173
}
191174
}
192175

0 commit comments

Comments
 (0)