aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/moe/yuuta/workmode/suspend/AsyncSuspender.kt
blob: 8cb49479b590727594cbfc96949dec2cf88453e1 (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
package moe.yuuta.workmode.suspend

import android.content.Context
import moe.yuuta.workmode.async.Async
import moe.yuuta.workmode.async.Callback
import moe.yuuta.workmode.async.Runnable
import moe.yuuta.workmode.async.Stoppable

/**
 * An async suspender which wraps suspend tasks and run them in the background
 */
class AsyncSuspender(private val mContext: Context) {
    fun suspend(packageNames: Array<String>, suspended: Boolean, callback: Callback<Array<String>>): Stoppable =
            Async.beginTask(object : Runnable<Array<String>> {
                override fun run(): Array<String> = Suspender(mContext).suspend(packageNames, suspended)
            }, callback)

    fun isSuspended(packageNames: Array<String>, callback: Callback<Boolean>): Stoppable =
            Async.beginTask(object : Runnable<Boolean> {
                override fun run(): Boolean = Suspender(mContext).isSuspended(packageNames)
            }, callback)

    fun applyFromSettings(callback: Callback<Unit>): Stoppable =
            Async.beginTask(object : Runnable<Unit> {
                override fun run(): Unit = Suspender(mContext).applyFromSettings()
            }, callback)
}