7
7
import android .support .v7 .widget .RecyclerView ;
8
8
import android .util .Log ;
9
9
import android .view .LayoutInflater ;
10
- import android .view .View ;
11
10
import android .view .ViewGroup ;
12
11
import android .widget .Button ;
13
12
import android .widget .EditText ;
16
15
17
16
import com .firebase .ui .auth .util .ui .ImeHelper ;
18
17
import com .firebase .ui .firestore .ClassSnapshotParser ;
19
- import com .firebase .ui .firestore .FirestoreInfiniteArray ;
20
18
import com .firebase .ui .firestore .FirestoreInfiniteScrollListener ;
19
+ import com .firebase .ui .firestore .FirestorePagingAdapter ;
20
+ import com .firebase .ui .firestore .FirestorePagingOptions ;
21
21
import com .firebase .ui .firestore .FirestoreRecyclerAdapter ;
22
- import com .firebase .ui .firestore .FirestoreRecyclerOptions ;
23
22
import com .firebase .uidemo .R ;
24
23
import com .firebase .uidemo .database .ChatHolder ;
25
24
import com .firebase .uidemo .util .SignInResultNotifier ;
@@ -51,7 +50,6 @@ public class FirestoreChatActivity extends AppCompatActivity
51
50
private static final Query sChatQuery = sChatCollection .orderBy ("timestamp" ).limit (50 );
52
51
53
52
private LinearLayoutManager mManager ;
54
- private FirestoreInfiniteArray <Chat > mArray ;
55
53
56
54
static {
57
55
FirebaseFirestore .setLoggingEnabled (true );
@@ -75,11 +73,6 @@ protected void onCreate(Bundle savedInstanceState) {
75
73
setContentView (R .layout .activity_chat );
76
74
ButterKnife .bind (this );
77
75
78
- mArray = new FirestoreInfiniteArray <>(
79
- sChatCollection .orderBy ("timestamp" , Query .Direction .ASCENDING ),
80
- sChatCollection .orderBy ("timestamp" , Query .Direction .DESCENDING ),
81
- new ClassSnapshotParser <>(Chat .class ));
82
-
83
76
mManager = new LinearLayoutManager (this );
84
77
mRecyclerView .setHasFixedSize (true );
85
78
mRecyclerView .setLayoutManager (mManager );
@@ -95,7 +88,6 @@ public void onDonePressed() {
95
88
@ Override
96
89
public void onStart () {
97
90
super .onStart ();
98
- if (isSignedIn ()) { attachRecyclerViewAdapter (); }
99
91
FirebaseAuth .getInstance ().addAuthStateListener (this );
100
92
}
101
93
@@ -123,7 +115,7 @@ private boolean isSignedIn() {
123
115
}
124
116
125
117
private void attachRecyclerViewAdapter () {
126
- final RecyclerView . Adapter adapter = newAdapter ();
118
+ final FirestorePagingAdapter adapter = newAdapter ();
127
119
128
120
// Scroll to bottom on new messages
129
121
// TODO
@@ -134,11 +126,9 @@ private void attachRecyclerViewAdapter() {
134
126
// }
135
127
// });
136
128
137
- // TODO
138
- mArray .setAdapter (adapter );
139
-
140
129
// Scroll listener for infinite
141
- mRecyclerView .addOnScrollListener (new FirestoreInfiniteScrollListener (mManager , mArray ));
130
+ mRecyclerView .addOnScrollListener (
131
+ new FirestoreInfiniteScrollListener (mManager , adapter ));
142
132
143
133
mRecyclerView .setAdapter (adapter );
144
134
}
@@ -153,14 +143,17 @@ public void onSendClick() {
153
143
mMessageEdit .setText ("" );
154
144
}
155
145
156
- protected RecyclerView .Adapter newAdapter () {
157
- FirestoreRecyclerOptions <Chat > options =
158
- new FirestoreRecyclerOptions .Builder <Chat >()
159
- .setSnapshotArray (mArray )
160
- .setLifecycleOwner (this )
161
- .build ();
146
+ protected FirestorePagingAdapter newAdapter () {
147
+ FirestorePagingOptions options = new FirestorePagingOptions .Builder ()
148
+ .setLoadTriggerDistance (7 )
149
+ .setMaxPages (3 )
150
+ .build ();
151
+
152
+ return new FirestorePagingAdapter <Chat , ChatHolder >(
153
+ new ClassSnapshotParser <>(Chat .class ),
154
+ sChatCollection .orderBy ("timestamp" , Query .Direction .ASCENDING ),
155
+ options ) {
162
156
163
- return new FirestoreRecyclerAdapter <Chat , ChatHolder >(options ) {
164
157
@ Override
165
158
public ChatHolder onCreateViewHolder (ViewGroup parent , int viewType ) {
166
159
return new ChatHolder (LayoutInflater .from (parent .getContext ())
@@ -171,12 +164,6 @@ public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
171
164
protected void onBindViewHolder (@ NonNull ChatHolder holder , int position , @ NonNull Chat model ) {
172
165
holder .bind (model );
173
166
}
174
-
175
- @ Override
176
- public void onDataChanged () {
177
- // If there are no chat messages, show a view that invites the user to add a message.
178
- mEmptyListMessage .setVisibility (getItemCount () == 0 ? View .VISIBLE : View .GONE );
179
- }
180
167
};
181
168
}
182
169
0 commit comments