[Android] Paged models: Confine headers acces to main htread

Geoffrey Métais git at videolan.org
Fri Feb 15 15:29:26 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Feb 15 15:28:56 2019 +0100| [b838c63a3f56818acf58df8543492303f43e7bd0] | committer: Geoffrey Métais

Paged models: Confine headers acces to main htread

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

 .../videolan/vlc/viewmodels/paged/MLPagedModel.kt  | 23 ++++++++++------------
 1 file changed, 10 insertions(+), 13 deletions(-)

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 89fff12c9..3eef2b154 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
@@ -1,6 +1,7 @@
 package org.videolan.vlc.viewmodels.paged
 
 import android.content.Context
+import androidx.annotation.MainThread
 import androidx.collection.SparseArrayCompat
 import androidx.lifecycle.MutableLiveData
 import androidx.paging.DataSource
@@ -8,8 +9,6 @@ import androidx.paging.LivePagedListBuilder
 import androidx.paging.PagedList
 import androidx.paging.PositionalDataSource
 import kotlinx.coroutines.*
-import kotlinx.coroutines.sync.Mutex
-import kotlinx.coroutines.sync.withLock
 import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
@@ -24,8 +23,7 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
     protected val medialibrary = Medialibrary.getInstance()
     val loading = MutableLiveData<Boolean>().apply { value = false }
 
-    protected val headers = SparseArrayCompat<String>()
-    private val mutex = Mutex()
+    private val headers = SparseArrayCompat<String>()
 
     private val pagingConfig = PagedList.Config.Builder()
             .setPageSize(MEDIALIBRARY_PAGE_SIZE)
@@ -66,7 +64,7 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
     abstract fun getAll() : Array<T>
 
     override fun sort(sort: Int) {
-        launch(Dispatchers.Unconfined) { mutex.withLock { headers.clear() } }
+        headers.clear()
         if (this.sort != sort) {
             this.sort = sort
             desc = false
@@ -97,7 +95,7 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
     }
 
     override fun refresh(): Boolean {
-        launch(Dispatchers.Unconfined) { mutex.withLock { headers.clear() } }
+        headers.clear()
         if (this::restoreJob.isInitialized && restoreJob.isActive) restoreJob.cancel()
         if (pagedList.value?.dataSource?.isInvalid == false) {
             loading.postValue(true)
@@ -114,20 +112,19 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
                 else -> null
             }
             ModelsHelper.getHeader(context, sort, item, previous)?.let {
-                launch(Dispatchers.Unconfined) {
-                    mutex.withLock { headers.put(startposition+position, it) }
-
-                }
+                launch { headers.put(startposition+position, it) }
             }
         }
     }
 
-    suspend fun getSectionforPosition(position: Int): String {
-        mutex.withLock { for (pos in 0 until headers.size()) if (position <= headers.keyAt(pos)) return headers.valueAt(pos) }
+    @MainThread
+    fun getSectionforPosition(position: Int): String {
+        for (pos in 0 until headers.size()) if (position <= headers.keyAt(pos)) return headers.valueAt(pos)
         return ""
     }
 
-    fun getHeaderForPostion(position: Int) = runBlocking { mutex.withLock { headers.get(position) } }
+    @MainThread
+    fun getHeaderForPostion(position: Int) = headers.get(position)
 
     inner class MLDataSource : PositionalDataSource<T>() {
 



More information about the Android mailing list