Skip to content

Commit 877fb21

Browse files
committed
Address review feedback
Change-Id: Ic3964c31bbeb4a12c43960352d1c8b9304a8741b
1 parent c749cf4 commit 877fb21

File tree

10 files changed

+58
-86
lines changed

10 files changed

+58
-86
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class FirestorePagingActivity extends AppCompatActivity {
4343
ProgressBar mProgressBar;
4444

4545
private FirebaseFirestore mFirestore;
46+
private CollectionReference mItemsCollection;
4647

4748
@Override
4849
protected void onCreate(Bundle savedInstanceState) {
@@ -51,14 +52,13 @@ protected void onCreate(Bundle savedInstanceState) {
5152
ButterKnife.bind(this);
5253

5354
mFirestore = FirebaseFirestore.getInstance();
54-
mProgressBar.setIndeterminate(true);
55+
mItemsCollection = mFirestore.collection("items");
5556

5657
setUpAdapter();
5758
}
5859

5960
private void setUpAdapter() {
60-
Query baseQuery = mFirestore.collection("items")
61-
.orderBy("value", Query.Direction.ASCENDING);
61+
Query baseQuery = mItemsCollection.orderBy("value", Query.Direction.ASCENDING);
6262

6363
PagedList.Config config = new PagedList.Config.Builder()
6464
.setEnablePlaceholders(false)
@@ -101,6 +101,7 @@ protected void onLoadingStateChanged(@NonNull LoadingState state) {
101101
break;
102102
case ERROR:
103103
showToast("An error occurred.");
104+
retry();
104105
break;
105106
}
106107
}
@@ -113,7 +114,7 @@ protected void onLoadingStateChanged(@NonNull LoadingState state) {
113114
@Override
114115
public boolean onCreateOptionsMenu(Menu menu) {
115116
getMenuInflater().inflate(R.menu.menu_firestore_paging, menu);
116-
return super.onCreateOptionsMenu(menu);
117+
return true;
117118
}
118119

119120
@Override
@@ -138,17 +139,15 @@ public void onComplete(@NonNull Task<Void> task) {
138139
}
139140

140141
private Task<Void> createItems() {
141-
142142
WriteBatch writeBatch = mFirestore.batch();
143-
CollectionReference collRef = mFirestore.collection("items");
144143

145-
for (int i = 0; i < 500; i++) {
144+
for (int i = 0; i < 250; i++) {
146145
String title = "Item " + i;
147146

148147
String id = String.format(Locale.getDefault(), "item_%03d", i);
149148
Item item = new Item(title, i);
150149

151-
writeBatch.set(collRef.document(id), item);
150+
writeBatch.set(mItemsCollection.document(id), item);
152151
}
153152

154153
return writeBatch.commit();

app/src/main/res/layout/activity_firestore_paging.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
android:layout_height="wrap_content"
1414
android:layout_marginTop="-6dp"
1515
android:background="@android:color/transparent"
16+
android:indeterminate="true"
1617
tools:ignore="NegativeMargin" />
1718

1819
<android.support.v7.widget.RecyclerView

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<string name="desc_auth">Demonstrates the Firebase Auth UI flow, with customization options.</string>
1212
<string name="desc_firestore">Demonstrates using a FirestoreRecyclerAdapter to load data from Cloud Firestore into a RecyclerView for a basic chat app.</string>
13-
<string name="desc_firestore_paging">Demonstrates using a FirestorePagingAdapter to load/infinite scroll paging data from Cloud Firestore.</string>
13+
<string name="desc_firestore_paging">Demonstrates using a FirestorePagingAdapter to load/infinite scroll paged data from Cloud Firestore.</string>
1414
<string name="desc_realtime_database">Demonstrates using a FirebaseRecyclerAdapter to load data from Firebase Database into a RecyclerView for a basic chat app.</string>
1515
<string name="desc_storage">Demonstrates displaying an image from Cloud Storage using Glide.</string>
1616

firestore/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dependencies {
2222
api "com.android.support:recyclerview-v7:$supportLibraryVersion"
2323
annotationProcessor "android.arch.lifecycle:compiler:$architectureVersion"
2424

25-
api "android.arch.paging:runtime:1.0.0-beta1"
25+
compileOnly "android.arch.paging:runtime:1.0.0-beta1"
2626

2727
androidTestImplementation 'junit:junit:4.12'
2828
androidTestImplementation 'com.android.support.test:runner:1.0.1'

firestore/src/main/java/com/firebase/ui/firestore/paging/SnapshotDiffCallback.java renamed to firestore/src/main/java/com/firebase/ui/firestore/paging/DefaultSnapshotDiffCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
* Default diff callback implementation for Firestore snapshots.
1111
*/
1212
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
13-
public class SnapshotDiffCallback<T> extends DiffUtil.ItemCallback<DocumentSnapshot> {
13+
public class DefaultSnapshotDiffCallback<T> extends DiffUtil.ItemCallback<DocumentSnapshot> {
1414

1515
private final SnapshotParser<T> mParser;
1616

17-
public SnapshotDiffCallback(SnapshotParser<T> parser) {
17+
public DefaultSnapshotDiffCallback(SnapshotParser<T> parser) {
1818
mParser = parser;
1919
}
2020

firestore/src/main/java/com/firebase/ui/firestore/paging/FirestoreDataSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void run() {
178178
private abstract class OnLoadSuccessListener implements OnSuccessListener<QuerySnapshot> {
179179

180180
@Override
181-
public void onSuccess(QuerySnapshot snapshots) {
181+
public void onSuccess(QuerySnapshot snapshot) {
182182
mLoadingState.postValue(LoadingState.LOADED);
183183
mRetryRunnable = null;
184184
}

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.firebase.ui.firestore.paging;
22

3+
import android.arch.core.util.Function;
34
import android.arch.lifecycle.Lifecycle;
45
import android.arch.lifecycle.LifecycleObserver;
6+
import android.arch.lifecycle.LiveData;
57
import android.arch.lifecycle.Observer;
68
import android.arch.lifecycle.OnLifecycleEvent;
9+
import android.arch.lifecycle.Transformations;
710
import android.arch.paging.PagedList;
811
import android.arch.paging.PagedListAdapter;
912
import android.support.annotation.NonNull;
@@ -26,7 +29,10 @@ public abstract class FirestorePagingAdapter<T, VH extends RecyclerView.ViewHold
2629
private static final String TAG = "FirestorePagingAdapter";
2730

2831
private final SnapshotParser<T> mParser;
29-
private final PagingData mData;
32+
33+
private final LiveData<PagedList<DocumentSnapshot>> mSnapshots;
34+
private final LiveData<LoadingState> mLoadingState;
35+
private final LiveData<FirestoreDataSource> mDataSource;
3036

3137
private final Observer<LoadingState> mStateObserver =
3238
new Observer<LoadingState>() {
@@ -55,8 +61,26 @@ public void onChanged(@Nullable PagedList<DocumentSnapshot> snapshots) {
5561
public FirestorePagingAdapter(@NonNull FirestorePagingOptions<T> options) {
5662
super(options.getDiffCallback());
5763

64+
mSnapshots = options.getData();
65+
66+
mLoadingState = Transformations.switchMap(mSnapshots,
67+
new Function<PagedList<DocumentSnapshot>, LiveData<LoadingState>>() {
68+
@Override
69+
public LiveData<LoadingState> apply(PagedList<DocumentSnapshot> input) {
70+
FirestoreDataSource dataSource = (FirestoreDataSource) input.getDataSource();
71+
return dataSource.getLoadingState();
72+
}
73+
});
74+
75+
mDataSource = Transformations.map(mSnapshots,
76+
new Function<PagedList<DocumentSnapshot>, FirestoreDataSource>() {
77+
@Override
78+
public FirestoreDataSource apply(PagedList<DocumentSnapshot> input) {
79+
return (FirestoreDataSource) input.getDataSource();
80+
}
81+
});
82+
5883
mParser = options.getParser();
59-
mData = options.getData();
6084

6185
if (options.getOwner() != null) {
6286
options.getOwner().getLifecycle().addObserver(this);
@@ -68,7 +92,7 @@ public FirestorePagingAdapter(@NonNull FirestorePagingOptions<T> options) {
6892
* to attempt to retry the most recent failure.
6993
*/
7094
public void retry() {
71-
FirestoreDataSource source = mData.getDataSource().getValue();
95+
FirestoreDataSource source = mDataSource.getValue();
7296
if (source == null) {
7397
Log.w(TAG, "Called retry() when FirestoreDataSource is null!");
7498
return;
@@ -79,14 +103,14 @@ public void retry() {
79103

80104
@OnLifecycleEvent(Lifecycle.Event.ON_START)
81105
public void startListening() {
82-
mData.getSnapshots().observeForever(mDataObserver);
83-
mData.getLoadingState().observeForever(mStateObserver);
106+
mSnapshots.observeForever(mDataObserver);
107+
mLoadingState.observeForever(mStateObserver);
84108
}
85109

86110
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
87111
public void stopListening() {
88-
mData.getSnapshots().removeObserver(mDataObserver);
89-
mData.getLoadingState().removeObserver(mStateObserver);
112+
mSnapshots.removeObserver(mDataObserver);
113+
mLoadingState.removeObserver(mStateObserver);
90114
}
91115

92116
@Override
@@ -95,7 +119,7 @@ public void onBindViewHolder(@NonNull VH holder, int position) {
95119
onBindViewHolder(holder, position, mParser.parseSnapshot(snapshot));
96120
}
97121

98-
protected abstract void onBindViewHolder(@NonNull VH holder, int position, T model);
122+
protected abstract void onBindViewHolder(@NonNull VH holder, int position, @NonNull T model);
99123

100124
protected void onLoadingStateChanged(@NonNull LoadingState state) {
101125
// For overriding

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.firebase.ui.firestore.paging;
22

33
import android.arch.lifecycle.LifecycleOwner;
4+
import android.arch.lifecycle.LiveData;
45
import android.arch.paging.LivePagedListBuilder;
56
import android.arch.paging.PagedList;
67
import android.support.annotation.NonNull;
@@ -17,12 +18,12 @@
1718
*/
1819
public class FirestorePagingOptions<T> {
1920

20-
private final PagingData mData;
21+
private final LiveData<PagedList<DocumentSnapshot>> mData;
2122
private final SnapshotParser<T> mParser;
2223
private final DiffUtil.ItemCallback<DocumentSnapshot> mDiffCallback;
2324
private final LifecycleOwner mOwner;
2425

25-
private FirestorePagingOptions(@NonNull PagingData data,
26+
private FirestorePagingOptions(@NonNull LiveData<PagedList<DocumentSnapshot>> data,
2627
@NonNull SnapshotParser<T> parser,
2728
@NonNull DiffUtil.ItemCallback<DocumentSnapshot> diffCallback,
2829
@Nullable LifecycleOwner owner) {
@@ -33,7 +34,7 @@ private FirestorePagingOptions(@NonNull PagingData data,
3334
}
3435

3536
@NonNull
36-
public PagingData getData() {
37+
public LiveData<PagedList<DocumentSnapshot>> getData() {
3738
return mData;
3839
}
3940

@@ -54,7 +55,7 @@ public LifecycleOwner getOwner() {
5455

5556
public static class Builder<T> {
5657

57-
private PagingData mData;
58+
private LiveData<PagedList<DocumentSnapshot>> mData;
5859
private SnapshotParser<T> mParser;
5960
private LifecycleOwner mOwner;
6061
private DiffUtil.ItemCallback<DocumentSnapshot> mDiffCallback;
@@ -72,21 +73,21 @@ public Builder<T> setQuery(@NonNull Query query,
7273
@NonNull SnapshotParser<T> parser) {
7374
// Build paged list
7475
FirestoreDataSource.Factory factory = new FirestoreDataSource.Factory(query);
75-
mData = new PagingData(new LivePagedListBuilder<>(factory, config).build());
76+
mData = new LivePagedListBuilder<>(factory, config).build();
7677

7778
mParser = parser;
7879
return this;
7980
}
8081

8182
@NonNull
82-
public Builder<T> setLifecycleOwner(@NonNull LifecycleOwner owner) {
83-
mOwner = owner;
83+
public Builder<T> setDiffCallback(@NonNull DiffUtil.ItemCallback<DocumentSnapshot> diffCallback) {
84+
mDiffCallback = diffCallback;
8485
return this;
8586
}
8687

8788
@NonNull
88-
public Builder<T> setDiffCallback(@NonNull DiffUtil.ItemCallback<DocumentSnapshot> diffCallback) {
89-
mDiffCallback = diffCallback;
89+
public Builder<T> setLifecycleOwner(@NonNull LifecycleOwner owner) {
90+
mOwner = owner;
9091
return this;
9192
}
9293

@@ -97,7 +98,7 @@ public FirestorePagingOptions<T> build() {
9798
}
9899

99100
if (mDiffCallback == null) {
100-
mDiffCallback = new SnapshotDiffCallback<T>(mParser);
101+
mDiffCallback = new DefaultSnapshotDiffCallback<T>(mParser);
101102
}
102103

103104
return new FirestorePagingOptions<>(mData, mParser, mDiffCallback, mOwner);

firestore/src/main/java/com/firebase/ui/firestore/paging/PageKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public Query getPageQuery(Query baseQuery, int size) {
3838

3939
@Override
4040
public String toString() {
41-
String startAfter = mStartAfter == null ? "null" : mStartAfter.getId();
42-
String endBefore = mEndBefore == null ? "null" : mEndBefore.getId();
41+
String startAfter = mStartAfter == null ? null : mStartAfter.getId();
42+
String endBefore = mEndBefore == null ? null : mEndBefore.getId();
4343
return "PageKey{" +
4444
"StartAfter=" + startAfter +
4545
", EndBefore=" + endBefore +

firestore/src/main/java/com/firebase/ui/firestore/paging/PagingData.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)