CoroutineScope.executeAsyncTask(
- onPreExecute: () -> Unit = {},
- doInBackground: suspend (suspend (P) -> Unit) -> R,
- onPostExecute: (R) -> Unit = {},
- onProgressUpdate: (P) -> Unit,
- onCancelled: () -> Unit = {}
- ) = launch {
- try {
- onPreExecute()
- val result = withContext(Dispatchers.IO) {
- doInBackground {
- withContext(Dispatchers.Main) { onProgressUpdate(it) }
- }
- }
- onPostExecute(result)
- } catch (e: CancellationException) {
- onCancelled()
- }
- }
-
- fun CoroutineScope.executeDelayedTask(
- task: () -> Unit,
- delayMillis: Long,
- onCancelled: () -> Unit = {}
- ) = launch {
- try {
- delay(delayMillis)
- withContext(Dispatchers.Main) { task() }
- } catch (e: CancellationException) {
- onCancelled()
- }
- }
-
- fun CoroutineScope.executeAsyncTask(task: AsyncTask, vararg arguments: A) = launch {
- try {
- task.onPreExecute()
- val result = withContext(Dispatchers.IO) {
- task.doInBackground(*arguments)
- }
- task.onPostExecute(result)
- } catch (e: CancellationException) {
- task.onCancelled()
- }
- }
-
- abstract class AsyncTask private constructor() {
- open fun onPreExecute() {}
- abstract suspend fun doInBackground(vararg argument: A): R
- open fun onPostExecute(result: R) {}
- open fun onCancelled() {}
- open fun onProgressUpdate(progress: P) {}
- protected fun publishProgress(progress: P) { onProgressUpdate(progress) }
-
- private var mJob: Job? = null
- val isCompleted: Boolean
- get() = mJob?.isCompleted ?: false
- val isActive: Boolean
- get() = mJob?.isActive ?: false
- val isCancelled: Boolean
- get() = mJob?.isCancelled ?: false
-
- fun execute(scope: CoroutineScope, vararg argument: A) {
- if (mJob != null && (!mJob!!.isCompleted || mJob!!.isActive)) {
- mJob!!.cancel()
- mJob = null
- }
- mJob = scope.executeAsyncTask(this, *argument)
- }
-
- fun cancel(exception: CancellationException? = null) {
- if (mJob != null && !mJob!!.isCancelled) {
- mJob!!.cancel(exception)
- }
- }
-
- fun cancel(message: String, cause: Throwable? = null) {
- if (mJob != null && !mJob!!.isCancelled) {
- mJob!!.cancel(message, cause)
- }
- }
-
- companion object {
-
- fun make(
- onPreExecute: () -> Unit = {},
- doInBackground: (Array CoroutineScope.executeAsyncTask(
+ onPreExecute: () -> Unit = {},
+ doInBackground: suspend (suspend (P) -> Unit) -> R,
+ onPostExecute: (R) -> Unit = {},
+ onProgressUpdate: (P) -> Unit,
+ onCancelled: () -> Unit = {}
+ ) = launch {
+ try {
+ onPreExecute()
+ val result = withContext(Dispatchers.IO) {
+ doInBackground {
+ withContext(Dispatchers.Main) { onProgressUpdate(it) }
+ }
+ }
+ onPostExecute(result)
+ } catch (e: CancellationException) {
+ onCancelled()
+ }
+ }
+
+ fun CoroutineScope.executeDelayedTask(
+ task: () -> Unit,
+ delayMillis: Long,
+ onCancelled: () -> Unit = {}
+ ) = launch {
+ try {
+ delay(delayMillis)
+ withContext(Dispatchers.Main) { task() }
+ } catch (e: CancellationException) {
+ onCancelled()
+ }
+ }
+
+ fun CoroutineScope.executeAsyncTask(task: AsyncTask, vararg arguments: A) = launch {
+ try {
+ task.onPreExecute()
+ val result = withContext(Dispatchers.IO) {
+ task.doInBackground(*arguments)
+ }
+ task.onPostExecute(result)
+ } catch (e: CancellationException) {
+ task.onCancelled()
+ }
+ }
+
+ abstract class AsyncTask private constructor() {
+ open fun onPreExecute() {}
+ abstract suspend fun doInBackground(vararg argument: A): R
+ open fun onPostExecute(result: R) {}
+ open fun onCancelled() {}
+ open fun onProgressUpdate(progress: P) {}
+ protected fun publishProgress(progress: P) { onProgressUpdate(progress) }
+
+ private var mJob: Job? = null
+ val isCompleted: Boolean
+ get() = mJob?.isCompleted ?: false
+ val isActive: Boolean
+ get() = mJob?.isActive ?: false
+ val isCancelled: Boolean
+ get() = mJob?.isCancelled ?: false
+
+ fun execute(scope: CoroutineScope, vararg argument: A) {
+ if (mJob != null && (!mJob!!.isCompleted || mJob!!.isActive)) {
+ mJob!!.cancel()
+ mJob = null
+ }
+ mJob = scope.executeAsyncTask(this, *argument)
+ }
+
+ fun cancel(exception: CancellationException? = null) {
+ if (mJob != null && !mJob!!.isCancelled) {
+ mJob!!.cancel(exception)
+ }
+ }
+
+ fun cancel(message: String, cause: Throwable? = null) {
+ if (mJob != null && !mJob!!.isCancelled) {
+ mJob!!.cancel(message, cause)
+ }
+ }
+
+ companion object {
+
+ fun make(
+ onPreExecute: () -> Unit = {},
+ doInBackground: (Array