Skip to content
Snippets Groups Projects
Commit e79de908 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

TV: Fix history display race condition

Fix #879
parent 8172a955
No related branches found
No related tags found
No related merge requests found
Pipeline #7085 passed with stage
in 3 minutes and 40 seconds
......@@ -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() }
}
}
}
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment