[Android] Lazy load comparator in sortable adapters

Geoffrey Métais git at videolan.org
Mon Dec 11 15:38:03 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Oct 10 19:22:29 2017 +0200| [4f4d12c7cc992985cb0587824051e63dcade8163] | committer: Geoffrey Métais

Lazy load comparator in sortable adapters

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

 .../src/org/videolan/vlc/SortableAdapter.java      | 34 ++++++++++++++--------
 .../vlc/gui/audio/AudioAlbumsSongsFragment.java    |  2 +-
 .../vlc/gui/audio/AudioBrowserAdapter.java         | 10 +++----
 .../vlc/gui/browser/BaseBrowserAdapter.java        |  2 +-
 4 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/SortableAdapter.java b/vlc-android/src/org/videolan/vlc/SortableAdapter.java
index 8a7959213..535037765 100644
--- a/vlc-android/src/org/videolan/vlc/SortableAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/SortableAdapter.java
@@ -13,24 +13,30 @@ import java.util.Collections;
 
 public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends RecyclerView.ViewHolder> extends BaseQueuedAdapter<T, VH> {
     private static final String TAG = "VLC/SortableAdapter";
-    public static final MediaLibraryItemComparator sMediaComparator = new MediaLibraryItemComparator(SortableAdapter.class);
-    private int mCurrentSort = -1, mCurrentDirection = 1;
+
+    public static MediaLibraryItemComparator getComparator() {
+        return Holder.mediaComparator;
+    }
+
+    private int mCurrentSort = MediaLibraryItemComparator.SORT_DEFAULT, mCurrentDirection = getDefaultDirection();
 
     public int sortDirection(int sortby) {
-        return sMediaComparator.sortDirection(sortby);
+        return mCurrentSort == MediaLibraryItemComparator.SORT_DEFAULT ? mCurrentDirection : getComparator().sortDirection(sortby);
     }
 
     public int getSortDirection() {
-        return sMediaComparator.sortDirection;
+        return getComparator().sortDirection;
     }
 
     public int getSortBy() {
-        return sMediaComparator.sortBy;
+        return getComparator().sortBy;
     }
 
     public void sortBy(int sortby, int direction) {
-        sMediaComparator.sortBy(sortby, direction);
+        getComparator().sortBy(sortby, direction);
         update(new ArrayList<>(peekLast()));
+        mCurrentDirection = getSortDirection();
+        mCurrentSort = getSortBy();
     }
 
     public void updateIfSortChanged() {
@@ -39,17 +45,17 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
     }
 
     private boolean hasSortChanged() {
-        return mCurrentSort != getSortBy() || mCurrentDirection != getSortDirection();
+        return mCurrentSort != MediaLibraryItemComparator.SORT_DEFAULT
+                && (mCurrentSort != getSortBy() || mCurrentDirection != getSortDirection());
     }
 
     protected boolean needsSorting() {
-        return sMediaComparator.sortBy != MediaLibraryItemComparator.SORT_DEFAULT && isSortAllowed(sMediaComparator.sortBy);
+        return mCurrentSort != MediaLibraryItemComparator.SORT_DEFAULT
+                && getComparator().sortBy != MediaLibraryItemComparator.SORT_DEFAULT && isSortAllowed(getComparator().sortBy);
     }
 
     @Override
     protected void onUpdateFinished() {
-        mCurrentDirection = getSortDirection();
-        mCurrentSort = getSortBy();
     }
 
     public int getDefaultSort() {
@@ -70,7 +76,7 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
 
     protected ArrayList<T> prepareList(ArrayList<T> list) {
         if (needsSorting())
-            Collections.sort(list, sMediaComparator);
+            Collections.sort(list, getComparator());
         return list;
     }
 
@@ -80,7 +86,7 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
                 @Override
                 public void run() {
                     if (getSortBy() == MediaLibraryItemComparator.SORT_DEFAULT)
-                        sMediaComparator.sortBy(getDefaultSort(), 1);
+                        getComparator().sortBy(getDefaultSort(), 1);
                     final ArrayList<T> list = new ArrayList<>(peekLast());
                     VLCApplication.runBackground(new Runnable() {
                         @Override
@@ -98,4 +104,8 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
             });
         }
     }
+
+    private static class Holder {
+        private static final MediaLibraryItemComparator mediaComparator = new MediaLibraryItemComparator(SortableAdapter.class);
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
index 0460d4cca..93fff03bd 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
@@ -91,7 +91,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         if (savedInstanceState == null)
-            AudioBrowserAdapter.sMediaComparator.setSortDefault();
+            AudioBrowserAdapter.getComparator().setSortDefault();
 
         mMediaLibrary = VLCApplication.getMLInstance();
         mItem = (MediaLibraryItem) (savedInstanceState != null ?
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
index 462a0f96d..804d4c351 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
@@ -351,17 +351,17 @@ public class AudioBrowserAdapter extends SortableAdapter<MediaLibraryItem, Audio
 
     protected ArrayList<MediaLibraryItem> prepareList(ArrayList<MediaLibraryItem> items) {
         if (!isSortAllowed(getSortBy()))
-            sMediaComparator.setSortDefault();
+            getComparator().setSortDefault();
         if (mMakeSections) {
-            if (sMediaComparator.sortBy == MediaLibraryItemComparator.SORT_DEFAULT) {
+            if (getComparator().sortBy == MediaLibraryItemComparator.SORT_DEFAULT) {
                 return generateSections(items, getDefaultSort());
             } else {
                 ArrayList<MediaLibraryItem> newList = removeSections(items);
-                Collections.sort(newList, sMediaComparator);
-                return generateSections(newList, sMediaComparator.sortBy);
+                Collections.sort(newList, getComparator());
+                return generateSections(newList, getComparator().sortBy);
             }
         } else {
-            Collections.sort(items, sMediaComparator);
+            Collections.sort(items, getComparator());
             return items;
         }
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
index 91260a1e3..ca4060c29 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -370,7 +370,7 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
     @Override
     protected ArrayList<MediaLibraryItem> prepareList(ArrayList<MediaLibraryItem> list) {
         if (fragment.isSortEnabled() && needsSorting())
-            Collections.sort(list, sMediaComparator);
+            Collections.sort(list, getComparator());
         mMediaCount = 0;
         for (MediaLibraryItem item : list) {
             if (item.getItemType() == MediaLibraryItem.TYPE_MEDIA



More information about the Android mailing list