Skip to content

Commit 51c0d95

Browse files
committed
Basic JavaDoc
Change-Id: I75cd769dd3f9cd4c6d6cc1de13d411646827430c
1 parent 2a5f554 commit 51c0d95

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

firestore/src/main/java/com/firebase/ui/firestore/FirestoreInfiniteScrollListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import android.support.v7.widget.RecyclerView;
66

77
/**
8-
* TODO(samstern): Document
8+
* RecyclerView scroll listener that triggers loading/unloading in a
9+
* {@link FirestorePagingAdapter} so that the adapter can appear to be infinite sroll.
910
*/
1011
public class FirestoreInfiniteScrollListener extends RecyclerView.OnScrollListener {
1112

@@ -53,7 +54,7 @@ public void run() {
5354
@Override
5455
public void run() {
5556
// Load one page up
56-
mAdapter.loadPrevPage();
57+
mAdapter.loadPageUp();
5758
}
5859
});
5960
}

firestore/src/main/java/com/firebase/ui/firestore/FirestorePagingAdapter.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.support.annotation.NonNull;
44
import android.support.annotation.Nullable;
5+
import android.support.annotation.RestrictTo;
56
import android.support.v7.widget.RecyclerView;
67
import android.util.Log;
78
import android.view.ViewGroup;
@@ -17,6 +18,9 @@
1718

1819
/**
1920
* Adapter to paginate a Cloud Firestore query and bind it to a RecyclerView.
21+
*
22+
* See also {@link FirestorePagingOptions}.
23+
* See also {@link FirestoreInfiniteScrollListener}.
2024
*/
2125
public abstract class FirestorePagingAdapter<T, VH extends RecyclerView.ViewHolder>
2226
extends RecyclerView.Adapter<VH> {
@@ -29,6 +33,9 @@ public abstract class FirestorePagingAdapter<T, VH extends RecyclerView.ViewHold
2933

3034
private List<Page> mPages = new ArrayList<>();
3135

36+
/**
37+
* Create a new paging adapter from the given options.
38+
*/
3239
public FirestorePagingAdapter(FirestorePagingOptions<T> options) {
3340
mOptions = options;
3441

@@ -59,6 +66,11 @@ public void onBindViewHolder(VH holder, int position) {
5966
onBindViewHolder(holder, position, get(position));
6067
}
6168

69+
/**
70+
* Get the number of pages currently loaded in memory. This is <b>not</b> the same
71+
* as the number of pages ever loaded by the adapter as the adapter dynamically unloads
72+
* pages that are not used.
73+
*/
6274
public int getPagesLoaded() {
6375
int count = 0;
6476
for (Page page : mPages) {
@@ -70,7 +82,13 @@ public int getPagesLoaded() {
7082
return count;
7183
}
7284

73-
public void loadPrevPage() {
85+
/**
86+
* When scrolling up through the list, load the next not-yet-loaded page.
87+
*
88+
* If this page load results in the number of loaded pages exceeding the maximum
89+
* specified in the options, unload the bottom-most page.
90+
*/
91+
public void loadPageUp() {
7492
if (countState(PageState.LOADING) > 0) {
7593
return;
7694
}
@@ -95,6 +113,12 @@ public void loadPrevPage() {
95113
}
96114
}
97115

116+
/**
117+
* When scrolling down through the list, load the next not-yet-loaded page.
118+
*
119+
* If this page load results in the number of loaded pages exceeding the maximum
120+
* specified in the options, unload the top-most page.
121+
*/
98122
public void loadNextPage() {
99123
if (countState(PageState.LOADING) > 0) {
100124
return;
@@ -144,6 +168,11 @@ public void loadNextPage() {
144168
}
145169
}
146170

171+
/**
172+
* Get the total number of loaded items among all loaded pages.
173+
*
174+
* Note that this operation is O(n) in the number of pages.
175+
*/
147176
@Override
148177
public int getItemCount() {
149178
int size = 0;
@@ -155,6 +184,12 @@ public int getItemCount() {
155184
return size;
156185
}
157186

187+
/**
188+
* Get the snapshot at the specified index, where 0 is the first loaded snapshot. These
189+
* indexes are relative to the snapshots loaded at any given time and are not absolute.
190+
*
191+
* This operation is O(n) in the number of pages.
192+
*/
158193
@NonNull
159194
public DocumentSnapshot getSnapshot(int index) {
160195
int remaining = index;
@@ -170,11 +205,22 @@ public DocumentSnapshot getSnapshot(int index) {
170205
"Requested non-existent index: " + index + ", size=" + getItemCount());
171206
}
172207

208+
/**
209+
* Get the model at the specified index by converting a snapfrot from
210+
* {@link #getSnapshot(int)}.
211+
*/
173212
@NonNull
174213
public T get(int index) {
175214
return mParser.parseSnapshot(getSnapshot(index));
176215
}
177216

217+
/**
218+
* Called when a page begins or finishes loading, to indicate if there are any current loading
219+
* operations going on.
220+
*
221+
* Useful to override and control UI elements such as a progress bar or loading spinner.
222+
* @param isLoading
223+
*/
178224
// TODO: Better interface
179225
protected void onLoadingStateChanged(boolean isLoading) {
180226
// No-op, this is for overriding.
@@ -310,12 +356,14 @@ private void logd(String message) {
310356
}
311357
}
312358

359+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
313360
private enum PageState {
314361
LOADING,
315362
LOADED,
316363
UNLOADED
317364
}
318365

366+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
319367
private class Page implements OnCompleteListener<QuerySnapshot> {
320368

321369
private final int mIndex;

firestore/src/main/java/com/firebase/ui/firestore/FirestorePagingOptions.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Options to configure a {@link FirestorePagingAdapter}
77
* and {@link FirestoreInfiniteScrollListener}.
88
*
9-
* TODO(samstern): Document all methods.
9+
* See {@link Builder} to create a new instance.
1010
*/
1111
public class FirestorePagingOptions<T> {
1212

@@ -30,22 +30,37 @@ public class FirestorePagingOptions<T> {
3030
mMaxPages = maxPages;
3131
}
3232

33+
/**
34+
* Get the base query to be used for paging.
35+
*/
3336
public Query getQuery() {
3437
return mQuery;
3538
}
3639

40+
/**
41+
* Get the {@link SnapshotParser} to be used to parse Firestore document snapshots.
42+
*/
3743
public SnapshotParser<T> getParser() {
3844
return mParser;
3945
}
4046

47+
/**
48+
* Get the distance from the top/bottom of the list that will trigger a new page load.
49+
*/
4150
public int getLoadTriggerDistance() {
4251
return mLoadTriggerDistance;
4352
}
4453

54+
/**
55+
* Get the max number of pages to keep in memory at any time.
56+
*/
4557
public int getMaxPages() {
4658
return mMaxPages;
4759
}
4860

61+
/**
62+
* Get the maximum number of items to load as a single page.
63+
*/
4964
public int getPageSize() {
5065
return mPageSize;
5166
}
@@ -61,29 +76,54 @@ public static class Builder<T> {
6176

6277
public Builder() {}
6378

79+
/**
80+
* Set the <b>base</b> query for pagination. The query may contain where() and orderBy()
81+
* clauses but may not contain any startAt/endAt or limit clauses as those will be
82+
* added automatically by the paging adapter.
83+
*
84+
* @param query the base query.
85+
* @param parser the SnapshotParser to be used to parse document snapshots.
86+
*/
6487
public Builder<T> setQuery(Query query, SnapshotParser<T> parser) {
6588
mQuery = query;
6689
mParser = parser;
6790
return this;
6891
}
6992

93+
/**
94+
* Sets the query using a standard {@link ClassSnapshotParser}.
95+
*
96+
* See {@link #setQuery(Query, Class)} for details.
97+
*/
7098
public Builder<T> setQuery(Query query, Class<T> clazz) {
7199
mQuery = query;
72100
mParser = new ClassSnapshotParser<>(clazz);
73101
return this;
74102
}
75103

76-
104+
/**
105+
* Set the maximum number of items to load in a single page.
106+
*/
77107
public Builder<T> setPageSize(int pageSize) {
78108
mPageSize = pageSize;
79109
return this;
80110
}
81111

112+
/**
113+
* Set the distance (in items) from the bottom or top of the data set that will trigger
114+
* the adapter to load the next page.
115+
*
116+
* This option is ignored unless a {@link FirestoreInfiniteScrollListener} is
117+
* attached to the adapter.
118+
*/
82119
public Builder<T> setLoadTriggerDistance(int distance) {
83120
mLoadTriggerDistance = distance;
84121
return this;
85122
}
86123

124+
/**
125+
* Set the maximum number of pages to keep in memory at a time.
126+
*/
87127
public Builder<T> setMaxPages(int maxPages) {
88128
mMaxPages = maxPages;
89129
return this;

0 commit comments

Comments
 (0)