[Android] TV: Fix history display race condition

Geoffrey Métais git at videolan.org
Tue May 21 15:31:12 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue May 21 15:30:11 2019 +0200| [e79de908212cca9c355c0b1d6c1348ca7b7ad0ba] | committer: Geoffrey Métais

TV: Fix history display race condition

Fix #879

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

 .../org/videolan/vlc/gui/tv/NowPlayingDelegate.kt  |  7 +++--
 .../org/videolan/vlc/viewmodels/tv/MainTvModel.kt  | 30 ++++++++++++----------
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/NowPlayingDelegate.kt b/vlc-android/src/org/videolan/vlc/gui/tv/NowPlayingDelegate.kt
index 84a4057f3..dfe086a31 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/NowPlayingDelegate.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/NowPlayingDelegate.kt
@@ -25,6 +25,7 @@ package org.videolan.vlc.gui.tv
 import androidx.lifecycle.Observer
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
+import kotlinx.coroutines.launch
 import org.videolan.libvlc.MediaPlayer
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.util.EmptyPBSCallback
@@ -61,7 +62,9 @@ class NowPlayingDelegate(private val model: MainTvModel): PlaybackService.Callba
     }
 
     private fun updateCurrent() {
-        model.updateAudioCategories()
-        model.updateHistory()
+        model.run {
+            updateAudioCategories()
+            if (showHistory) launch { updateHistory() }
+        }
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/tv/MainTvModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/tv/MainTvModel.kt
index 6a1757e33..fcf6f8273 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/tv/MainTvModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/tv/MainTvModel.kt
@@ -52,6 +52,7 @@ import org.videolan.vlc.repository.DirectoryRepository
 import org.videolan.vlc.util.*
 
 private const val NUM_ITEMS_PREVIEW = 5
+private const val TAG = "MainTvModel"
 
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
@@ -64,7 +65,8 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
     private val showInternalStorage = AndroidDevices.showInternalStorage()
     private val browserFavRepository = BrowserFavRepository.getInstance(context)
     private var updatedFavoritList: List<MediaWrapper> = listOf()
-    private var showHistory = false
+    var showHistory = false
+    private set
     // LiveData
     private val favorites: LiveData<List<BrowserFav>> = browserFavRepository.browserFavorites
     val videos : LiveData<List<MediaLibraryItem>> = MutableLiveData()
@@ -78,6 +80,10 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
         for (action in channel) updateBrowsers()
     }
 
+    private val historyActor = actor<Unit>(capacity = Channel.CONFLATED) {
+        for (action in channel) setHistory()
+    }
+
     private val favObserver = Observer<List<BrowserFav>> { list ->
         updatedFavoritList = convertFavorites(list)
         if (!updateActor.isClosedForSend) updateActor.offer(Unit)
@@ -99,24 +105,22 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
 
     fun refresh() = launch {
         updateAudioCategories()
-        updateActor.offer(Unit)
         updateVideos()
-        setHistory()
+        historyActor.offer(Unit)
+        updateActor.offer(Unit)
     }
 
-    private fun setHistory() {
+    private suspend fun setHistory() {
+        if (!medialibrary.isStarted) return
         val historyEnabled = settings.getBoolean(PLAYBACK_HISTORY, true)
-        if (showHistory != historyEnabled) {
-            showHistory = historyEnabled
-            if (!historyEnabled) (history as MutableLiveData).value = emptyList()
-            else updateHistory()
-        }
+        showHistory = historyEnabled
+        if (!historyEnabled) (history as MutableLiveData).value = emptyList()
+        else updateHistory()
     }
 
-    fun updateHistory() {
-        if (showHistory) launch {
-            (history as MutableLiveData).value = withContext(Dispatchers.Default) { medialibrary.lastMediaPlayed().toMutableList() }
-        }
+    suspend fun updateHistory() {
+        if (!showHistory) return
+        (history as MutableLiveData).value = context.getFromMl { lastMediaPlayed().toMutableList() }
     }
 
     private fun updateVideos() = launch {



More information about the Android mailing list