Skip to content

Instantly share code, notes, and snippets.

@radoyankov
Last active January 11, 2020 10:29
Show Gist options
  • Save radoyankov/f6f62882f37abf706dcfac9ad57e7b09 to your computer and use it in GitHub Desktop.
Save radoyankov/f6f62882f37abf706dcfac9ad57e7b09 to your computer and use it in GitHub Desktop.
Delayed Handler implementation on Kotlin
class Do {
enum class duration {
SECONDS, MILLISECONDS
}
infix fun seconds(function: () -> Unit) {
Do.mFormat = duration.SECONDS
Do.handle(function)
}
infix fun milliseconds(function: () -> Unit) {
Do.mFormat = duration.MILLISECONDS
Do.handle(function)
}
companion object {
var mDuration = 0
var mFormat: duration = duration.MILLISECONDS
infix fun now(function: () -> Unit) {
function()
}
fun handle(function: () -> Unit) {
Handler().postDelayed(function,
when (mFormat) {
duration.SECONDS -> mDuration * 1000L
duration.MILLISECONDS -> (mDuration).toLong()
else -> (mDuration).toLong()
})
}
infix fun after(seconds: Int): Do {
Do.mDuration = seconds
return Do()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment