Skip to content

Commit 702e7be

Browse files
committed
Refactroing TextViewExtensions
1 parent e821801 commit 702e7be

File tree

1 file changed

+51
-66
lines changed

1 file changed

+51
-66
lines changed

extensionslib/src/main/java/com/omega_r/libs/extensions/view/TextViewExtensions.kt

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.omega_r.libs.extensions.view
22

33
import android.graphics.drawable.Drawable
4-
import android.os.Build.*
4+
import android.os.Build.VERSION
5+
import android.os.Build.VERSION_CODES
56
import android.text.*
67
import android.text.Annotation
78
import android.text.method.LinkMovementMethod
@@ -11,7 +12,6 @@ import android.view.View
1112
import android.widget.TextView
1213
import androidx.annotation.ColorRes
1314
import androidx.core.content.ContextCompat
14-
import androidx.core.widget.TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds
1515
import com.omega_r.libs.extensions.context.getCompatColor
1616

1717
fun TextView.addTextListener(
@@ -58,7 +58,11 @@ fun TextView.addDebounceTextListener(
5858
})
5959
}
6060

61-
fun TextView.addDebounceChangeStateListener(delayInMillis: Long = 500, timeoutInMillis: Long = 0, listener: (Boolean) -> Unit) {
61+
fun TextView.addDebounceChangeStateListener(
62+
delayInMillis: Long = 500,
63+
timeoutInMillis: Long = 0,
64+
listener: (Boolean) -> Unit
65+
) {
6266
addTextChangedListener(object : TextWatcher {
6367
private var start: Boolean = false
6468
private val runnable: Runnable = Runnable {
@@ -145,100 +149,81 @@ fun TextView.setTextColorResource(@ColorRes colorRes: Int) {
145149
setTextColor(context.getCompatColor(colorRes))
146150
}
147151

148-
var TextView.drawableLeft: Drawable?
149-
get() = compoundDrawables[0]
150-
set(value) {
151-
val drawables = compoundDrawables
152-
setCompoundDrawablesWithIntrinsicBounds(value, drawables[1], drawables[2], drawables[3])
152+
private fun TextView.getDrawableRelative(index: Int): Drawable? {
153+
val drawables =
154+
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) compoundDrawablesRelative else compoundDrawables
155+
return drawables[index]
156+
}
157+
158+
private fun TextView.setDrawableRelative(index: Int, drawable: Drawable?) {
159+
val drawables =
160+
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) compoundDrawablesRelative else compoundDrawables
161+
162+
drawables[index] = drawable
163+
164+
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
165+
setCompoundDrawablesRelativeWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3])
166+
} else {
167+
setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3])
153168
}
169+
}
170+
171+
private fun TextView.getDrawable(index: Int): Drawable? {
172+
val drawables = compoundDrawables
173+
return drawables[index]
174+
}
175+
176+
private fun TextView.setDrawable(index: Int, drawable: Drawable?) {
177+
val drawables = compoundDrawables
178+
179+
drawables[index] = drawable
180+
181+
setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3])
182+
}
183+
184+
var TextView.drawableLeft: Drawable?
185+
get() = getDrawable(0)
186+
set(value) = setDrawable(0, value)
154187

155188
fun TextView.setDrawableLeft(drawableRes: Int) {
156189
drawableLeft = ContextCompat.getDrawable(context, drawableRes)
157190
}
158191

159192
var TextView.drawableStart: Drawable?
160-
get() = (if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) compoundDrawablesRelative[0] else compoundDrawables[0])
161-
set(value) {
162-
val drawables = if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
163-
compoundDrawablesRelative
164-
} else {
165-
compoundDrawables
166-
}
167-
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
168-
setCompoundDrawablesRelativeWithIntrinsicBounds(value, drawables[1], drawables[2], drawables[3])
169-
} else {
170-
setCompoundDrawablesWithIntrinsicBounds(value, drawables[1], drawables[2], drawables[3])
171-
}
172-
}
193+
get() = getDrawableRelative(0)
194+
set(value) = setDrawableRelative(0, value)
173195

174196
fun TextView.setDrawableStart(drawableRes: Int) {
175197
drawableStart = ContextCompat.getDrawable(context, drawableRes)
176198
}
177199

178200
var TextView.drawableRight: Drawable?
179-
get() = compoundDrawables[2]
180-
set(value) {
181-
val drawables = compoundDrawables
182-
setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], value, drawables[3])
183-
}
201+
get() = getDrawable(2)
202+
set(value) = setDrawableRelative(2, value)
184203

185204
fun TextView.setDrawableRight(drawableRes: Int) {
186205
drawableRight = ContextCompat.getDrawable(context, drawableRes)
187206
}
188207

189208
var TextView.drawableEnd: Drawable?
190-
get() = (if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) compoundDrawablesRelative[2] else compoundDrawables[2])
191-
set(value) {
192-
val drawables = if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
193-
compoundDrawablesRelative
194-
} else {
195-
compoundDrawables
196-
}
197-
198-
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
199-
setCompoundDrawablesRelativeWithIntrinsicBounds(drawables[0], drawables[1], value, drawables[3])
200-
} else {
201-
setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], value, drawables[3])
202-
}
203-
}
209+
get() = getDrawableRelative(2)
210+
set(value) = setDrawableRelative(2, value)
204211

205212
fun TextView.setDrawableEnd(drawableRes: Int) {
206213
drawableEnd = ContextCompat.getDrawable(context, drawableRes)
207214
}
208215

209216
var TextView.drawableTop: Drawable?
210-
get() = (if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) compoundDrawablesRelative[1] else compoundDrawables[1])
211-
set(value) {
212-
val drawables = if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
213-
compoundDrawablesRelative
214-
} else {
215-
compoundDrawables
216-
}
217-
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
218-
setCompoundDrawablesRelativeWithIntrinsicBounds(drawables[0], value, drawables[2], drawables[3])
219-
} else {
220-
setCompoundDrawablesWithIntrinsicBounds(drawables[0], value, drawables[2], drawables[3])
221-
}
222-
}
217+
get() = getDrawableRelative(1)
218+
set(value) = setDrawableRelative(1, value)
223219

224220
fun TextView.setDrawableTop(drawableRes: Int) {
225221
drawableTop = ContextCompat.getDrawable(context, drawableRes)
226222
}
227223

228224
var TextView.drawableBottom: Drawable?
229-
get() = (if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) compoundDrawablesRelative[3] else compoundDrawables[3])
230-
set(value) {
231-
val drawables = if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
232-
compoundDrawablesRelative
233-
} else {
234-
compoundDrawables
235-
}
236-
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
237-
setCompoundDrawablesRelativeWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], value)
238-
} else {
239-
setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], value)
240-
}
241-
}
225+
get() = getDrawableRelative(3)
226+
set(value) = setDrawableRelative(3, value)
242227

243228
fun TextView.setDrawableBottom(drawableRes: Int) {
244229
drawableBottom = ContextCompat.getDrawable(context, drawableRes)

0 commit comments

Comments
 (0)