[Android] Streams: Fix list update

Geoffrey Métais git at videolan.org
Tue Mar 24 09:41:08 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Mar 19 16:40:57 2020 +0100| [cfeb5d9f4e4d7ee003ebad200a000f81ef30d0b2] | committer: Nicolas Pomepuy

Streams: Fix list update

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

 .../videolan/vlc/gui/network/MRLPanelFragment.kt   |  1 -
 .../org/videolan/vlc/viewmodels/StreamsModel.kt    | 27 +++++++++++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
index 6316ab4b4..e33f7f9f5 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
@@ -158,7 +158,6 @@ class MRLPanelFragment : Fragment(), View.OnKeyListener, TextView.OnEditorAction
         mw.type = MediaWrapper.TYPE_STREAM
         if (mw.uri.scheme?.startsWith("rtsp") == true) VideoPlayerActivity.start(requireContext(), mw.uri)
         else MediaUtils.openMedia(activity, mw)
-        viewModel.refresh()
         activity?.invalidateOptionsMenu()
         UiTools.setKeyboardVisibility(editText, false)
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt
index 1b70532ff..3b5221494 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt
@@ -26,22 +26,29 @@ import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.viewModelScope
 import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.launchIn
+import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
+import org.videolan.libvlc.MediaPlayer
+import org.videolan.libvlc.interfaces.IMedia
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.resources.util.getFromMl
 import org.videolan.tools.CoroutineContextProvider
+import org.videolan.vlc.PlaybackService
+import org.videolan.vlc.util.EmptyPBSCallback
 
 
 class StreamsModel(context: Context, coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) : MedialibraryModel<MediaWrapper>(context, coroutineContextProvider) {
     var deletingMedia: MediaWrapper? = null
     val observableSearchText = ObservableField<String>()
+    var service: PlaybackService? = null
 
     init {
         if (medialibrary.isStarted) refresh()
+        PlaybackService.serviceFlow.onEach { onServiceChanged(it) }.launchIn(viewModelScope)
     }
 
-
     override suspend fun updateList() {
         dataset.value = withContext(coroutineContextProvider.Default) { medialibrary.lastStreamsPlayed().toMutableList().also { deletingMedia?.let { remove(it) } } }
     }
@@ -62,6 +69,24 @@ class StreamsModel(context: Context, coroutineContextProvider: CoroutineContextP
             }
         }
     }
+
+    private fun onServiceChanged(service: PlaybackService?) {
+        if (this.service == service) return
+        if (service != null) {
+            service.addCallback(serviceCb)
+            this.service = service
+        } else {
+            this.service?.apply { removeCallback(serviceCb) }
+            this.service = null
+        }
+    }
+
+    private val serviceCb = object : PlaybackService.Callback by EmptyPBSCallback {
+        override fun update() {
+            refresh()
+        }
+    }
+
     class Factory(private val context: Context) : ViewModelProvider.Factory {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             @Suppress("UNCHECKED_CAST")



More information about the Android mailing list