[Android] Clear callbacks on delegates/models death

Geoffrey Métais git at videolan.org
Mon Jun 22 15:03:42 CEST 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jun 19 18:16:44 2020 +0200| [59eefe9dba43ccd08a762880884771674fb391df] | committer: Geoffrey Métais

Clear callbacks on delegates/models death

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

 .../src/main/java/org/videolan/television/ui/NowPlayingDelegate.kt   | 5 ++++-
 .../vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt     | 5 ++++-
 .../vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt      | 5 ++++-
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/ui/NowPlayingDelegate.kt b/application/television/src/main/java/org/videolan/television/ui/NowPlayingDelegate.kt
index 1586a8b55..873fb8acc 100644
--- a/application/television/src/main/java/org/videolan/television/ui/NowPlayingDelegate.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/NowPlayingDelegate.kt
@@ -27,6 +27,7 @@ import androidx.lifecycle.viewModelScope
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
 import kotlinx.coroutines.flow.launchIn
+import kotlinx.coroutines.flow.onCompletion
 import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.launch
 import org.videolan.libvlc.MediaPlayer
@@ -44,7 +45,9 @@ class NowPlayingDelegate(private val model: MainTvModel): PlaybackService.Callba
 
     init {
         PlaylistManager.showAudioPlayer.observeForever(nowPlayingObserver)
-        PlaybackService.serviceFlow.onEach { onServiceChanged(it) }.launchIn(model.viewModelScope)
+        PlaybackService.serviceFlow.onEach { onServiceChanged(it) }
+                .onCompletion { service?.removeCallback(this at NowPlayingDelegate) }
+                .launchIn(model.viewModelScope)
     }
 
     fun onClear() {
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
index 993146d6d..b496fdfb3 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
@@ -27,6 +27,7 @@ import androidx.lifecycle.*
 import kotlinx.coroutines.*
 import kotlinx.coroutines.channels.actor
 import kotlinx.coroutines.flow.launchIn
+import kotlinx.coroutines.flow.onCompletion
 import kotlinx.coroutines.flow.onEach
 import org.videolan.medialibrary.Tools
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
@@ -61,7 +62,9 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
     }
 
     init {
-        PlaybackService.serviceFlow.onEach { onServiceChanged(it) }.launchIn(viewModelScope)
+        PlaybackService.serviceFlow.onEach { onServiceChanged(it) }
+                .onCompletion { onServiceChanged(null) }
+                .launchIn(viewModelScope)
     }
 
     private fun setup(service: PlaybackService) {
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 9eccf3bfc..e28c6bf0e 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt
@@ -27,6 +27,7 @@ import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.viewModelScope
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.launchIn
+import kotlinx.coroutines.flow.onCompletion
 import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
@@ -48,7 +49,9 @@ class StreamsModel(context: Context, private val showDummy: Boolean = false, cor
 
     init {
         if (medialibrary.isStarted) refresh()
-        PlaybackService.serviceFlow.onEach { onServiceChanged(it) }.launchIn(viewModelScope)
+        PlaybackService.serviceFlow.onEach { onServiceChanged(it) }
+                .onCompletion { onServiceChanged(null) }
+                .launchIn(viewModelScope)
     }
 
     override suspend fun updateList() {



More information about the Android mailing list