[Android] Revert paging offloadind

Geoffrey Métais git at videolan.org
Fri Nov 29 11:03:57 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey at videolan.org> | Fri Nov 29 11:03:55 2019 +0100| [6819f4dcc8d055111afe82726857ed722b816227] | committer: Nicolas Pomepuy

Revert paging offloadind

It was causing glitches

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

 .../src/org/videolan/vlc/gui/PlaylistFragment.kt   |  5 +---
 .../videolan/vlc/gui/audio/AudioBrowserFragment.kt |  3 +--
 .../vlc/gui/browser/MediaBrowserFragment.kt        |  2 ++
 .../videolan/vlc/gui/video/VideoGridFragment.kt    | 24 +++++--------------
 .../providers/medialibrary/MedialibraryProvider.kt | 28 +++++++---------------
 5 files changed, 19 insertions(+), 43 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/PlaylistFragment.kt b/vlc-android/src/org/videolan/vlc/gui/PlaylistFragment.kt
index 3a4a11631..e9a618187 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PlaylistFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/PlaylistFragment.kt
@@ -105,10 +105,7 @@ class PlaylistFragment : BaseAudioBrowser<PlaylistsViewModel>(), SwipeRefreshLay
             playlistAdapter.submitList(it as PagedList<MediaLibraryItem>)
         })
         viewModel.provider.loading.observe(requireActivity(), Observer<Boolean> { loading ->
-            lifecycleScope.launchWhenStarted {
-                binding.swipeLayout.isRefreshing = loading == true
-                (activity as? MainActivity)?.refreshing = loading
-            }
+            setRefreshing(loading)
             if (!loading) binding.empty.visibility = if (empty) View.VISIBLE else View.GONE
         })
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
index be3998940..446f835c2 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
@@ -204,6 +204,7 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>() {
             provider.pagedList.observe(this, Observer { items ->
                 @Suppress("UNCHECKED_CAST")
                 if (items != null) adapters[index].submitList(items as PagedList<MediaLibraryItem>?)
+                updateEmptyView()
                 restorePositions.get(index)?.let {
                     lists[index].scrollToPosition(it)
                     restorePositions.delete(index)
@@ -215,10 +216,8 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>() {
                 if (loading) empty_loading.state = EmptyLoadingState.LOADING
                 else {
                     swipeRefreshLayout.isEnabled = (getCurrentRV().layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() <= 0
-                    updateEmptyView()
                     songs_fast_scroller.setRecyclerView(getCurrentRV(), viewModel.providers[currentTab])
                 }
-                (activity as? MainActivity)?.refreshing = loading
             })
         }
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
index 3a27a01fb..73df5c6bf 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
@@ -50,6 +50,7 @@ import org.videolan.vlc.R
 import org.videolan.vlc.gui.AudioPlayerContainerActivity
 import org.videolan.vlc.gui.ContentActivity
 import org.videolan.vlc.gui.InfoActivity
+import org.videolan.vlc.gui.MainActivity
 import org.videolan.vlc.gui.helpers.SparseBooleanArrayParcelable
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.gui.helpers.UiTools.snackerConfirm
@@ -363,6 +364,7 @@ abstract class MediaBrowserFragment<T : SortableModel> : Fragment(), ActionMode.
         refreshJob = lifecycleScope.launchWhenStarted {
             if (refreshing) delay(300L)
             swipeRefreshLayout.isRefreshing = refreshing
+            (activity as? MainActivity)?.refreshing = refreshing
         }
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
index 96fd953ea..7101b8764 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
@@ -76,7 +76,7 @@ private const val UNSET_REFRESHING = 16
 
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
-class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshLayout.OnRefreshListener, Observer<PagedList<AbstractMediaWrapper>>, CtxActionReceiver {
+class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshLayout.OnRefreshListener, CtxActionReceiver {
 
     private lateinit var videoListAdapter: VideoListAdapter
     private lateinit var multiSelectHelper: MultiSelectHelper<MediaLibraryItem>
@@ -111,17 +111,11 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
 
     private fun setDataObservers() {
         videoListAdapter.dataType = viewModel.groupingType
-        when (viewModel.groupingType) {
-            VideoGroupingType.NONE -> {
-                (viewModel.provider as VideosProvider).pagedList.observe(this, this)
-            }
-            VideoGroupingType.FOLDER, VideoGroupingType.NAME -> {
-                viewModel.provider.pagedList.observe(requireActivity(), Observer {
-                    if (it != null) videoListAdapter.submitList(it as PagedList<MediaLibraryItem>)
-                    restoreMultiSelectHelper()
-                })
-            }
-        }
+        viewModel.provider.pagedList.observe(requireActivity(), Observer {
+            (it as? PagedList<MediaLibraryItem>)?.let { videoListAdapter.submitList(it) }
+            updateEmptyView()
+            restoreMultiSelectHelper()
+        })
 
         viewModel.provider.loading.observe(this, Observer { loading ->
             setRefreshing(loading)
@@ -130,8 +124,6 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
                 menu?.let { UiTools.updateSortTitles(it, viewModel.provider) }
                 restoreMultiSelectHelper()
             }
-            (activity as? MainActivity)?.refreshing = loading
-            updateEmptyView()
         })
         videoListAdapter.showFilename.set(viewModel.groupingType == VideoGroupingType.NONE && viewModel.provider.sort == AbstractMedialibrary.SORT_FILENAME)
     }
@@ -253,10 +245,6 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
         gridItemDecoration = null
     }
 
-    override fun onChanged(list: PagedList<AbstractMediaWrapper>?) {
-        if (list != null) videoListAdapter.submitList(list as PagedList<MediaLibraryItem>)
-    }
-
     override fun getTitle() = when(viewModel.groupingType) {
         VideoGroupingType.NONE -> viewModel.folder?.displayTitle ?: viewModel.group?.displayTitle ?: getString(R.string.videos)
         VideoGroupingType.FOLDER -> getString(R.string.videos_folders_title)
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 b3300123a..711de3934 100644
--- a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
@@ -27,10 +27,10 @@ import androidx.paging.Config
 import androidx.paging.DataSource
 import androidx.paging.PositionalDataSource
 import androidx.paging.toLiveData
+import kotlinx.coroutines.CoroutineStart
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.launch
-import kotlinx.coroutines.withContext
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.tools.retry
@@ -117,28 +117,18 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
 
     inner class MLDataSource : PositionalDataSource<T>() {
 
-        @ExperimentalCoroutinesApi
         override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<T>) {
-            model.viewModelScope.launch(Dispatchers.Unconfined) {
-                retry(1) {
-                    val page = withContext(Dispatchers.IO) { getPage(params.requestedLoadSize, params.requestedStartPosition) }
-                    val count = if (page.size < params.requestedLoadSize) page.size else withContext(Dispatchers.IO) { getTotalCount() }
-                    try {
-                        callback.onResult(page.toList(), params.requestedStartPosition, count)
-                        true
-                    } catch (e: IllegalArgumentException) {
-                        false
-                    }
-                }
-                isRefreshing = false
-            }
+            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)
+            } catch (e: IllegalArgumentException) {}
+            isRefreshing = false
         }
 
         override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) {
-            model.viewModelScope.launch {
-                    val result = withContext(Dispatchers.IO) { getPage(params.loadSize, params.startPosition).toList() }
-                    callback.onResult(result)
-            }
+            val result = getPage(params.loadSize, params.startPosition).toList()
+            callback.onResult(result)
         }
     }
 



More information about the Android mailing list