[Android] Fix crash during headers update

Geoffrey Métais git at videolan.org
Thu Jul 18 16:06:58 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jul 18 15:17:56 2019 +0200| [fe9ceedd1ff72dff47454a5b58bf3407175c363b] | committer: Geoffrey Métais

Fix crash during headers update

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

 vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt      | 6 +++---
 vlc-android/src/org/videolan/vlc/providers/HeaderProvider.kt       | 6 +++++-
 .../videolan/vlc/providers/medialibrary/MedialibraryProvider.kt    | 7 +++----
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index ba257b7db..b88bde5bb 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -127,7 +127,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
     }
 
     fun computeHeaders(value: MutableList<MediaLibraryItem>) {
-        headers.clear()
+        privateHeaders.clear()
         for ((position, item) in value.withIndex()) {
             val previous = when {
                 position > 0 -> value[position - 1]
@@ -135,11 +135,11 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
             }
             ModelsHelper.getHeader(context, AbstractMedialibrary.SORT_ALPHA, item, previous)?.let {
                 launch {
-                    headers.put(position, it)
-                    (liveHeaders as MutableLiveData<HeadersIndex>).value = headers
+                    privateHeaders.put(position, it)
                 }
             }
         }
+        (liveHeaders as MutableLiveData).postValue(privateHeaders.clone())
     }
 
     internal open fun parseSubDirectories() {
diff --git a/vlc-android/src/org/videolan/vlc/providers/HeaderProvider.kt b/vlc-android/src/org/videolan/vlc/providers/HeaderProvider.kt
index 14ea7adb3..40b640893 100644
--- a/vlc-android/src/org/videolan/vlc/providers/HeaderProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/HeaderProvider.kt
@@ -8,9 +8,13 @@ import androidx.lifecycle.MutableLiveData
 typealias HeadersIndex = SparseArrayCompat<String>
 
 open class HeaderProvider {
-    val headers = HeadersIndex()
     val liveHeaders: LiveData<HeadersIndex> = MutableLiveData()
+    protected val privateHeaders = HeadersIndex()
+    var headers = HeadersIndex()
 
+    init {
+        liveHeaders.observeForever { headers = it }
+    }
 
     @MainThread
     fun getSectionforPosition(position: Int): String {
diff --git a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
index f0b0ff375..82f152cd1 100644
--- a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
@@ -32,7 +32,6 @@ import kotlinx.coroutines.launch
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.providers.HeaderProvider
-import org.videolan.vlc.providers.HeadersIndex
 import org.videolan.vlc.util.*
 import org.videolan.vlc.viewmodels.SortableModel
 import kotlin.properties.Delegates
@@ -82,7 +81,7 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
 
     fun refresh(): Boolean {
         if (isRefreshing || !medialibrary.isStarted || !this::dataSource.isInitialized) return false
-        headers.clear()
+        privateHeaders.clear()
         if (!dataSource.isInvalid) {
             isRefreshing = true
             dataSource.invalidate()
@@ -100,10 +99,10 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
                 else -> null
             }
             ModelsHelper.getHeader(context, sort, item, previous)?.let {
-                headers.put(startposition + position, it)
-                (liveHeaders as MutableLiveData<HeadersIndex>).postValue(headers)
+                privateHeaders.put(startposition + position, it)
             }
         }
+        (liveHeaders as MutableLiveData).postValue(privateHeaders.clone())
     }
 
     inner class MLDataSource : PositionalDataSource<T>() {



More information about the Android mailing list