1
1
package com .firebase .uidemo .database .firestore ;
2
2
3
3
import android .os .Bundle ;
4
+ import android .support .annotation .NonNull ;
4
5
import android .support .v7 .app .AppCompatActivity ;
5
6
import android .support .v7 .widget .LinearLayoutManager ;
6
7
import android .support .v7 .widget .RecyclerView ;
8
+ import android .util .Log ;
7
9
import android .view .LayoutInflater ;
10
+ import android .view .Menu ;
11
+ import android .view .MenuItem ;
8
12
import android .view .View ;
9
13
import android .view .ViewGroup ;
14
+ import android .widget .ProgressBar ;
10
15
import android .widget .TextView ;
16
+ import android .widget .Toast ;
11
17
12
18
import com .firebase .ui .firestore .FirestoreInfiniteScrollListener ;
13
19
import com .firebase .ui .firestore .FirestorePagingAdapter ;
14
20
import com .firebase .ui .firestore .FirestorePagingOptions ;
15
21
import com .firebase .uidemo .R ;
22
+ import com .google .android .gms .tasks .OnCompleteListener ;
16
23
import com .google .android .gms .tasks .Task ;
17
24
import com .google .firebase .firestore .CollectionReference ;
18
25
import com .google .firebase .firestore .FirebaseFirestore ;
26
33
27
34
public class FirestorePagingActivity extends AppCompatActivity {
28
35
36
+ private static final String TAG = "FirestorePagingActivity" ;
37
+
29
38
@ BindView (R .id .paging_recycler )
30
39
RecyclerView mRecycler ;
31
40
41
+ @ BindView (R .id .paging_loading )
42
+ ProgressBar mProgressBar ;
43
+
44
+ private FirebaseFirestore mFirestore ;
45
+
32
46
@ Override
33
47
protected void onCreate (Bundle savedInstanceState ) {
34
48
super .onCreate (savedInstanceState );
35
49
setContentView (R .layout .activity_firestore_paging );
36
50
ButterKnife .bind (this );
37
51
52
+ mFirestore = FirebaseFirestore .getInstance ();
53
+ mProgressBar .setIndeterminate (true );
54
+
38
55
setUpAdapter ();
39
56
}
40
57
41
58
private void setUpAdapter () {
42
- Query baseQuery = FirebaseFirestore .getInstance ()
43
- .collection ("items" )
59
+ Query baseQuery = mFirestore .collection ("items" )
44
60
.orderBy ("value" , Query .Direction .ASCENDING );
45
61
62
+ // Paging options:
63
+ // * pageSize - number of items to load in each 'page'.
64
+ // * loadTriggerDistance - how far from the bottom/top of the data set to trigger a load.
65
+ // * maxPages - maximum number of pages to keep in memory at a time.
46
66
FirestorePagingOptions <Item > options = new FirestorePagingOptions .Builder <Item >()
47
67
.setQuery (baseQuery , Item .class )
48
- .setPageSize (10 )
49
- .setLoadTriggerDistance (5 )
50
- .setMaxPages (3 )
68
+ .setPageSize (20 )
69
+ .setLoadTriggerDistance (10 )
70
+ .setMaxPages (5 )
51
71
.build ();
52
72
53
73
FirestorePagingAdapter <Item , ItemViewHolder > adapter =
@@ -64,10 +84,22 @@ public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
64
84
protected void onBindViewHolder (ItemViewHolder holder , int position , Item model ) {
65
85
holder .bind (model );
66
86
}
87
+
88
+ @ Override
89
+ protected void onLoadingStateChanged (boolean isLoading ) {
90
+ super .onLoadingStateChanged (isLoading );
91
+ if (isLoading ) {
92
+ mProgressBar .setVisibility (View .VISIBLE );
93
+ } else {
94
+ mProgressBar .setVisibility (View .INVISIBLE );
95
+ }
96
+ }
67
97
};
68
98
69
99
LinearLayoutManager manager = new LinearLayoutManager (this );
70
100
101
+ // The infinite scroll listener will instruct the adapter to page up and down at the
102
+ // appropriate times.
71
103
FirestoreInfiniteScrollListener listener =
72
104
new FirestoreInfiniteScrollListener (manager , adapter );
73
105
@@ -76,11 +108,37 @@ protected void onBindViewHolder(ItemViewHolder holder, int position, Item model)
76
108
mRecycler .addOnScrollListener (listener );
77
109
}
78
110
79
- // TODO: Bind this to a menu item
80
- public Task <Void > createItems () {
111
+ @ Override
112
+ public boolean onCreateOptionsMenu (Menu menu ) {
113
+ getMenuInflater ().inflate (R .menu .menu_firestore_paging , menu );
114
+ return super .onCreateOptionsMenu (menu );
115
+ }
116
+
117
+ @ Override
118
+ public boolean onOptionsItemSelected (MenuItem item ) {
119
+ if (item .getItemId () == R .id .item_add_data ) {
120
+ showToast ("Adding data..." );
121
+ createItems ().addOnCompleteListener (this , new OnCompleteListener <Void >() {
122
+ @ Override
123
+ public void onComplete (@ NonNull Task <Void > task ) {
124
+ if (task .isSuccessful ()) {
125
+ showToast ("Data added." );
126
+ } else {
127
+ Log .w (TAG , "addData" , task .getException ());
128
+ showToast ("Error adding data." );
129
+ }
130
+ }
131
+ });
81
132
82
- WriteBatch writeBatch = FirebaseFirestore .getInstance ().batch ();
83
- CollectionReference collRef = FirebaseFirestore .getInstance ().collection ("items" );
133
+ return true ;
134
+ }
135
+ return super .onOptionsItemSelected (item );
136
+ }
137
+
138
+ private Task <Void > createItems () {
139
+
140
+ WriteBatch writeBatch = mFirestore .batch ();
141
+ CollectionReference collRef = mFirestore .collection ("items" );
84
142
85
143
for (int i = 0 ; i < 500 ; i ++) {
86
144
String title = "Item " + i ;
@@ -94,6 +152,10 @@ public Task<Void> createItems() {
94
152
return writeBatch .commit ();
95
153
}
96
154
155
+ private void showToast (String message ) {
156
+ Toast .makeText (this , message , Toast .LENGTH_SHORT ).show ();
157
+ }
158
+
97
159
public static class Item {
98
160
99
161
public String text ;
0 commit comments