summaryrefslogtreecommitdiff
path: root/android/arch/paging/TiledDataSource.java
diff options
context:
space:
mode:
authorJason Monk <jmonk@google.com>2017-10-19 09:30:56 -0400
committerJason Monk <jmonk@google.com>2017-10-19 09:30:56 -0400
commitd439404c9988df6001e4ff8bce31537e2692660e (patch)
treeb1462a7177b8a2791140964761eb49d173cdc878 /android/arch/paging/TiledDataSource.java
parent93b7ee4fce01df52a6607f0b1965cbafdfeaf1a6 (diff)
downloadandroid-28-d439404c9988df6001e4ff8bce31537e2692660e.tar.gz
Import Android SDK Platform P [4402356]
/google/data/ro/projects/android/fetch_artifact \ --bid 4386628 \ --target sdk_phone_armv7-win_sdk \ sdk-repo-linux-sources-4402356.zip AndroidVersion.ApiLevel has been modified to appear as 28 Change-Id: Ie49e24e1f4ae9dc96306111e953d3db1e1495b53
Diffstat (limited to 'android/arch/paging/TiledDataSource.java')
-rw-r--r--android/arch/paging/TiledDataSource.java58
1 files changed, 56 insertions, 2 deletions
diff --git a/android/arch/paging/TiledDataSource.java b/android/arch/paging/TiledDataSource.java
index 36be423d..61dead3a 100644
--- a/android/arch/paging/TiledDataSource.java
+++ b/android/arch/paging/TiledDataSource.java
@@ -19,6 +19,7 @@ package android.arch.paging;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
+import java.util.Collections;
import java.util.List;
/**
@@ -92,7 +93,6 @@ public abstract class TiledDataSource<Type> extends DataSource<Integer, Type> {
* @return Number of items this DataSource can provide. Must be <code>0</code> or greater.
*/
@WorkerThread
- @Override
public abstract int countItems();
@Override
@@ -118,7 +118,61 @@ public abstract class TiledDataSource<Type> extends DataSource<Integer, Type> {
@WorkerThread
public abstract List<Type> loadRange(int startPosition, int count);
- final List<Type> loadRangeWrapper(int startPosition, int count) {
+ /**
+ * blocking, and splits pages
+ */
+ void loadRangeInitial(int startPosition, int count, int pageSize, int itemCount,
+ PageResult.Receiver<Integer, Type> receiver) {
+
+ if (itemCount == 0) {
+ // no data to load, just immediately return empty
+ receiver.onPageResult(new PageResult<>(
+ PageResult.INIT, new Page<Integer, Type>(Collections.<Type>emptyList()),
+ 0, 0, startPosition));
+ return;
+ }
+
+
+ List<Type> list = loadRangeWrapper(startPosition, count);
+
+ count = Math.min(count, itemCount - startPosition);
+
+ if (list == null) {
+ // invalid data, pass to receiver
+ receiver.onPageResult(new PageResult<Integer, Type>(
+ PageResult.INIT, null, 0, 0, startPosition));
+ return;
+ }
+
+ if (list.size() != count) {
+ throw new IllegalStateException("Invalid list, requested size: " + count
+ + ", returned size: " + list.size());
+ }
+
+ // emit the results as multiple pages
+ int pageCount = (count + (pageSize - 1)) / pageSize;
+ for (int i = 0; i < pageCount; i++) {
+ int beginInclusive = i * pageSize;
+ int endExclusive = Math.min(count, (i + 1) * pageSize);
+
+ Page<Integer, Type> page = new Page<>(list.subList(beginInclusive, endExclusive));
+
+ int leadingNulls = startPosition + beginInclusive;
+ int trailingNulls = itemCount - leadingNulls - page.items.size();
+ receiver.onPageResult(new PageResult<>(
+ PageResult.INIT, page, leadingNulls, trailingNulls, 0));
+ }
+ }
+
+ void loadRange(int startPosition, int count, PageResult.Receiver<Integer, Type> receiver) {
+ List<Type> list = loadRangeWrapper(startPosition, count);
+
+ Page<Integer, Type> page = list != null ? new Page<Integer, Type>(list) : null;
+ receiver.postOnPageResult(new PageResult<>(
+ PageResult.TILE, page, startPosition, 0, 0));
+ }
+
+ private List<Type> loadRangeWrapper(int startPosition, int count) {
if (isInvalid()) {
return null;
}