[Android] Revert "Lazy load comparator in sortable adapters"

Geoffrey Métais git at videolan.org
Fri Dec 15 16:03:28 CET 2017


vlc-android | branch: chromecast | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Dec 15 09:40:39 2017 +0100| [980f10c4576893ab0036968a1ba9c890bda1ee46] | committer: Geoffrey Métais

Revert "Lazy load comparator in sortable adapters"

This reverts commit 4f4d12c7cc992985cb0587824051e63dcade8163.

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

 .../src/org/videolan/vlc/SortableAdapter.java      | 35 ++++++++--------------
 .../vlc/gui/audio/AudioAlbumsSongsFragment.java    |  2 +-
 .../vlc/gui/audio/AudioBrowserAdapter.java         | 14 ++++-----
 .../vlc/gui/browser/BaseBrowserAdapter.java        |  2 +-
 4 files changed, 21 insertions(+), 32 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/SortableAdapter.java b/vlc-android/src/org/videolan/vlc/SortableAdapter.java
index 6d6422961..e0bee5fae 100644
--- a/vlc-android/src/org/videolan/vlc/SortableAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/SortableAdapter.java
@@ -16,30 +16,24 @@ import java.util.List;
 
 public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends RecyclerView.ViewHolder> extends DiffUtilAdapter<T, VH> {
     private static final String TAG = "VLC/SortableAdapter";
-
-    public static MediaLibraryItemComparator getComparator() {
-        return Holder.mediaComparator;
-    }
-
-    private int mCurrentSort = MediaLibraryItemComparator.SORT_DEFAULT, mCurrentDirection = getDefaultDirection();
+    public static final MediaLibraryItemComparator sMediaComparator = new MediaLibraryItemComparator(SortableAdapter.class);
+    private int mCurrentSort = -1, mCurrentDirection = 1;
 
     public int sortDirection(int sortby) {
-        return mCurrentSort == MediaLibraryItemComparator.SORT_DEFAULT ? mCurrentDirection : getComparator().sortDirection(sortby);
+        return sMediaComparator.sortDirection(sortby);
     }
 
     public int getSortDirection() {
-        return getComparator().sortDirection;
+        return sMediaComparator.sortDirection;
     }
 
     public int getSortBy() {
-        return getComparator().sortBy;
+        return sMediaComparator.sortBy;
     }
 
     public void sortBy(int sortby, int direction) {
-        getComparator().sortBy(sortby, direction);
+        sMediaComparator.sortBy(sortby, direction);
         update(new ArrayList<>(peekLast()));
-        mCurrentDirection = getSortDirection();
-        mCurrentSort = getSortBy();
     }
 
     public void updateIfSortChanged() {
@@ -48,17 +42,17 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
     }
 
     private boolean hasSortChanged() {
-        return mCurrentSort != MediaLibraryItemComparator.SORT_DEFAULT
-                && (mCurrentSort != getSortBy() || mCurrentDirection != getSortDirection());
+        return mCurrentSort != getSortBy() || mCurrentDirection != getSortDirection();
     }
 
     protected boolean needsSorting() {
-        return mCurrentSort != MediaLibraryItemComparator.SORT_DEFAULT
-                && getComparator().sortBy != MediaLibraryItemComparator.SORT_DEFAULT && isSortAllowed(getComparator().sortBy);
+        return sMediaComparator.sortBy != MediaLibraryItemComparator.SORT_DEFAULT && isSortAllowed(sMediaComparator.sortBy);
     }
 
     @Override
     protected void onUpdateFinished() {
+        mCurrentDirection = getSortDirection();
+        mCurrentSort = getSortBy();
     }
 
     public int getDefaultSort() {
@@ -82,8 +76,7 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
     @NotNull
     @Override
     protected List<T> prepareList(@NotNull List<? extends T> list) {
-        if (needsSorting())
-            Collections.sort(list, getComparator());
+        if (needsSorting()) Collections.sort(list, sMediaComparator);
         return (List<T>) list;
     }
 
@@ -93,7 +86,7 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
                 @Override
                 public void run() {
                     if (getSortBy() == MediaLibraryItemComparator.SORT_DEFAULT)
-                        getComparator().sortBy(getDefaultSort(), 1);
+                        sMediaComparator.sortBy(getDefaultSort(), 1);
                     final ArrayList<T> list = new ArrayList<>(peekLast());
                     VLCApplication.runBackground(new Runnable() {
                         @Override
@@ -106,8 +99,4 @@ 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 a045151b7..b0b2eb07a 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.getComparator().setSortDefault();
+            AudioBrowserAdapter.sMediaComparator.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 144cdb075..c0df3a9a1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
@@ -356,24 +356,24 @@ public class AudioBrowserAdapter extends SortableAdapter<MediaLibraryItem, Audio
     @Override
     protected List<MediaLibraryItem> prepareList(List<? extends MediaLibraryItem> items) {
         if (!isSortAllowed(getSortBy()))
-            getComparator().setSortDefault();
+            sMediaComparator.setSortDefault();
         if (mMakeSections) {
-            if (getComparator().sortBy == MediaLibraryItemComparator.SORT_DEFAULT) {
+            if (sMediaComparator.sortBy == MediaLibraryItemComparator.SORT_DEFAULT) {
                 return generateSections(items, getDefaultSort());
             } else {
-                List<MediaLibraryItem> newList = removeSections((List<MediaLibraryItem>) items);
-                Collections.sort(newList, getComparator());
-                return generateSections(newList, getComparator().sortBy);
+                final List<MediaLibraryItem> newList = removeSections((List<MediaLibraryItem>) items);
+                Collections.sort(newList, sMediaComparator);
+                return generateSections(newList, sMediaComparator.sortBy);
             }
         } else {
-            Collections.sort(items, getComparator());
+            Collections.sort(items, sMediaComparator);
             return (List<MediaLibraryItem>) items;
         }
     }
 
     @MainThread
     public List<MediaLibraryItem> getSelection() {
-        List<MediaLibraryItem> selection = new LinkedList<>();
+        final List<MediaLibraryItem> selection = new LinkedList<>();
         for (MediaLibraryItem item : getDataset())
             if (item.hasStateFlags(FLAG_SELECTED))
                 selection.add(item);
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 b04bcfdb0..7fce44417 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -378,7 +378,7 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
     @Override
     protected List<MediaLibraryItem> prepareList(List<? extends MediaLibraryItem> list) {
         if (fragment.isSortEnabled() && needsSorting())
-            Collections.sort(list, getComparator());
+            Collections.sort(list, sMediaComparator);
         mMediaCount = 0;
         for (MediaLibraryItem item : list) {
             if (item.getItemType() == MediaLibraryItem.TYPE_MEDIA



More information about the Android mailing list