Last active
January 11, 2020 10:29
-
-
Save radoyankov/f6f62882f37abf706dcfac9ad57e7b09 to your computer and use it in GitHub Desktop.
Delayed Handler implementation on Kotlin
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
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