[Android] Improve paged refresh

Geoffrey Métais git at videolan.org
Fri Jun 21 11:11:48 CEST 2019


vlc-android | branch: 3.1.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jun 21 11:10:41 2019 +0200| [8ef207b26f9e890ea6aed9b20cbdbb6da80b0e79] | committer: Geoffrey Métais

Improve paged refresh

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

 .../src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt        | 9 +++++----
 .../src/org/videolan/vlc/viewmodels/paged/PagedAlbumsModel.kt    | 1 -
 .../src/org/videolan/vlc/viewmodels/paged/PagedArtistsModel.kt   | 2 +-
 .../src/org/videolan/vlc/viewmodels/paged/PagedFoldersModel.kt   | 4 ----
 .../src/org/videolan/vlc/viewmodels/paged/PagedGenresModel.kt    | 1 -
 .../src/org/videolan/vlc/viewmodels/paged/PagedPlaylistsModel.kt | 1 -
 .../src/org/videolan/vlc/viewmodels/paged/PagedTracksModel.kt    | 1 -
 7 files changed, 6 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 d82f87bda..758e6f56e 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
@@ -37,6 +37,7 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
             maxSize = MEDIALIBRARY_PAGE_SIZE*3
     )
 
+    private lateinit var dataSource : DataSource<Int, T>
     val pagedList = MLDatasourceFactory().toLiveData(pagingConfig)
 
     init {
@@ -101,10 +102,10 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
 
     override fun refresh(): Boolean {
         headers.clear()
-        if (this::restoreJob.isInitialized && restoreJob.isActive) restoreJob.cancel()
-        if (pagedList.value?.dataSource?.isInvalid == false) {
+        if (::restoreJob.isInitialized && restoreJob.isActive) restoreJob.cancel()
+        if (::dataSource.isInitialized && !dataSource.isInvalid) {
             loading.postValue(true)
-            pagedList.value?.dataSource?.invalidate()
+            dataSource.invalidate()
         }
         return true
     }
@@ -171,6 +172,6 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
     }
 
     inner class MLDatasourceFactory : DataSource.Factory<Int, T>() {
-        override fun create() = MLDataSource()
+        override fun create() = MLDataSource().also { dataSource = it }
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedAlbumsModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedAlbumsModel.kt
index 5b33949ae..c43281751 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedAlbumsModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedAlbumsModel.kt
@@ -23,7 +23,6 @@ class PagedAlbumsModel(context: Context, val parent: MediaLibraryItem? = null) :
         desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
         if (sort == Medialibrary.SORT_ALPHA && parent is Artist) sort = Medialibrary.SORT_RELEASEDATE
         medialibrary.addAlbumsCb(this)
-        if (medialibrary.isStarted) refresh()
     }
 
     override fun onAlbumsAdded() {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedArtistsModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedArtistsModel.kt
index ae520e50d..f47589de8 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedArtistsModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedArtistsModel.kt
@@ -12,8 +12,8 @@ class PagedArtistsModel(context: Context, private var showAll: Boolean = false):
 
     init {
         medialibrary.addArtistsCb(this)
-        if (medialibrary.isStarted) refresh()
     }
+
     override fun onArtistsAdded() {
         refresh()
     }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedFoldersModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedFoldersModel.kt
index 9e7d74132..b72c5a3d2 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedFoldersModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedFoldersModel.kt
@@ -12,10 +12,6 @@ import org.videolan.vlc.media.getAll
 
 class PagedFoldersModel(context: Context, val type: Int) : MLPagedModel<Folder>(context) {
 
-    init {
-        if (medialibrary.isStarted) refresh()
-    }
-
     override fun getAll() : Array<Folder> = medialibrary.getFolders(type, sort, desc, getTotalCount(), 0)
 
     override fun getTotalCount() = medialibrary.getFoldersCount(type)
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedGenresModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedGenresModel.kt
index 4fbfb3c7b..abc824e34 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedGenresModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedGenresModel.kt
@@ -14,7 +14,6 @@ class PagedGenresModel(context: Context): MLPagedModel<Genre>(context), Medialib
         sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
         desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
         medialibrary.addGenreCb(this)
-        if (medialibrary.isStarted) refresh()
     }
 
     override fun onCleared() {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedPlaylistsModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedPlaylistsModel.kt
index f9abe8d1d..09f682003 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedPlaylistsModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedPlaylistsModel.kt
@@ -10,7 +10,6 @@ class PagedPlaylistsModel(context: Context): MLPagedModel<Playlist>(context), Me
 
     init {
         medialibrary.addPlaylistCb(this)
-        if (medialibrary.isStarted) refresh()
     }
 
     override fun onCleared() {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedTracksModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedTracksModel.kt
index c7da221d1..d46969c76 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedTracksModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedTracksModel.kt
@@ -28,7 +28,6 @@ class PagedTracksModel(context: Context, val parent: MediaLibraryItem? = null):
             is Album -> Medialibrary.SORT_DEFAULT
             else -> Medialibrary.SORT_ALPHA
         }
-        if (medialibrary.isStarted) refresh()
         when (parent) {
             is Artist -> medialibrary.addArtistsCb(this at PagedTracksModel)
             is Album -> medialibrary.addAlbumsCb(this at PagedTracksModel)



More information about the Android mailing list