Skip to content

Instantly share code, notes, and snippets.

@miguelhincapie
Created April 4, 2020 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelhincapie/ad790873da075d9ed51c1624add2939f to your computer and use it in GitHub Desktop.
Save miguelhincapie/ad790873da075d9ed51c1624add2939f to your computer and use it in GitHub Desktop.
Util extension function to send an event after X time.
const val DELAY_AFTER_TALK_BACK_GETS_STARTED = 400L
fun View.weakPostDelayed(delay: Long = DELAY_AFTER_TALK_BACK_GETS_STARTED, code: (View) -> Unit) {
postDelayed(WeakRunnable(this) {
code(it)
}, delay)
}
class WeakRunnable<T>(instance: T?, private val action: (T) -> Unit) : Runnable {
private val weakReference: WeakReference<T?> = WeakReference(instance)
override fun run() {
val instance: T? = weakReference.get()
if (instance != null) {
action(instance)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment