diff options
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence/util/BackgroundTask.kt')
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/util/BackgroundTask.kt | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/util/BackgroundTask.kt b/app/src/main/java/net/lacolaco/smileessence/util/BackgroundTask.kt new file mode 100644 index 00000000..7b9e188a --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/util/BackgroundTask.kt @@ -0,0 +1,113 @@ +package net.lacolaco.smileessence.util + +import android.os.AsyncTask +import net.lacolaco.smileessence.logging.Logger + +abstract class BackgroundTask<Result, Progress> { + private val task: InnerAsyncTask + private var cbThen: ((Result) -> Unit)? = null + private var cbProgress: ((Progress) -> Unit)? = null + private var cbFail: ((Exception) -> Unit)? = null + private var cbFinish: (() -> Unit)? = null + private var exception: Exception? = null + + init { + this.task = InnerAsyncTask() + } + + fun onDone(cb: (Result) -> Unit): BackgroundTask<Result, Progress> { + this.cbThen = cb + return this + } + + fun onProgress(cb: (Progress) -> Unit): BackgroundTask<Result, Progress> { + this.cbProgress = cb + return this + } + + fun onFail(cb: (Exception) -> Unit): BackgroundTask<Result, Progress> { + this.cbFail = cb + return this + } + + fun onFinish(cb: () -> Unit): BackgroundTask<Result, Progress> { + this.cbFinish = cb + return this + } + + fun onDoneUI(cb: (Result) -> Unit): BackgroundTask<Result, Progress> { + return onDone({ r -> UIHandler().post { cb(r) } }) + } + + fun onProgressUI(cb: (Progress) -> Unit): BackgroundTask<Result, Progress> { + return onProgress({ p -> UIHandler().post { cb(p) } }) + } + + fun onFailUI(cb: (Exception) -> Unit): BackgroundTask<Result, Progress> { + return onFail({ e -> UIHandler().post { cb(e) } }) + } + + fun onFinishUI(cb: () -> Unit): BackgroundTask<Result, Progress> { + return onFinish({ UIHandler().post(cb) }) + } + + fun cancel(): Boolean { + return task.cancel(true) + } + + fun execute(): BackgroundTask<Result, Progress> { + task.execute() + return this + } + + val immediately: Result + @Throws(Exception::class) + get() { + val result = task.get() + return if (exception == null) { + result + } else { + throw exception!! + } + } + + protected fun fail(ex: Exception) { + exception = ex + ex.printStackTrace() + Logger.error(ex) + + if (!task.isCancelled && cbFail != null) { + cbFail!!(ex) + } + } + + protected fun progress(value: Progress) { + if (!task.isCancelled && exception == null && cbProgress != null) { + cbProgress!!(value) + } + } + + @Throws(Exception::class) + protected abstract fun doInBackground(): Result + + private inner class InnerAsyncTask : AsyncTask<Void, Progress, Result>() { + override fun onPostExecute(result: Result) { + if (!isCancelled && exception == null && cbThen != null) { + cbThen!!(result) + } + if (cbFinish != null) { + cbFinish!!() + } + } + + override fun doInBackground(vararg params: Void): Result? { + try { + return this@BackgroundTask.doInBackground() + } catch (ex: Exception) { + fail(ex) + return null + } + + } + } +} |