Skip to content

Instantly share code, notes, and snippets.

@piruin
Created October 30, 2017 09:41
Show Gist options
  • Save piruin/d515716e234fd998b21e34b6334eb5c2 to your computer and use it in GitHub Desktop.
Save piruin/d515716e234fd998b21e34b6334eb5c2 to your computer and use it in GitHub Desktop.
Kotlin's Extension for Easy Handler Thread
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