diff options
author | YuutaW <17158086+Trumeet@users.noreply.github.com> | 2019-02-24 11:59:17 -0800 |
---|---|---|
committer | YuutaW <17158086+Trumeet@users.noreply.github.com> | 2019-02-24 11:59:17 -0800 |
commit | a08328403be84d85c006f801169a3feed0d956a4 (patch) | |
tree | ceebece6443a3e6662a4937b911c58904bb5b1ff /app/src/main/java/moe/yuuta/workmode/async | |
download | WorkMode-a08328403be84d85c006f801169a3feed0d956a4.tar WorkMode-a08328403be84d85c006f801169a3feed0d956a4.tar.gz WorkMode-a08328403be84d85c006f801169a3feed0d956a4.tar.bz2 WorkMode-a08328403be84d85c006f801169a3feed0d956a4.zip |
First Commit
Signed-off-by: YuutaW <17158086+Trumeet@users.noreply.github.com>
Diffstat (limited to 'app/src/main/java/moe/yuuta/workmode/async')
4 files changed, 87 insertions, 0 deletions
diff --git a/app/src/main/java/moe/yuuta/workmode/async/Async.kt b/app/src/main/java/moe/yuuta/workmode/async/Async.kt new file mode 100644 index 0000000..41542cd --- /dev/null +++ b/app/src/main/java/moe/yuuta/workmode/async/Async.kt @@ -0,0 +1,47 @@ +package moe.yuuta.workmode.async + +import android.annotation.SuppressLint +import android.os.AsyncTask + +object Async { + fun <T> beginTask(runnable: Runnable<T>, callback: Callback<T>): Stoppable { + val task = @SuppressLint("StaticFieldLeak") + object : AsyncTask<Void, Void, TaskResult<T>>() { + override fun onPostExecute(result: TaskResult<T>) { + callback.onStop(result.successful, result.result, result.e) + } + + override fun doInBackground(vararg params: Void?): TaskResult<T> { + try { + return TaskResult(true, runnable.run(), null) + } catch (e: Throwable) { + return TaskResult(false, null, e) + } + } + + override fun onPreExecute() { + callback.onStart() + } + } + val stoppable = object : Stoppable { + override fun stop() { + if (!isStopped()) task.cancel(true) + } + + override fun isStopped(): Boolean = task.isCancelled + } + task.execute() + return stoppable + } +} + +@FunctionalInterface +interface Runnable<T> { + fun run(): T? +} + +data class TaskResult<T>( + val successful: Boolean, + val result: T?, + val e: Throwable? +)
\ No newline at end of file diff --git a/app/src/main/java/moe/yuuta/workmode/async/Callback.kt b/app/src/main/java/moe/yuuta/workmode/async/Callback.kt new file mode 100644 index 0000000..bac767b --- /dev/null +++ b/app/src/main/java/moe/yuuta/workmode/async/Callback.kt @@ -0,0 +1,6 @@ +package moe.yuuta.workmode.async + +interface Callback<T> { + fun onStart() {} + fun onStop(success: Boolean, result: T?, e: Throwable?) {} +}
\ No newline at end of file diff --git a/app/src/main/java/moe/yuuta/workmode/async/Stoppable.kt b/app/src/main/java/moe/yuuta/workmode/async/Stoppable.kt new file mode 100644 index 0000000..36e0335 --- /dev/null +++ b/app/src/main/java/moe/yuuta/workmode/async/Stoppable.kt @@ -0,0 +1,6 @@ +package moe.yuuta.workmode.async + +interface Stoppable { + fun stop() + fun isStopped(): Boolean +}
\ No newline at end of file diff --git a/app/src/main/java/moe/yuuta/workmode/async/StoppableGroup.kt b/app/src/main/java/moe/yuuta/workmode/async/StoppableGroup.kt new file mode 100644 index 0000000..ef651fe --- /dev/null +++ b/app/src/main/java/moe/yuuta/workmode/async/StoppableGroup.kt @@ -0,0 +1,28 @@ +package moe.yuuta.workmode.async + +class StoppableGroup : Stoppable { + private val innerList: MutableList<Stoppable> = mutableListOf() + + fun add(stoppable: Stoppable): StoppableGroup { + innerList.add(stoppable) + return this + } + + fun remove(stoppable: Stoppable): StoppableGroup { + innerList.remove(stoppable) + return this + } + + override fun stop() { + innerList.stream().forEach { + it.stop() + } + } + + override fun isStopped(): Boolean { + for (stoppable in innerList) { + if (!stoppable.isStopped()) return false + } + return true + } +}
\ No newline at end of file |