Skip to content

Instantly share code, notes, and snippets.

Avatar
🤔
Is this Slack?

Pablo Gonzalez Alonso pablisco

🤔
Is this Slack?
View GitHub Profile
View ArgbEvaluator.java
public Object evaluate(float fraction, Object startValue, Object endValue) {
int startInt = (Integer) startValue;
float startA = ((startInt >> 24) & 0xff) / 255.0f;
float startR = ((startInt >> 16) & 0xff) / 255.0f;
float startG = ((startInt >> 8) & 0xff) / 255.0f;
float startB = ( startInt & 0xff) / 255.0f;
int endInt = (Integer) endValue;
float endA = ((endInt >> 24) & 0xff) / 255.0f;
float endR = ((endInt >> 16) & 0xff) / 255.0f;
@pablisco
pablisco / README.md
Last active Mar 3, 2020
Gradle Multi project settings Kotlin DSL
View README.md

Gradle Multi project settings Kotlin DSL

This is a small Gradle DSL that can be used to include multiple modules. Let's imagine that we have a set up like this:

root
+-- app
+-- modules
    +-- core
    +-- androidx
@pablisco
pablisco / Coroutine_scope_functions.kt
Last active Jul 11, 2019
Simple way to jump into a separate context
View Coroutine_scope_functions.kt
suspend fun <T, R> T.letOn(
context: CoroutineContext,
block: suspend CoroutineScope.(T) -> R
): R = withContext(context) { block(this@letOn) }
suspend fun <T> T.alsoOn(
context: CoroutineContext,
block: suspend CoroutineScope.(T) -> Unit
): T = also { withContext(context) { block(this@alsoOn) } }
View test.kt
package com.pusher.chatkit.users
val string by lazy { "" }
val List<String>.describe: String by lazy {
"this"
}
val List<String>.something: String get() {
return ""
View Throttler.kt
/**
* Used to do a trailing throttle of actions.
*/
internal class Throttler<A, B>(
private val delay: Long = 500,
private val action: (A) -> B
) {
private val target = AtomicReference<A>()
View GhettoParser.kt
fun <A> A.toJson(): String {
fun <A> Iterable<A>.toJsonArray(): String =
joinToString(", ", "[", "]") { it.toJson() }
fun <A, B> Map<A, B>.toJsonObject() =
entries.joinToString(", ", "{", "}") { (key, value) -> "\"$key\" : ${value.toJson()}" }
return when(this) {
null -> "null"
@pablisco
pablisco / Logger.kt
Last active May 17, 2018
Fluent Logging with Kotlin
View Logger.kt
inline fun <A> A.logWith(logger: Logger, block: Logger.(A) -> Unit) : A =
this.also { logger.block(it) }
// With interface injection
interface HasLog {
val log: Logger
fun <A> A.log(block: Logger.(A) -> Unit) : A =
logWith(logger, block)
}
View DimenExtensions.kt
fun Int.dp(): Int =
toFloat().dp().toInt()
fun Float.dp(): Float = applyDimension(
COMPLEX_UNIT_DIP,
this,
Resources.getSystem().displayMetrics
)
View ViewCompanion.kt
import android.os.Build
import android.view.View
import java.util.concurrent.atomic.AtomicInteger
object ViewCompanion {
private val NEXT_GENERATED_ID = AtomicInteger(1)
/**
* Inspired by: https://stackoverflow.com/a/15442997/458365
You can’t perform that action at this time.