aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-24 20:05:06 -0500
committerGitHub <noreply@github.com>2018-12-24 20:05:06 -0500
commit8447b1ae8ce89b3f1bbe79dbae8847d901831c12 (patch)
treece516950e452581766e905ead32970d891bb46f6 /core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt
parent701b94ab09ff53aca682fac6c4ef5364566339be (diff)
downloadkau-8447b1ae8ce89b3f1bbe79dbae8847d901831c12.tar.gz
kau-8447b1ae8ce89b3f1bbe79dbae8847d901831c12.tar.bz2
kau-8447b1ae8ce89b3f1bbe79dbae8847d901831c12.zip
Enhancement/coroutines (#180)
* Add coroutine dependency * Add coroutines to kprefactivity * Change base job to supervisor * Update coroutines for faq * Update changelog * Use preloading in media picker core * Make test logging internal * Remove anko
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt30
1 files changed, 28 insertions, 2 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt b/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt
index 85e711b..bf977f2 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt
@@ -15,8 +15,14 @@
*/
package ca.allanwang.kau.internal
+import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import ca.allanwang.kau.permissions.kauOnRequestPermissionsResult
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.SupervisorJob
+import kotlin.coroutines.CoroutineContext
/**
* Created by Allan Wang on 2017-08-01.
@@ -26,11 +32,31 @@ import ca.allanwang.kau.permissions.kauOnRequestPermissionsResult
* Ensures that some singleton methods are called.
* This is simply a convenience class;
* you can always copy and paste this to your own class.
+ *
+ * This also implements [CoroutineScope] that adheres to the activity lifecycle.
+ * Note that by default, [SupervisorJob] is used, to avoid exceptions in one child from affecting that of another.
+ * The default job can be overridden within [defaultJob]
*/
-abstract class KauBaseActivity : AppCompatActivity() {
+abstract class KauBaseActivity : AppCompatActivity(), CoroutineScope {
+
+ open lateinit var job: Job
+ override val coroutineContext: CoroutineContext
+ get() = Dispatchers.Main + job
+
+ open fun defaultJob(): Job = SupervisorJob()
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ job = defaultJob()
+ }
+
+ override fun onDestroy() {
+ job.cancel()
+ super.onDestroy()
+ }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
kauOnRequestPermissionsResult(permissions, grantResults)
}
-}
+} \ No newline at end of file