Skip to content

Commit 75240b7

Browse files
committed
Some cleanup
Change-Id: I278fe22606f979011409b37938092763358b4dac
1 parent 8e08336 commit 75240b7

File tree

4 files changed

+197
-104
lines changed

4 files changed

+197
-104
lines changed

app/src/main/java/com/firebase/uidemo/database/firestore/FirestoreChatActivity.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import android.support.v7.widget.RecyclerView;
88
import android.util.Log;
99
import android.view.LayoutInflater;
10-
import android.view.View;
1110
import android.view.ViewGroup;
1211
import android.widget.Button;
1312
import android.widget.EditText;
@@ -16,10 +15,10 @@
1615

1716
import com.firebase.ui.auth.util.ui.ImeHelper;
1817
import com.firebase.ui.firestore.ClassSnapshotParser;
19-
import com.firebase.ui.firestore.FirestoreInfiniteArray;
2018
import com.firebase.ui.firestore.FirestoreInfiniteScrollListener;
19+
import com.firebase.ui.firestore.FirestorePagingAdapter;
20+
import com.firebase.ui.firestore.FirestorePagingOptions;
2121
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
22-
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
2322
import com.firebase.uidemo.R;
2423
import com.firebase.uidemo.database.ChatHolder;
2524
import com.firebase.uidemo.util.SignInResultNotifier;
@@ -51,7 +50,6 @@ public class FirestoreChatActivity extends AppCompatActivity
5150
private static final Query sChatQuery = sChatCollection.orderBy("timestamp").limit(50);
5251

5352
private LinearLayoutManager mManager;
54-
private FirestoreInfiniteArray<Chat> mArray;
5553

5654
static {
5755
FirebaseFirestore.setLoggingEnabled(true);
@@ -75,11 +73,6 @@ protected void onCreate(Bundle savedInstanceState) {
7573
setContentView(R.layout.activity_chat);
7674
ButterKnife.bind(this);
7775

78-
mArray = new FirestoreInfiniteArray<>(
79-
sChatCollection.orderBy("timestamp", Query.Direction.ASCENDING),
80-
sChatCollection.orderBy("timestamp", Query.Direction.DESCENDING),
81-
new ClassSnapshotParser<>(Chat.class));
82-
8376
mManager = new LinearLayoutManager(this);
8477
mRecyclerView.setHasFixedSize(true);
8578
mRecyclerView.setLayoutManager(mManager);
@@ -95,7 +88,6 @@ public void onDonePressed() {
9588
@Override
9689
public void onStart() {
9790
super.onStart();
98-
if (isSignedIn()) { attachRecyclerViewAdapter(); }
9991
FirebaseAuth.getInstance().addAuthStateListener(this);
10092
}
10193

@@ -123,7 +115,7 @@ private boolean isSignedIn() {
123115
}
124116

125117
private void attachRecyclerViewAdapter() {
126-
final RecyclerView.Adapter adapter = newAdapter();
118+
final FirestorePagingAdapter adapter = newAdapter();
127119

128120
// Scroll to bottom on new messages
129121
// TODO
@@ -134,11 +126,9 @@ private void attachRecyclerViewAdapter() {
134126
// }
135127
// });
136128

137-
// TODO
138-
mArray.setAdapter(adapter);
139-
140129
// Scroll listener for infinite
141-
mRecyclerView.addOnScrollListener(new FirestoreInfiniteScrollListener(mManager, mArray));
130+
mRecyclerView.addOnScrollListener(
131+
new FirestoreInfiniteScrollListener(mManager, adapter));
142132

143133
mRecyclerView.setAdapter(adapter);
144134
}
@@ -153,14 +143,17 @@ public void onSendClick() {
153143
mMessageEdit.setText("");
154144
}
155145

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) {
162156

163-
return new FirestoreRecyclerAdapter<Chat, ChatHolder>(options) {
164157
@Override
165158
public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
166159
return new ChatHolder(LayoutInflater.from(parent.getContext())
@@ -171,12 +164,6 @@ public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
171164
protected void onBindViewHolder(@NonNull ChatHolder holder, int position, @NonNull Chat model) {
172165
holder.bind(model);
173166
}
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-
}
180167
};
181168
}
182169

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66

77
public class FirestoreInfiniteScrollListener extends RecyclerView.OnScrollListener {
88

9-
private LinearLayoutManager mManager;
10-
private FirestoreInfiniteArray mArray;
9+
private final LinearLayoutManager mManager;
10+
private final FirestorePagingAdapter mAdapter;
1111

1212
private Handler mHandler = new Handler();
1313

14-
// TODO: This could take the same options as the Array
1514
public FirestoreInfiniteScrollListener(LinearLayoutManager manager,
16-
FirestoreInfiniteArray array) {
15+
FirestorePagingAdapter adapter) {
1716
mManager = manager;
18-
mArray = array;
17+
mAdapter = adapter;
1918
}
2019

2120
@Override
@@ -30,44 +29,41 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
3029
int firstVisible = mManager.findFirstVisibleItemPosition();
3130
int lastVisible = mManager.findLastVisibleItemPosition();
3231

33-
int totalSize = mArray.size();
32+
int totalSize = mAdapter.getItemCount();
3433

35-
// TODO: configurable "closeness" and number of pages to keep
3634
boolean movingDown = dy > 0;
3735
boolean movingUp = !movingDown;
38-
boolean closeToTop = (firstVisible <= 5);
39-
boolean closeToBottom = (totalSize - lastVisible) <= 5;
36+
boolean closeToTop = (firstVisible <= mAdapter.getOptions().getLoadTriggerDistance());
37+
boolean closeToBottom = (totalSize - lastVisible) <= mAdapter.getOptions().getLoadTriggerDistance();
4038

4139
if (closeToBottom && movingDown) {
42-
4340
mHandler.post(new Runnable() {
4441
@Override
4542
public void run() {
4643
// Load one page down
47-
mArray.loadNextPage();
44+
mAdapter.loadNextPage();
4845

4946
// Unload top page.
50-
if (mArray.getPagesLoaded() >= 3 ) {
51-
mArray.unloadTopPage();
47+
// TODO: This should probably be inside the paging adapter
48+
if (mAdapter.getPagesLoaded() > mAdapter.getOptions().getMaxPages() ) {
49+
mAdapter.unloadTopPage();
5250
}
5351
}
5452
});
5553

5654
} else if (closeToTop && movingUp) {
57-
5855
mHandler.post(new Runnable() {
5956
@Override
6057
public void run() {
6158
// Load one page up
62-
mArray.loadPrevPage();
59+
mAdapter.loadPrevPage();
6360

6461
// Unload bottom page
65-
if (mArray.getPagesLoaded() >= 3) {
66-
mArray.unloadBottomPage();
62+
if (mAdapter.getPagesLoaded() > mAdapter.getOptions().getMaxPages()) {
63+
mAdapter.unloadBottomPage();
6764
}
6865
}
6966
});
70-
7167
}
7268
}
7369

0 commit comments

Comments
 (0)