[Android] CoroutineScope helper for LifecycleOwner classes

Geoffrey Métais git at videolan.org
Mon Sep 17 11:21:10 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Sep 17 11:15:52 2018 +0200| [773b5a174e76148dcc389e395bb11b821a56d8be] | committer: Geoffrey Métais

CoroutineScope helper for LifecycleOwner classes

> https://code.videolan.org/videolan/vlc-android/commit/773b5a174e76148dcc389e395bb11b821a56d8be
---

 tools/build.gradle                                 |  3 +++
 .../java/org/videolan/tools/KotlinExtensions.kt    | 29 ++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/tools/build.gradle b/tools/build.gradle
index 74d351dcd..b467ed011 100644
--- a/tools/build.gradle
+++ b/tools/build.gradle
@@ -30,6 +30,9 @@ dependencies {
     api "com.android.support:appcompat-v7:$rootProject.ext.appCompatVersion"
     api "com.android.support:support-tv-provider:$rootProject.ext.appCompatVersion"
     implementation fileTree(dir: 'libs', include: ['*.jar'])
+    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.ext.kotlinx_version"
+    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.ext.kotlinx_version"
 
     testImplementation "junit:junit:$rootProject.ext.junitVersion"
     androidTestImplementation "com.android.support.test:runner:$rootProject.ext.supportTest"
diff --git a/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt b/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
new file mode 100644
index 000000000..5fa1d0b94
--- /dev/null
+++ b/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
@@ -0,0 +1,29 @@
+package org.videolan.tools
+
+import android.arch.lifecycle.GenericLifecycleObserver
+import android.arch.lifecycle.Lifecycle
+import android.arch.lifecycle.LifecycleOwner
+import kotlinx.coroutines.experimental.CoroutineScope
+import kotlinx.coroutines.experimental.Dispatchers
+import kotlinx.coroutines.experimental.Job
+import kotlinx.coroutines.experimental.android.Main
+
+fun LifecycleOwner.createJob(cancelEvent: Lifecycle.Event = Lifecycle.Event.ON_STOP): Job = Job().also { job ->
+    lifecycle.addObserver(object : GenericLifecycleObserver {
+        override fun onStateChanged(source: LifecycleOwner?, event: Lifecycle.Event) {
+            if (event == cancelEvent) {
+                lifecycle.removeObserver(this)
+                job.cancel()
+            }
+        }
+    })
+}
+private val lifecycleCoroutineScopes = mutableMapOf<Lifecycle, CoroutineScope>()
+
+val LifecycleOwner.coroutineScope: CoroutineScope
+    get() = lifecycleCoroutineScopes[lifecycle] ?: createJob().let {
+        val newScope = CoroutineScope(it + Dispatchers.Main.immediate)
+        lifecycleCoroutineScopes[lifecycle] = newScope
+        it.invokeOnCompletion { _ -> lifecycleCoroutineScopes -= lifecycle }
+        newScope
+    }
\ No newline at end of file



More information about the Android mailing list