Created
October 30, 2017 09:41
-
-
Save piruin/d515716e234fd998b21e34b6334eb5c2 to your computer and use it in GitHub Desktop.
Kotlin's Extension for Easy Handler Thread
This file contains hidden or 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
package org.tanrabad.survey.larvaecam | |
import android.app.Fragment | |
import android.content.Context | |
import android.os.AsyncTask | |
import android.os.Handler | |
import android.os.Looper | |
import android.view.View | |
inline fun Context.runOnWorkerThread(crossinline task: () -> Unit) { | |
AsyncTask.execute { task() } | |
} | |
inline fun Context.runOnUiThread(delay: Long, crossinline f: () -> Unit) { | |
Handler(Looper.getMainLooper()).postDelayed({ f() }, delay) | |
} | |
inline fun View.runOnWorkerThread(crossinline task: () -> Unit) = context.runOnWorkerThread(task) | |
inline fun Fragment.runOnWorkerThread(crossinline task: () -> Unit) = activity.runOnWorkerThread(task) | |
inline fun View.runOnUiThread(delay: Long, crossinline f: () -> Unit) = context.runOnUiThread(delay, f) | |
inline fun Fragment.runOnUiThread(delay: Long, crossinline f: () -> Unit) = activity.runOnUiThread(delay, f) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment