[Android] Replace handler in MLPagedModel

Geoffrey Métais git at videolan.org
Wed Jan 16 12:03:06 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jan 16 11:45:49 2019 +0100| [a6926c24eae1ed8216330bb4c344214931718a8b] | committer: Geoffrey Métais

Replace handler in MLPagedModel

Handler couldn't be initialized outside of a looper.

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

 .../videolan/vlc/viewmodels/paged/MLPagedModel.kt  | 32 ++++++++++------------
 1 file changed, 15 insertions(+), 17 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 a25526457..3c6778811 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
@@ -1,19 +1,17 @@
 package org.videolan.vlc.viewmodels.paged
 
 import android.content.Context
-import android.os.Handler
 import androidx.lifecycle.MutableLiveData
 import androidx.paging.DataSource
 import androidx.paging.LivePagedListBuilder
 import androidx.paging.PagedList
 import androidx.paging.PositionalDataSource
-import kotlinx.coroutines.Job
-import kotlinx.coroutines.delay
-import kotlinx.coroutines.launch
+import kotlinx.coroutines.*
 import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
 import org.videolan.vlc.util.Settings
+import org.videolan.vlc.util.retry
 import org.videolan.vlc.viewmodels.SortableModel
 
 abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableModel(context), Medialibrary.OnMedialibraryReadyListener, Medialibrary.OnDeviceChangeListener {
@@ -29,8 +27,6 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
     val pagedList = LivePagedListBuilder(MLDatasourceFactory(), pagingConfig)
             .build()
 
-    private val handler by lazy { Handler() }
-
     init {
         medialibrary.apply {
             medialibrary.addOnMedialibraryReadyListener(this at MLPagedModel)
@@ -103,22 +99,24 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
     }
 
     inner class MLDataSource : PositionalDataSource<T>() {
-        private var retry = true
 
+        @ExperimentalCoroutinesApi
         override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<T>) {
-            val page = getPage(params.requestedLoadSize, params.requestedStartPosition)
-            val count = if (page.size < params.requestedLoadSize) page.size else getTotalCount()
-            try {
-                callback.onResult(page.toList(), params.requestedStartPosition, count)
+            launch(Dispatchers.Unconfined) {
+                retry( 1) {
+                    val page = getPage(params.requestedLoadSize, params.requestedStartPosition)
+                    val count = if (page.size < params.requestedLoadSize) page.size else getTotalCount()
+                    try {
+                        callback.onResult(page.toList(), params.requestedStartPosition, count)
+                        true
+                    } catch (e: IllegalArgumentException) {
+                        false
+                    }
+                }
                 loading.postValue(false)
-                retry = true
-            } catch (e: IllegalArgumentException) {
-                if (retry) {
-                    retry = false
-                    handler.postDelayed({ loadInitial(params, callback) }, 500L)
-                } else retry = true
             }
         }
+
         override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) {
             callback.onResult(getPage(params.loadSize, params.startPosition).toList())
         }



More information about the Android mailing list