[Android] Remove custom scope for LifecycleOwner
Geoffrey Métais
git at videolan.org
Tue Nov 26 15:38:28 CET 2019
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Nov 21 17:23:21 2019 +0100| [efdd27199eea158d6efafecbea49970525d4066b] | committer: Geoffrey Métais
Remove custom scope for LifecycleOwner
> https://code.videolan.org/videolan/vlc-android/commit/efdd27199eea158d6efafecbea49970525d4066b
---
.../java/org/videolan/tools/KotlinExtensions.kt | 26 ++++------------------
.../src/org/videolan/vlc/gui/AboutFragment.kt | 4 ++--
.../dialogs/SubtitleDownloaderDialogFragment.kt | 4 ++--
.../videolan/vlc/gui/network/MRLPanelFragment.kt | 4 ++--
.../src/org/videolan/vlc/gui/tv/SearchFragment.kt | 4 ++--
5 files changed, 12 insertions(+), 30 deletions(-)
diff --git a/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt b/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
index fc94db43d..d8be7cb74 100644
--- a/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
+++ b/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
@@ -13,30 +13,12 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
-import kotlinx.coroutines.*
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.actor
-
-fun LifecycleOwner.createJob(cancelEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY): Job = Job().also { job ->
- lifecycle.addObserver(object : LifecycleObserver {
- @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
- fun clear() {
- lifecycle.removeObserver(this)
- job.cancel()
- }
- })
-}
-
-private val lifecycleCoroutineScopes = mutableMapOf<Lifecycle, CoroutineScope>()
-
- at ExperimentalCoroutinesApi
-val LifecycleOwner.coroutineScope: CoroutineScope
- get() = lifecycleCoroutineScopes[lifecycle] ?: createJob().let {
- val newScope = CoroutineScope(it + Dispatchers.Main.immediate)
- lifecycleCoroutineScopes[lifecycle] = newScope
- it.invokeOnCompletion { lifecycleCoroutineScopes -= lifecycle }
- newScope
- }
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.yield
fun <T> List<T>.getposition(target: T): Int {
for ((index, item) in withIndex()) if (item == target) return index
diff --git a/vlc-android/src/org/videolan/vlc/gui/AboutFragment.kt b/vlc-android/src/org/videolan/vlc/gui/AboutFragment.kt
index 7e6b60ca5..ceaf074be 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AboutFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/AboutFragment.kt
@@ -32,13 +32,13 @@ import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
+import androidx.lifecycle.lifecycleScope
import androidx.viewpager.widget.ViewPager
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.tabs.TabLayout
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.launch
-import org.videolan.tools.coroutineScope
import org.videolan.tools.setGone
import org.videolan.vlc.BuildConfig
import org.videolan.vlc.R
@@ -76,7 +76,7 @@ class AboutFragment : Fragment() {
visibility = View.VISIBLE
setupWithViewPager(viewPager)
}
- coroutineScope.launch {
+ lifecycleScope.launch {
UiTools.fillAboutView(view)
webView.loadUrl("file:///android_asset/licence.htm")
diff --git a/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt b/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
index 11ddb4f84..5d8b3323a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
@@ -15,10 +15,10 @@ import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.ViewModelProviders
+import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.isActive
-import org.videolan.tools.coroutineScope
import org.videolan.vlc.R
import org.videolan.vlc.databinding.SubtitleDownloaderDialogBinding
import org.videolan.vlc.gui.DialogActivity
@@ -38,7 +38,7 @@ class SubtitleDownloaderDialogFragment : DialogFragment() {
private lateinit var viewModel: SubtitlesModel
private lateinit var toast: Toast
- val listEventActor = coroutineScope.actor<SubtitleEvent> {
+ val listEventActor = lifecycleScope.actor<SubtitleEvent> {
for (subtitleEvent in channel) if (isActive) when (subtitleEvent) {
is Click -> when (subtitleEvent.item.state) {
State.NotDownloaded -> VLCDownloadManager.download(requireActivity(), subtitleEvent.item)
diff --git a/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt b/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
index 75743a210..705357448 100644
--- a/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
@@ -36,6 +36,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
+import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
@@ -47,7 +48,6 @@ import kotlinx.coroutines.channels.actor
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.tools.copy
-import org.videolan.tools.coroutineScope
import org.videolan.vlc.R
import org.videolan.vlc.databinding.MrlPanelBinding
import org.videolan.vlc.gui.ContentActivity
@@ -71,7 +71,7 @@ class MRLPanelFragment : Fragment(), View.OnKeyListener, TextView.OnEditorAction
private lateinit var editText: com.google.android.material.textfield.TextInputLayout
private lateinit var viewModel: StreamsModel
- private val listEventActor = coroutineScope.actor<MrlAction> {
+ private val listEventActor = lifecycleScope.actor<MrlAction> {
for (event in channel) when (event) {
is Playmedia -> playMedia(event.media)
is ShowContext -> showContext(event.position)
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/SearchFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/SearchFragment.kt
index 3b9fc1978..481fd426d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/SearchFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/SearchFragment.kt
@@ -29,11 +29,11 @@ import android.os.Bundle
import android.text.TextUtils
import androidx.leanback.app.SearchSupportFragment
import androidx.leanback.widget.*
+import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.medialibrary.media.MediaLibraryItem
-import org.videolan.tools.coroutineScope
import org.videolan.vlc.R
import org.videolan.vlc.util.getFromMl
@@ -76,7 +76,7 @@ class SearchFragment : SearchSupportFragment(), SearchSupportFragment.SearchResu
return true
}
- private fun loadRows(query: String?) = coroutineScope.launch {
+ private fun loadRows(query: String?) = lifecycleScope.launch {
if (query == null || query.length < 3) return at launch
val searchAggregate = context?.getFromMl { search(query) }
val empty = searchAggregate == null || searchAggregate.isEmpty
More information about the Android
mailing list