[Android] TV: Fix main fragment restoration

Geoffrey Métais git at videolan.org
Tue May 14 13:32:27 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue May 14 13:26:39 2019 +0200| [41a92cfb39f9dea3427ae513ff4e82c1a46d2533] | committer: Geoffrey Métais

TV: Fix main fragment restoration

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

 .../org/videolan/vlc/viewmodels/tv/MainTvModel.kt  | 50 +++++++++++-----------
 1 file changed, 25 insertions(+), 25 deletions(-)

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 9d9863919..9223b479c 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/tv/MainTvModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/tv/MainTvModel.kt
@@ -75,9 +75,13 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
 
     private val nowPlayingDelegate = NowPlayingDelegate(this)
 
+    private val updateActor = actor<Unit>(capacity = Channel.CONFLATED) {
+        for (action in channel) updateBrowsers()
+    }
+
     private val favObserver = Observer<List<BrowserFav>> { list ->
         updatedFavoritList = convertFavorites(list)
-        updateActor.offer(Unit)
+        if (!updateActor.isClosedForSend) updateActor.offer(Unit)
     }
 
     private val monitorObserver = Observer<Any> { updateActor.offer(Unit) }
@@ -142,6 +146,25 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
         (audioCategories as MutableLiveData).value = list
     }
 
+    private suspend fun updateBrowsers() {
+        val list = mutableListOf<MediaLibraryItem>()
+        val directories = DirectoryRepository.getInstance(context).getMediaDirectoriesList(context).toMutableList()
+        if (!showInternalStorage && directories.isNotEmpty()) directories.removeAt(0)
+        directories.forEach { if (it.location.scanAllowed()) list.add(it) }
+
+        if (ExternalMonitor.isLan) {
+            list.add(DummyItem(HEADER_NETWORK, context.getString(R.string.network_browsing), null))
+            list.add(DummyItem(HEADER_STREAM, context.getString(R.string.open_mrl), null))
+            list.add(DummyItem(HEADER_SERVER, context.getString(R.string.server_add_title), null))
+            updatedFavoritList.forEach {
+                it.description = it.uri.scheme
+                list.add(it)
+            }
+        }
+        (browsers as MutableLiveData).value = list
+        delay(500L)
+    }
+
     override fun onMedialibraryIdle() { refresh() }
 
     override fun onMedialibraryReady() { refresh() }
@@ -196,7 +219,7 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
     }
 
     companion object {
-        fun Fragment.getMainTvModel() = ViewModelProviders.of(this, Factory(requireActivity().application)).get(MainTvModel::class.java)
+        fun Fragment.getMainTvModel() = ViewModelProviders.of(requireActivity(), Factory(requireActivity().application)).get(MainTvModel::class.java)
     }
 
     class Factory(private val app: Application): ViewModelProvider.NewInstanceFactory() {
@@ -205,27 +228,4 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
             return MainTvModel(app) as T
         }
     }
-
-    private val updateActor = actor<Unit>(capacity = Channel.CONFLATED) {
-        for (action in channel) {
-            val list = mutableListOf<MediaLibraryItem>()
-            val directories = DirectoryRepository.getInstance(context).getMediaDirectoriesList(context).toMutableList()
-            if (!showInternalStorage && directories.isNotEmpty()) directories.removeAt(0)
-            for (directory in directories) {
-                if (directory.location.scanAllowed()) list.add(directory)
-            }
-
-            if (ExternalMonitor.isLan) {
-                list.add(DummyItem(HEADER_NETWORK, context.getString(R.string.network_browsing), null))
-                list.add(DummyItem(HEADER_STREAM, context.getString(R.string.open_mrl), null))
-                list.add(DummyItem(HEADER_SERVER, context.getString(R.string.server_add_title), null))
-                updatedFavoritList.forEach {
-                    it.description = it.uri.scheme
-                    list.add(it)
-                }
-            }
-            (browsers as MutableLiveData).value = list
-            delay(500L)
-        }
-    }
 }



More information about the Android mailing list