aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/util/BackgroundTask.kt
blob: 8b180941908a4a0c03689fd14d7a9cf39da54c46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
        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)
        }
    }

    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!!()
            }
            super.onPostExecute(result)
        }

        override fun doInBackground(vararg params: Void): Result? {
            try {
                return this@BackgroundTask.doInBackground()
            } catch (ex: Exception) {
                fail(ex)
                return null
            }

        }
    }
}