[Android] Make the CallBackDelegate pausable to avoid refreshes while the UI is not shown

Nicolas Pomepuy git at videolan.org
Thu Mar 17 16:26:05 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Mar 17 15:29:01 2022 +0100| [2ed65b9048868eca90affb32da003bf23b2a512d] | committer: Duncan McNamara

Make the CallBackDelegate pausable to avoid refreshes while the UI is not shown

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

 .../vlc/gui/browser/MediaBrowserFragment.kt        | 11 ++++++++
 .../src/org/videolan/vlc/media/PlaylistManager.kt  |  2 +-
 .../videolan/vlc/viewmodels/CallBackDelegate.kt    | 31 +++++++++++++++++++++-
 3 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
index da0d29388..2e515fdc3 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
@@ -101,6 +101,17 @@ abstract class MediaBrowserFragment<T : SortableModel> : BaseFragment(), Filtera
         releaseBreadCrumb()
     }
 
+    override fun onResume() {
+        super.onResume()
+        (viewModel as? MedialibraryViewModel)?.resume()
+    }
+
+
+    override fun onPause() {
+        super.onPause()
+        (viewModel as? MedialibraryViewModel)?.pause()
+    }
+
     override fun onDestroy() {
         super.onDestroy()
         savedSelection.clear()
diff --git a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 0b59daa2c..a521e202e 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -999,7 +999,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                     }
                     if (player.getCurrentTime() % 10 == 0L) savePosition()
                     val now = System.currentTimeMillis()
-                    if (now - lastTimeMetaSaved > 20000){
+                    if (now - lastTimeMetaSaved > 5000L){
                         lastTimeMetaSaved = now
                         saveMediaMeta()
                     }
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
index 1610c909b..0d1b08170 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
@@ -44,6 +44,8 @@ interface ICallBackHandler {
     fun watchPlaylists()
     fun watchHistory()
     fun watchMediaGroups()
+    fun pause()
+    fun resume()
 }
 
 class CallBackDelegate : ICallBackHandler,
@@ -69,9 +71,36 @@ class CallBackDelegate : ICallBackHandler,
     private var playlistsCb = false
     private var historyCb = false
     private var mediaGroupsCb = false
+    var paused = false
+        set(value) {
+            field = value
+            if (!value && isInvalid) {
+                refreshActor.trySend(Unit)
+                isInvalid = false
+            }
+        }
+    var isInvalid = false
+
+    /**
+     * Pause the callbacks while the caller is paused to avoid unwanted refreshes
+     * During this time, instead of refreshing, it's marked as invalid.
+     * If invalid, a refresh is launched upon resuming
+     */
+    override fun pause() {
+        paused = true
+    }
+
+    /**
+     * Resumes the callback and refresh if it has been marked invalid in the meantime
+     */
+    override fun resume() {
+        paused = false
+    }
 
     override fun CoroutineScope.registerCallBacks(refresh: () -> Unit) {
-        refreshActor = conflatedActor { refresh() }
+        refreshActor = conflatedActor {
+           if (paused) isInvalid = true else refresh()
+        }
         deleteActor = actor(context = Dispatchers.IO, capacity = Channel.UNLIMITED) {
             for (action in channel) when (action) {
                 is MediaDeletedAction -> {



More information about the Android mailing list