2
2
3
3
import android .support .annotation .NonNull ;
4
4
import android .support .annotation .Nullable ;
5
+ import android .support .annotation .RestrictTo ;
5
6
import android .support .v7 .widget .RecyclerView ;
6
7
import android .util .Log ;
7
8
import android .view .ViewGroup ;
17
18
18
19
/**
19
20
* Adapter to paginate a Cloud Firestore query and bind it to a RecyclerView.
21
+ *
22
+ * See also {@link FirestorePagingOptions}.
23
+ * See also {@link FirestoreInfiniteScrollListener}.
20
24
*/
21
25
public abstract class FirestorePagingAdapter <T , VH extends RecyclerView .ViewHolder >
22
26
extends RecyclerView .Adapter <VH > {
@@ -29,6 +33,9 @@ public abstract class FirestorePagingAdapter<T, VH extends RecyclerView.ViewHold
29
33
30
34
private List <Page > mPages = new ArrayList <>();
31
35
36
+ /**
37
+ * Create a new paging adapter from the given options.
38
+ */
32
39
public FirestorePagingAdapter (FirestorePagingOptions <T > options ) {
33
40
mOptions = options ;
34
41
@@ -59,6 +66,11 @@ public void onBindViewHolder(VH holder, int position) {
59
66
onBindViewHolder (holder , position , get (position ));
60
67
}
61
68
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
+ */
62
74
public int getPagesLoaded () {
63
75
int count = 0 ;
64
76
for (Page page : mPages ) {
@@ -70,7 +82,13 @@ public int getPagesLoaded() {
70
82
return count ;
71
83
}
72
84
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 () {
74
92
if (countState (PageState .LOADING ) > 0 ) {
75
93
return ;
76
94
}
@@ -95,6 +113,12 @@ public void loadPrevPage() {
95
113
}
96
114
}
97
115
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
+ */
98
122
public void loadNextPage () {
99
123
if (countState (PageState .LOADING ) > 0 ) {
100
124
return ;
@@ -144,6 +168,11 @@ public void loadNextPage() {
144
168
}
145
169
}
146
170
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
+ */
147
176
@ Override
148
177
public int getItemCount () {
149
178
int size = 0 ;
@@ -155,6 +184,12 @@ public int getItemCount() {
155
184
return size ;
156
185
}
157
186
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
+ */
158
193
@ NonNull
159
194
public DocumentSnapshot getSnapshot (int index ) {
160
195
int remaining = index ;
@@ -170,11 +205,22 @@ public DocumentSnapshot getSnapshot(int index) {
170
205
"Requested non-existent index: " + index + ", size=" + getItemCount ());
171
206
}
172
207
208
+ /**
209
+ * Get the model at the specified index by converting a snapfrot from
210
+ * {@link #getSnapshot(int)}.
211
+ */
173
212
@ NonNull
174
213
public T get (int index ) {
175
214
return mParser .parseSnapshot (getSnapshot (index ));
176
215
}
177
216
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
+ */
178
224
// TODO: Better interface
179
225
protected void onLoadingStateChanged (boolean isLoading ) {
180
226
// No-op, this is for overriding.
@@ -310,12 +356,14 @@ private void logd(String message) {
310
356
}
311
357
}
312
358
359
+ @ RestrictTo (RestrictTo .Scope .LIBRARY_GROUP )
313
360
private enum PageState {
314
361
LOADING ,
315
362
LOADED ,
316
363
UNLOADED
317
364
}
318
365
366
+ @ RestrictTo (RestrictTo .Scope .LIBRARY_GROUP )
319
367
private class Page implements OnCompleteListener <QuerySnapshot > {
320
368
321
369
private final int mIndex ;
0 commit comments