[Android] Improve LiveDataset

Geoffrey Métais git at videolan.org
Fri Feb 21 14:34:11 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb 18 15:41:05 2020 +0100| [edf66da253531d38e13979856d33b7953121586f] | committer: Geoffrey Métais

Improve LiveDataset

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

 .../src/main/java/org/videolan/tools/livedata/LiveDataset.kt | 12 +++++++++++-
 .../vlc-android/src/org/videolan/vlc/gui/ContentActivity.kt  |  2 +-
 .../src/org/videolan/vlc/providers/FileBrowserProvider.kt    |  2 +-
 .../src/org/videolan/vlc/viewmodels/HistoryModel.kt          | 11 ++---------
 .../src/org/videolan/vlc/viewmodels/StreamsModel.kt          |  2 +-
 5 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/application/tools/src/main/java/org/videolan/tools/livedata/LiveDataset.kt b/application/tools/src/main/java/org/videolan/tools/livedata/LiveDataset.kt
index 799892484..ca0789edd 100644
--- a/application/tools/src/main/java/org/videolan/tools/livedata/LiveDataset.kt
+++ b/application/tools/src/main/java/org/videolan/tools/livedata/LiveDataset.kt
@@ -40,12 +40,14 @@ class LiveDataset<T> : MutableLiveData<MutableList<T>>() {
         super.setValue(value)
     }
 
-    override fun getValue() = super.getValue() ?: emptyList
+    override fun getValue() = internalList
 
     fun get(position: Int) = internalList[position]
 
     fun getList() = internalList.toList()
 
+    val size = internalList.size
+
     fun clear() {
         value = internalList.apply { clear() }
     }
@@ -69,4 +71,12 @@ class LiveDataset<T> : MutableLiveData<MutableList<T>>() {
     fun remove(position: Int) {
         value = internalList.apply { removeAt(position) }
     }
+
+    fun move(from: Int, to: Int) {
+        value = internalList.apply { move(from, to) }
+    }
+
+    fun move (item: T, position: Int) {
+        value = internalList.apply { move(item, position) }
+    }
 }
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/ContentActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/ContentActivity.kt
index f78926843..eba85c9c4 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/ContentActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/ContentActivity.kt
@@ -117,7 +117,7 @@ open class ContentActivity : AudioPlayerContainerActivity(), SearchView.OnQueryT
                 return true
             }
             R.id.ml_menu_renderers -> {
-                if (!PlaybackService.hasRenderer() && RendererDelegate.renderers.value.size == 1) {
+                if (!PlaybackService.hasRenderer() && RendererDelegate.renderers.size == 1) {
                     val renderer = RendererDelegate.renderers.value[0]
                     PlaybackService.renderer.value = renderer
                     val v = findViewById<View>(R.id.audio_player_container)
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
index 29637477f..0d3daaf8c 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
@@ -128,7 +128,7 @@ open class FileBrowserProvider(
             StoragePermissionsDelegate.storageAccessGranted.observeForever(storageObserver)
         }
         if (!storageAccess) return // For first launch, storage access may not already be granted
-        if (AndroidUtil.isLolliPopOrLater && !ExternalMonitor.devices.value.isEmpty()) {
+        if (AndroidUtil.isLolliPopOrLater && !ExternalMonitor.devices.isEmpty()) {
             val otg = MLServiceLocator.getAbstractMediaWrapper(Uri.parse("otg://")).apply {
                 title = context.getString(R.string.otg_device_title)
                 type = MediaWrapper.TYPE_DIR
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/HistoryModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/HistoryModel.kt
index c3e6d6f8b..364954300 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/HistoryModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/HistoryModel.kt
@@ -35,16 +35,9 @@ class HistoryModel(context: Context, coroutineContextProvider: CoroutineContextP
         dataset.value = withContext(coroutineContextProvider.Default) { medialibrary.lastMediaPlayed().toMutableList() }
     }
 
-    fun moveUp(media: MediaWrapper) {
-        dataset.value = dataset.value.apply {
-            remove(media)
-            add(0, media)
-        }
-    }
+    fun moveUp(media: MediaWrapper) = dataset.move(media, 0)
 
-    fun clearHistory() {
-        dataset.value = mutableListOf()
-    }
+    fun clearHistory() = dataset.clear()
 
     class Factory(private val context: Context) : ViewModelProvider.Factory {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt
index b26dd2327..b752bdf2a 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/StreamsModel.kt
@@ -45,7 +45,7 @@ class StreamsModel(context: Context, coroutineContextProvider: CoroutineContextP
     }
 
     fun rename(position: Int, name: String) {
-        val media = dataset.value[position]
+        val media = dataset.get(position)
         viewModelScope.launch(Dispatchers.IO) { media.rename(name) }
         refresh()
     }



More information about the Android mailing list