Created
April 4, 2020 03:49
-
-
Save miguelhincapie/ad790873da075d9ed51c1624add2939f to your computer and use it in GitHub Desktop.
Util extension function to send an event after X time.
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
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