Skip to content

Instantly share code, notes, and snippets.

Avatar
🤔
Is this Slack?

Pablisco pablisco

🤔
Is this Slack?
View GitHub Profile
View gist:da25563d57559dd1d18f165272269b57
import android.content.Context
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.support.annotation.AnyRes
import android.support.v4.app.Fragment
import android.support.v4.content.res.ResourcesCompat.*
import android.view.View
val Context.animations
get() = ResourceMapper { resources.getAnimation(it) }
@pablisco
pablisco / README.md
Last active Sep 7, 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
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 / 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) } }
@pablisco
pablisco / ReflexionExtensions.kt
Created Sep 3, 2017
Port of Gson's TypeToken to Kotlin
View ReflexionExtensions.kt
import java.io.Serializable
import java.lang.reflect.*
import java.lang.reflect.Array as ArrayType
import java.util.*
val EMPTY_TYPE_ARRAY: kotlin.Array<Type> = emptyArray()
/**
* Returns a new parameterized type, applying {@code typeArguments} to
* {@code rawType} and enclosed by {@code ownerType}.
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>()
@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 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"