11package com.omega_r.libs.extensions.view
22
33import android.graphics.drawable.Drawable
4- import android.os.Build.*
4+ import android.os.Build.VERSION
5+ import android.os.Build.VERSION_CODES
56import android.text.*
67import android.text.Annotation
78import android.text.method.LinkMovementMethod
@@ -11,7 +12,6 @@ import android.view.View
1112import android.widget.TextView
1213import androidx.annotation.ColorRes
1314import androidx.core.content.ContextCompat
14- import androidx.core.widget.TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds
1515import com.omega_r.libs.extensions.context.getCompatColor
1616
1717fun 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
155188fun TextView.setDrawableLeft (drawableRes : Int ) {
156189 drawableLeft = ContextCompat .getDrawable(context, drawableRes)
157190}
158191
159192var 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
174196fun TextView.setDrawableStart (drawableRes : Int ) {
175197 drawableStart = ContextCompat .getDrawable(context, drawableRes)
176198}
177199
178200var 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
185204fun TextView.setDrawableRight (drawableRes : Int ) {
186205 drawableRight = ContextCompat .getDrawable(context, drawableRes)
187206}
188207
189208var 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
205212fun TextView.setDrawableEnd (drawableRes : Int ) {
206213 drawableEnd = ContextCompat .getDrawable(context, drawableRes)
207214}
208215
209216var 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
224220fun TextView.setDrawableTop (drawableRes : Int ) {
225221 drawableTop = ContextCompat .getDrawable(context, drawableRes)
226222}
227223
228224var 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
243228fun TextView.setDrawableBottom (drawableRes : Int ) {
244229 drawableBottom = ContextCompat .getDrawable(context, drawableRes)
0 commit comments