Skip to content

Commit 57f43eb

Browse files
committed
Backwards compatibility hack to support ParseQueryAdapter#setPaginationEnabled(false)
1 parent 6086672 commit 57f43eb

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

ParseUI-Widget/src/main/java/com/parse/ParseQueryAdapter.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ private ParseQueryPager<T> getPager() {
285285
protected ParseQuery<T> createQuery(int page) {
286286
// Workaround for backwards compatibility
287287
ParseQuery<T> query = new ParseQuery<>(getQuery());
288-
setPageOnQuery(page, query);
288+
if (paginationEnabled) {
289+
setPageOnQuery(page, query);
290+
}
289291
return query;
290292
}
291293
};
@@ -296,13 +298,17 @@ protected ParseQuery<T> createQuery(int page) {
296298
}
297299
}
298300

301+
private List<T> getObjects() {
302+
return getPager().getObjects();
303+
}
304+
299305
/** {@inheritDoc} **/
300306
@Override
301307
public T getItem(int index) {
302308
if (index == getPaginationCellRow()) {
303309
return null;
304310
}
305-
return getPager().getObjects().get(index);
311+
return getObjects().get(index);
306312
}
307313

308314
/** {@inheritDoc} **/
@@ -412,7 +418,7 @@ public void loadNextPage() {
412418
*/
413419
@Override
414420
public int getCount() {
415-
int count = getPager().getObjects().size();
421+
int count = getObjects().size();
416422

417423
if (shouldShowPaginationCell()) {
418424
count++;
@@ -619,7 +625,7 @@ public void setAutoload(boolean autoload) {
619625
return;
620626
}
621627
this.autoload = autoload;
622-
if (this.autoload && !dataSetObservers.isEmpty() && getPager().getObjects().isEmpty()) {
628+
if (this.autoload && !dataSetObservers.isEmpty() && getObjects().isEmpty()) {
623629
loadObjects();
624630
}
625631
}
@@ -655,7 +661,7 @@ private View getDefaultView(Context context) {
655661
}
656662

657663
private int getPaginationCellRow() {
658-
return getPager().getObjects().size();
664+
return getObjects().size();
659665
}
660666

661667
private boolean shouldShowPaginationCell() {

ParseUI-Widget/src/main/java/com/parse/widget/util/ParseQueryPager.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,16 @@ public Task<List<T>> loadNextPage(CancellationToken ct) {
250250
final int page = getCurrentPage() + 1;
251251

252252
// TODO(grantland): Utilize query.findInBackground(CancellationToken)
253-
Task<List<T>> task = findAsync(createQuery(page), ct).continueWithTask(new Continuation<List<T>, Task<List<T>>>() {
253+
final ParseQuery<T> query = createQuery(page);
254+
Task<List<T>> task = findAsync(query, ct).continueWithTask(new Continuation<List<T>, Task<List<T>>>() {
254255
@Override
255256
public Task<List<T>> then(Task<List<T>> task) throws Exception {
256257
if (task.isCancelled() || task.isFaulted()) {
257258
return task;
258259
}
259260

260261
List<T> results = task.getResult();
261-
onPage(page, results);
262+
onPage(query, page, results);
262263

263264
return task;
264265
}
@@ -296,15 +297,16 @@ public void loadNextPage(final FindCallback<T> callback, final CancellationToken
296297
final int page = getCurrentPage() + 1;
297298

298299
final TaskCompletionSource<List<T>> tcs = new TaskCompletionSource<>();
299-
createQuery(page).findInBackground(new FindCallback<T>() {
300+
final ParseQuery<T> query = createQuery(page);
301+
query.findInBackground(new FindCallback<T>() {
300302

301303
AtomicInteger callbacks = new AtomicInteger();
302304

303305
@Override
304306
public void done(List<T> results, ParseException e) {
305307
boolean isCancelled = ct != null && ct.isCancellationRequested();
306308
if (!isCancelled && e == null) {
307-
onPage(page, results);
309+
onPage(query, page, results);
308310
}
309311

310312
boolean isCacheThenNetwork = false;
@@ -329,16 +331,21 @@ public void done(List<T> results, ParseException e) {
329331
setLoadNextPageTask(tcs.getTask());
330332
}
331333

332-
private void onPage(int page, List<T> results) {
334+
private void onPage(ParseQuery<T> query, int page, List<T> results) {
333335
synchronized (lock) {
334336
int itemCount = results.size();
335337

336338
currentPage = page;
337-
// We detect if there are more pages by setting the limit pageSize + 1 and we remove the extra
338-
// if there are more pages.
339-
hasNextPage = itemCount >= pageSize + 1;
340-
if (itemCount > pageSize) {
341-
results.remove(pageSize);
339+
if (query.getLimit() == pageSize) {
340+
// Backwards compatibility hack to support ParseQueryAdapter#setPaginationEnabled(false)
341+
hasNextPage = false;
342+
} else {
343+
// We detect if there are more pages by setting the limit pageSize + 1 and we remove the extra
344+
// if there are more pages.
345+
hasNextPage = itemCount >= pageSize + 1;
346+
if (itemCount > pageSize) {
347+
results.remove(pageSize);
348+
}
342349
}
343350
int objectsSize = objects.size();
344351
boolean inserted = true;

0 commit comments

Comments
 (0)