[Android] LiveData to publish headers index update

Geoffrey Métais git at videolan.org
Mon Feb 18 15:53:59 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Feb 18 15:21:35 2019 +0100| [c1842f84d0ff8471a2ab93ef1097d7a59b6ba750] | committer: Geoffrey Métais

LiveData to publish headers index update

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

 vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt | 15 ++++++++++++---
 .../src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt | 11 +++++++++--
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt b/vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt
index a4279ae82..0ef28658e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/view/FastScroller.kt
@@ -40,6 +40,7 @@ import android.widget.ImageView
 import android.widget.LinearLayout
 import android.widget.TextView
 import androidx.coordinatorlayout.widget.CoordinatorLayout
+import androidx.lifecycle.Observer
 import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import com.google.android.material.appbar.AppBarLayout
@@ -51,6 +52,7 @@ import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.R
 import org.videolan.vlc.util.WeakHandler
+import org.videolan.vlc.viewmodels.paged.HeadersIndex
 import org.videolan.vlc.viewmodels.paged.MLPagedModel
 import java.util.concurrent.atomic.AtomicBoolean
 
@@ -67,7 +69,8 @@ private const val ITEM_THRESHOLD = 25
 
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
-class FastScroller : LinearLayout, CoroutineScope {
+class FastScroller : LinearLayout, CoroutineScope, Observer<HeadersIndex> {
+
     override val coroutineContext = Dispatchers.Main.immediate + SupervisorJob()
     private var currentHeight: Int = 0
     private var itemCount: Int = 0
@@ -230,7 +233,9 @@ class FastScroller : LinearLayout, CoroutineScope {
         this.recyclerView.removeOnScrollListener(scrollListener)
         visibility = View.INVISIBLE
         itemCount = recyclerView.adapter!!.itemCount
+        if (this::model.isInitialized) this.model.liveHeaders.removeObserver(this)
         this.model = model
+        model.liveHeaders.observeForever(this)
         recyclerView.addOnScrollListener(scrollListener)
         showBubble = (recyclerView.adapter as SeparatedAdapter).hasSections()
     }
@@ -341,7 +346,7 @@ class FastScroller : LinearLayout, CoroutineScope {
     }
 
     private val actor = actor<Unit>(capacity = Channel.CONFLATED) {
-        for (evt in channel) {
+        for (evt in channel) if (fastScrolling) {
             sb.setLength(0)
 
             //ItemDecoration has to be taken into account so we add 1 for the sticky header
@@ -366,6 +371,10 @@ class FastScroller : LinearLayout, CoroutineScope {
         val proportion = if (recyclerviewTotalHeight == 0) 0f else verticalScrollOffset / recyclerviewTotalHeight.toFloat()
         setPosition(currentHeight * proportion)
         if (visibility == View.INVISIBLE) handler.sendEmptyMessage(SHOW_SCROLLER)
-        if (fastScrolling) actor.offer(Unit)
+        actor.offer(Unit)
+    }
+
+    override fun onChanged(t: HeadersIndex?) {
+        actor.offer(Unit)
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
index af3ac0070..3c779b44a 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
@@ -3,6 +3,7 @@ package org.videolan.vlc.viewmodels.paged
 import android.content.Context
 import androidx.annotation.MainThread
 import androidx.collection.SparseArrayCompat
+import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
 import androidx.paging.DataSource
 import androidx.paging.LivePagedListBuilder
@@ -17,13 +18,16 @@ import org.videolan.vlc.util.Settings
 import org.videolan.vlc.util.retry
 import org.videolan.vlc.viewmodels.SortableModel
 
+typealias HeadersIndex = SparseArrayCompat<String>
+
 @Suppress("LeakingThis")
 @ExperimentalCoroutinesApi
 abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableModel(context), Medialibrary.OnMedialibraryReadyListener, Medialibrary.OnDeviceChangeListener {
     protected val medialibrary = Medialibrary.getInstance()
     val loading = MutableLiveData<Boolean>().apply { value = false }
 
-    private val headers = SparseArrayCompat<String>()
+    private val headers = HeadersIndex()
+    val liveHeaders : LiveData<HeadersIndex> = MutableLiveData<HeadersIndex>()
 
     private val pagingConfig = PagedList.Config.Builder()
             .setPageSize(MEDIALIBRARY_PAGE_SIZE)
@@ -112,7 +116,10 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
                 else -> null
             }
             ModelsHelper.getHeader(context, sort, item, previous)?.let {
-                launch { headers.put(startposition + position, it) }
+                launch {
+                    headers.put(startposition + position, it)
+                    (liveHeaders as MutableLiveData<HeadersIndex>).value = headers
+                }
             }
         }
     }



More information about the Android mailing list