[Android] TV: Loading display in browsers

Geoffrey Métais git at videolan.org
Mon Jan 7 17:22:02 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jan  7 17:21:04 2019 +0100| [691890d62600e7b3802c372ea3231a264d7f3e1d] | committer: Geoffrey Métais

TV: Loading display in browsers

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

 .../src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt       | 2 ++
 vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt       | 6 ++++++
 vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt   | 4 ++++
 vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt            | 2 ++
 vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt | 2 ++
 5 files changed, 16 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt
index 7936bbbb6..c58260ef1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt
@@ -31,6 +31,7 @@ import androidx.core.content.ContextCompat
 import androidx.leanback.app.BackgroundManager
 import androidx.leanback.app.BrowseSupportFragment
 import androidx.leanback.widget.*
+import androidx.lifecycle.Observer
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.R
@@ -72,6 +73,7 @@ open class CategoriesFragment<T : BaseModel<out MediaLibraryItem>> : BrowseSuppo
         backgroundManager.attachToView(view)
         searchAffordanceColor = ContextCompat.getColor(requireContext(), R.color.orange500)
         requireActivity().findViewById<ImageView>(R.id.icon).setImageResource(R.drawable.ic_menu_sort)
+        if (this::viewModel.isInitialized) viewModel.loading.observe(this, Observer { (activity as? VerticalGridActivity)?.showProgress(it) })
     }
 
     override fun onStart() {
diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index 4cc3e1ca7..2c1990be5 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -50,6 +50,7 @@ const val TAG = "VLC/BrowserProvider"
 abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<MediaLibraryItem>, val url: String?, private val showHiddenFiles: Boolean) : EventListener, CoroutineScope {
 
     override val coroutineContext = Dispatchers.Main.immediate
+    val loading = MutableLiveData<Boolean>().apply { value = false }
 
     protected val mutex= Mutex()
     protected var mediabrowser: MediaBrowser? = null
@@ -95,6 +96,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
     }
 
     protected open fun browse(url: String? = null) {
+        loading.value = true
         if (!browserActor.isClosedForSend) browserActor.offer(Browse(url))
     }
 
@@ -105,6 +107,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
             for (media in browserChannel) findMedia(media)?.let { addMedia(it) }
             if (dataset.value.isNotEmpty()) parseSubDirectories()
             else dataset.clear() // send observable event when folder is empty
+            loading.value = false
         }
     }
 
@@ -112,6 +115,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
 
     open fun refresh() : Boolean {
         if (url === null || browserActor.isClosedForSend) return false
+        loading.value = true
         browserActor.offer(Refresh)
         return true
     }
@@ -126,6 +130,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         job = launch {
             dataset.value = browserChannel.mapNotNullTo(mutableListOf()) { findMedia(it) }
             parseSubDirectories()
+            loading.value = false
         }
     }
 
@@ -249,6 +254,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
                 }
             }
         }
+        loading.value = false
     }
 
     fun saveList(media: MediaWrapper) = foldersContentMap[media]?.let { if (!it.isEmpty()) prefetchLists[media.location] = it }
diff --git a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
index ec88bc9c9..ec8e6da7c 100644
--- a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
@@ -96,6 +96,7 @@ open class FileBrowserProvider(
     private lateinit var storageObserver : Observer<Boolean>
 
     override suspend fun browseRoot() {
+        loading.postValue(true)
         var storageAccess = false
         val internalmemoryTitle = context.getString(R.string.internal_memory)
         val browserStorage = context.getString(R.string.browser_storages)
@@ -135,13 +136,16 @@ open class FileBrowserProvider(
         // observe devices & favorites
         ExternalMonitor.devices.observeForever(this at FileBrowserProvider)
         if (showFavorites) favorites?.observeForever(favoritesObserver)
+        loading.postValue(false)
     }
 
 
     override fun browse(url: String?) {
         when {
             url == "otg://" || url?.startsWith("content:") == true -> launch {
+                loading.postValue(true)
                 dataset.value = withContext(Dispatchers.IO) { getDocumentFiles(context, Uri.parse(url).path?.substringAfterLast(':') ?: "") as? MutableList<MediaLibraryItem> ?: mutableListOf() }
+                loading.postValue(false)
             }
             else -> super.browse(url)
         }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
index fb030d691..0bc4313ca 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
@@ -22,6 +22,7 @@ package org.videolan.vlc.viewmodels
 
 import android.content.Context
 import androidx.lifecycle.MediatorLiveData
+import androidx.lifecycle.MutableLiveData
 import kotlinx.coroutines.*
 import kotlinx.coroutines.channels.Channel
 import kotlinx.coroutines.channels.actor
@@ -38,6 +39,7 @@ abstract class BaseModel<T : MediaLibraryItem>(context: Context) : SortableModel
     private val filter by lazy(LazyThreadSafetyMode.NONE) { FilterDelegate(dataset) }
 
     val dataset = LiveDataset<T>()
+    open val loading = MutableLiveData<Boolean>().apply { value = false }
 
     val categories by lazy(LazyThreadSafetyMode.NONE) {
         MediatorLiveData<Map<String, List<MediaLibraryItem>>>().apply {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
index 04251c98a..9a28371a5 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
@@ -47,6 +47,8 @@ open class BrowserModel(context: Context, val url: String?, type: Int, showHidde
         else -> FileBrowserProvider(context, dataset, url, showHiddenFiles = showHiddenFiles)
     }
 
+    override val loading = provider.loading
+
     override fun refresh() = provider.refresh()
 
     fun browserRoot() = launch { provider.browseRoot() }



More information about the Android mailing list