[Android] Retry strategy for paged lists loading
Geoffrey Métais
git at videolan.org
Wed Jan 9 15:48:00 CET 2019
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jan 9 15:34:54 2019 +0100| [0fbe8aa8dee1c916b24353d805717ea3190e805a] | committer: Geoffrey Métais
Retry strategy for paged lists loading
> https://code.videolan.org/videolan/vlc-android/commit/0fbe8aa8dee1c916b24353d805717ea3190e805a
---
.../org/videolan/vlc/viewmodels/paged/MLPagedModel.kt | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 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 18f95b52a..5f7d20ec9 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 android.os.Handler
import androidx.lifecycle.MutableLiveData
import androidx.paging.DataSource
import androidx.paging.LivePagedListBuilder
@@ -22,11 +23,13 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
.setPageSize(100)
.setPrefetchDistance(100)
.setEnablePlaceholders(true)
- .build()!!
+ .build()
val pagedList = LivePagedListBuilder(MLDatasourceFactory(), pagingConfig)
.build()
+ private val handler by lazy { Handler() }
+
init {
medialibrary.apply {
medialibrary.addOnMedialibraryReadyListener(this at MLPagedModel)
@@ -99,12 +102,21 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
}
inner class MLDataSource : PositionalDataSource<T>() {
+ private var retry = true
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()
- callback.onResult(page.toList(), params.requestedStartPosition, count)
- loading.postValue(false)
+ try {
+ callback.onResult(page.toList(), params.requestedStartPosition, count)
+ 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