Last active
December 26, 2023 13:31
-
-
Save kishan-vadoliya/ae536a5e45dde3340630c6b350640dba to your computer and use it in GitHub Desktop.
Inline function kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.stream.IntStream | |
object Funciones { | |
private const val NUMERO_MULTIPLO_DE_PAR = 2 | |
private infix fun Int.doubleDiv(i: Int): Double = this / i.toDouble() | |
infix fun Int.esMultiplo(numero:Int):Boolean = this % numero == 0 | |
fun Int.esPar() = this esMultiplo NUMERO_MULTIPLO_DE_PAR | |
fun Int.esInpar() = this.esPar().not() | |
fun Int.esBiciesto() = this esMultiplo 4 && (this esMultiplo 400 || (this esMultiplo 100).not()) | |
fun numeroMayor(vararg numeros:Int) = numeros.max() | |
fun Int.secuenciaUno():Double = IntStream.range(1, this + 1) | |
.mapToDouble { 1 doubleDiv it } | |
.reduce{ left, right -> left + right} | |
.orElse(0.0) | |
fun Activity.hideSoftKeyboard() { | |
if (currentFocus != null) { | |
val inputMethodManager = getSystemService( | |
Context.INPUT_METHOD_SERVICE | |
) as InputMethodManager | |
inputMethodManager.hideSoftInputFromWindow(currentFocus!!.windowToken, 0) | |
} | |
fun getBitmap(width: Int, height: Int, mColorCode: Int): Bitmap { | |
val colorBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) | |
val surfaceCanvas = Canvas(colorBitmap) | |
val surfacePaint = Paint(Paint.ANTI_ALIAS_FLAG) | |
surfacePaint.isAntiAlias = true | |
surfacePaint.isDither = true | |
surfacePaint.isFilterBitmap = true | |
surfacePaint.color = mColorCode | |
surfaceCanvas.drawRect(0F, 0F, width.toFloat(), height.toFloat(), surfacePaint) | |
return colorBitmap | |
} | |
fun Bitmap.rotate(degrees: Float): Bitmap? = | |
Bitmap.createBitmap(this, 0, 0, width, height, Matrix().apply { postRotate(degrees) }, true) | |
fun Bitmap.copy(): Bitmap? = copy(config, isMutable) | |
fun Bitmap.flip(xFlip: Boolean = false, yFlip: Boolean = false): Bitmap? { | |
return let { | |
Bitmap.createBitmap( | |
it, 0, 0, width, height, Matrix().apply { | |
postScale( | |
if (xFlip) -1F else 1F, | |
if (yFlip) -1F else 1F, | |
width / 2f, | |
height / 2f | |
) | |
}, true | |
) | |
} | |
} | |
fun Bitmap.radius(mRadiusValue: Float = 0F): Bitmap? { | |
return if (mRadiusValue > 0F) { | |
val output = Bitmap.createBitmap(width, height, config) | |
val canvas = Canvas(output) | |
val paint = Paint() | |
val rect = Rect(0, 0, width, height) | |
val rectF = RectF(rect) | |
paint.isAntiAlias = true | |
paint.isFilterBitmap = true | |
paint.isDither = true | |
val minSizeValue = min(width, height) | |
val localRadiusValue: Float = ((minSizeValue / 2F) * mRadiusValue) / 100F | |
canvas.drawRoundRect(rectF, localRadiusValue, localRadiusValue, paint) | |
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) | |
canvas.drawBitmap(this, rect, rect, paint) | |
output | |
} else { | |
this | |
} | |
} | |
} | |
/** | |
* Extension method to Get Color for resource for Context. | |
*/ | |
fun Context.getColor(@ColorRes id: Int) = ContextCompat.getColor(this, id) | |
/** | |
* Extension method to Get Drawable for resource for Context. | |
*/ | |
fun Context.getDrawable(@DrawableRes id: Int) = ContextCompat.getDrawable(this, id) | |
private val values = FloatArray(9) | |
fun Matrix.getScaleX(): Float { | |
getValues(values) | |
return values[Matrix.MSCALE_X] | |
} | |
fun Matrix.getTranslateX(): Float { | |
getValues(values) | |
return values[Matrix.MTRANS_X] | |
} | |
fun Matrix.getTranslateY(): Float { | |
getValues(values) | |
return values[Matrix.MTRANS_Y] | |
} | |
fun Matrix.clone(): Matrix { | |
getValues(values) | |
return Matrix().apply { | |
setValues(values) | |
} | |
} | |
inline fun View.doOnGlobalLayout(crossinline action: (view: View) -> Unit) { | |
val vto = viewTreeObserver | |
vto.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { | |
override fun onGlobalLayout() { | |
action(this@doOnGlobalLayout) | |
when { | |
vto.isAlive -> { | |
vto.removeOnGlobalLayoutListener(this) | |
} | |
else -> { | |
viewTreeObserver.removeOnGlobalLayoutListener(this) | |
} | |
} | |
} | |
}) | |
} | |
inline fun View.onClick(crossinline action: (view: View) -> Unit) { | |
setOnClickListener { | |
if (Utils.checkClickTime()) | |
action(this@onClick) | |
} | |
} | |
inline fun EditText.onTextChange(crossinline onChange: (String) -> Unit): Disposable { | |
val textWatcher = object : TextWatcher { | |
override fun afterTextChanged(password: Editable?) { | |
} | |
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { | |
} | |
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | |
onChange(s?.toString() ?: "") | |
} | |
} | |
addTextChangedListener(textWatcher) | |
return object : Disposable { | |
@kotlin.jvm.Volatile | |
override var isDisposed: Boolean = false | |
private set | |
override fun dispose() { | |
removeTextChangedListener(textWatcher) | |
isDisposed = true | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment