-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated to sdk 30, moved sample from asyncTask to coroutines
- Loading branch information
1 parent
bccb493
commit 1d3530f
Showing
7 changed files
with
45 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 28 additions & 6 deletions
34
sampleapp/src/main/java/com/kirkbushman/sampleapp/util/doAsync.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,40 @@ | ||
package com.kirkbushman.sampleapp.util | ||
|
||
import android.os.AsyncTask | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class doAsync( | ||
|
||
private val doWork: () -> Unit, | ||
private val onPre: (() -> Unit)? = null, | ||
private val onPost: (() -> Unit)? = null | ||
) : CoroutineScope { | ||
|
||
private val job = Job() | ||
override val coroutineContext: CoroutineContext | ||
get() = Dispatchers.Main + job | ||
|
||
class doAsync(private val doWork: () -> Unit, private val onPost: (() -> Unit)? = null) : AsyncTask<Void, Void, Void>() { | ||
init { | ||
execute() | ||
} | ||
|
||
override fun doInBackground(vararg params: Void?): Void? { | ||
doWork() | ||
return null | ||
fun cancel() { | ||
job.cancel() | ||
} | ||
|
||
override fun onPostExecute(result: Void?) { | ||
private fun execute() = launch { | ||
|
||
onPre?.invoke() | ||
doInBackground() | ||
onPost?.invoke() | ||
} | ||
|
||
private suspend fun doInBackground() = withContext(Dispatchers.IO) { | ||
|
||
doWork.invoke() | ||
} | ||
} |