diff options
Diffstat (limited to 'android/arch/paging/PagedList.java')
-rw-r--r-- | android/arch/paging/PagedList.java | 145 |
1 files changed, 22 insertions, 123 deletions
diff --git a/android/arch/paging/PagedList.java b/android/arch/paging/PagedList.java index 1f07bfab..6a31b689 100644 --- a/android/arch/paging/PagedList.java +++ b/android/arch/paging/PagedList.java @@ -20,12 +20,9 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; -import java.lang.ref.WeakReference; import java.util.AbstractList; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; -import java.util.concurrent.atomic.AtomicBoolean; /** * Lazy loading list that pages in content from a {@link DataSource}. @@ -93,28 +90,9 @@ import java.util.concurrent.atomic.AtomicBoolean; * @param <T> The type of the entries in the list. */ public abstract class PagedList<T> extends AbstractList<T> { - final Executor mMainThreadExecutor; - final Executor mBackgroundThreadExecutor; - final Config mConfig; - - @NonNull - final PagedStorage<?, T> mStorage; - - int mLastLoad = 0; - T mLastItem = null; - - private final AtomicBoolean mDetached = new AtomicBoolean(false); - - protected final ArrayList<WeakReference<Callback>> mCallbacks = new ArrayList<>(); - - PagedList(@NonNull PagedStorage<?, T> storage, - @NonNull Executor mainThreadExecutor, - @NonNull Executor backgroundThreadExecutor, - @NonNull Config config) { - mStorage = storage; - mMainThreadExecutor = mainThreadExecutor; - mBackgroundThreadExecutor = backgroundThreadExecutor; - mConfig = config; + // Since we currently rely on implementation details of two implementations, + // prevent external subclassing + PagedList() { } /** @@ -302,13 +280,7 @@ public abstract class PagedList<T> extends AbstractList<T> { */ @Override @Nullable - public T get(int index) { - T item = mStorage.get(index); - if (item != null) { - mLastItem = item; - } - return item; - } + public abstract T get(int index); /** @@ -316,10 +288,7 @@ public abstract class PagedList<T> extends AbstractList<T> { * * @param index Index at which to load. */ - public void loadAround(int index) { - mLastLoad = index + getPositionOffset(); - loadAroundInternal(index); - } + public abstract void loadAround(int index); /** @@ -328,9 +297,7 @@ public abstract class PagedList<T> extends AbstractList<T> { * @return Current total size of the list. */ @Override - public int size() { - return mStorage.size(); - } + public abstract int size(); /** * Returns whether the list is immutable. Immutable lists may not become mutable again, and may @@ -338,25 +305,15 @@ public abstract class PagedList<T> extends AbstractList<T> { * * @return True if the PagedList is immutable. */ - @SuppressWarnings("WeakerAccess") - public boolean isImmutable() { - return isDetached(); - } + public abstract boolean isImmutable(); /** * Returns an immutable snapshot of the PagedList. If this PagedList is already * immutable, it will be returned. * - * @return Immutable snapshot of PagedList data. + * @return Immutable snapshot of PagedList, which may be the PagedList itself. */ - @NonNull - public List<T> snapshot() { - if (isImmutable()) { - return this; - } - - return new SnapshotPagedList<>(this); - } + public abstract List<T> snapshot(); abstract boolean isContiguous(); @@ -371,7 +328,9 @@ public abstract class PagedList<T> extends AbstractList<T> { * @return Key of position most recently passed to {@link #loadAround(int)}. */ @Nullable - public abstract Object getLastKey(); + public Object getLastKey() { + return null; + } /** * True if the PagedList has detached the DataSource it was loading from, and will no longer @@ -379,9 +338,8 @@ public abstract class PagedList<T> extends AbstractList<T> { * * @return True if the data source is detached. */ - @SuppressWarnings("WeakerAccess") public boolean isDetached() { - return mDetached.get(); + return true; } /** @@ -391,9 +349,7 @@ public abstract class PagedList<T> extends AbstractList<T> { * signal to stop loading. The PagedList will continue to present existing data, but will not * initiate new loads. */ - @SuppressWarnings("WeakerAccess") public void detach() { - mDetached.set(true); } /** @@ -405,7 +361,7 @@ public abstract class PagedList<T> extends AbstractList<T> { * If the DataSource is a {@link KeyedDataSource}, and thus doesn't use positions, returns 0. */ public int getPositionOffset() { - return mStorage.getPositionOffset(); + return 0; } /** @@ -429,68 +385,16 @@ public abstract class PagedList<T> extends AbstractList<T> { * @param callback Callback to dispatch to. * @see #removeWeakCallback(Callback) */ - @SuppressWarnings("WeakerAccess") - public void addWeakCallback(@Nullable List<T> previousSnapshot, @NonNull Callback callback) { - if (previousSnapshot != null && previousSnapshot != this) { - PagedList<T> storageSnapshot = (PagedList<T>) previousSnapshot; - //noinspection unchecked - dispatchUpdatesSinceSnapshot(storageSnapshot, callback); - } - - // first, clean up any empty weak refs - for (int i = mCallbacks.size() - 1; i >= 0; i--) { - Callback currentCallback = mCallbacks.get(i).get(); - if (currentCallback == null) { - mCallbacks.remove(i); - } - } + public abstract void addWeakCallback(@Nullable PagedList<T> previousSnapshot, + @NonNull Callback callback); - // then add the new one - mCallbacks.add(new WeakReference<>(callback)); - } /** * Removes a previously added callback. * * @param callback Callback, previously added. - * @see #addWeakCallback(List, Callback) + * @see #addWeakCallback(PagedList, Callback) */ - @SuppressWarnings("WeakerAccess") - public void removeWeakCallback(@NonNull Callback callback) { - for (int i = mCallbacks.size() - 1; i >= 0; i--) { - Callback currentCallback = mCallbacks.get(i).get(); - if (currentCallback == null || currentCallback == callback) { - // found callback, or empty weak ref - mCallbacks.remove(i); - } - } - } - - void notifyInserted(int position, int count) { - if (count != 0) { - for (int i = mCallbacks.size() - 1; i >= 0; i--) { - Callback callback = mCallbacks.get(i).get(); - if (callback != null) { - callback.onInserted(position, count); - } - } - } - } - - void notifyChanged(int position, int count) { - if (count != 0) { - for (int i = mCallbacks.size() - 1; i >= 0; i--) { - Callback callback = mCallbacks.get(i).get(); - if (callback != null) { - callback.onChanged(position, count); - } - } - } - } - - abstract void dispatchUpdatesSinceSnapshot(@NonNull PagedList<T> snapshot, - @NonNull Callback callback); - - abstract void loadAroundInternal(int index); + public abstract void removeWeakCallback(Callback callback); /** * Callback signaling when content is loaded into the list. @@ -641,15 +545,10 @@ public abstract class PagedList<T> extends AbstractList<T> { * Defines how many items to load when first load occurs, if you are using a * {@link KeyedDataSource}. * <p> - * This value is typically larger than page size, so on first load data there's a large - * enough range of content loaded to cover small scrolls. - * <p> - * If used with a {@link TiledDataSource}, this value is rounded to the nearest number - * of pages, with a minimum of two pages, and loaded with a single call to - * {@link TiledDataSource#loadRange(int, int)}. - * <p> - * If used with a {@link KeyedDataSource}, this value will be passed to - * {@link KeyedDataSource#loadInitial(int)}. + * If you are using an {@link TiledDataSource}, this value is currently ignored. + * Otherwise, this value will be passed to + * {@link KeyedDataSource#loadInitial(int)} to load a (typically) larger amount + * of data on first load. * <p> * If not set, defaults to three times page size. * |