Skip to content

Commit cef8528

Browse files
committed
More feedback
Change-Id: Icaa6a6e24a65b87bc6d0e5c49059f72f99522d6c
1 parent 75e206a commit cef8528

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

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

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.google.firebase.firestore.Query;
1616
import com.google.firebase.firestore.QuerySnapshot;
1717

18-
import java.util.Collections;
1918
import java.util.List;
2019

2120
/**
@@ -60,29 +59,13 @@ public void loadInitial(@NonNull LoadInitialParams<PageKey> params,
6059
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
6160
@Override
6261
public void onSuccess(QuerySnapshot snapshot) {
63-
List<DocumentSnapshot> data = snapshot.getDocuments();
64-
DocumentSnapshot last = getLast(data);
65-
66-
PageKey nextPage = new PageKey(last, null);
67-
callback.onResult(data, null, nextPage);
62+
PageKey nextPage = getNextPageKey(snapshot);
63+
callback.onResult(snapshot.getDocuments(), null, nextPage);
6864

6965
mLoadingState.postValue(LoadingState.LOADED);
7066
}
7167
})
72-
.addOnFailureListener(new OnFailureListener() {
73-
@Override
74-
public void onFailure(@NonNull Exception e) {
75-
Log.w(TAG, "loadInitial:failure", e);
76-
77-
// On error, return an empty page with the next page key being basically
78-
// equal to the initial query.
79-
PageKey nextPage = new PageKey(null, null);
80-
callback.onResult(Collections.<DocumentSnapshot>emptyList(),
81-
null, nextPage);
82-
83-
mLoadingState.postValue(LoadingState.ERROR);
84-
}
85-
});
68+
.addOnFailureListener(new OnLoadFailureListener());
8669

8770
}
8871

@@ -109,28 +92,21 @@ public void loadAfter(@NonNull LoadParams<PageKey> params,
10992
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
11093
@Override
11194
public void onSuccess(QuerySnapshot snapshot) {
112-
List<DocumentSnapshot> data = snapshot.getDocuments();
113-
DocumentSnapshot last = getLast(data);
114-
115-
PageKey nextPage = new PageKey(last, null);
116-
callback.onResult(data, nextPage);
95+
PageKey nextPage = getNextPageKey(snapshot);
96+
callback.onResult(snapshot.getDocuments(), nextPage);
11797

11898
mLoadingState.postValue(LoadingState.LOADED);
11999
}
120100
})
121-
.addOnFailureListener(new OnFailureListener() {
122-
@Override
123-
public void onFailure(@NonNull Exception e) {
124-
Log.w(TAG, "loadAfter:failure", e);
101+
.addOnFailureListener(new OnLoadFailureListener());
125102

126-
// On error, return an empty page with the next page key being basically
127-
// equal to the initial query.
128-
callback.onResult(Collections.<DocumentSnapshot>emptyList(), key);
103+
}
129104

130-
mLoadingState.postValue(LoadingState.ERROR);
131-
}
132-
});
105+
private PageKey getNextPageKey(@NonNull QuerySnapshot snapshot) {
106+
List<DocumentSnapshot> data = snapshot.getDocuments();
107+
DocumentSnapshot last = getLast(data);
133108

109+
return new PageKey(last, null);
134110
}
135111

136112
public LiveData<LoadingState> getLoadingState() {
@@ -145,4 +121,19 @@ private DocumentSnapshot getLast(@NonNull List<DocumentSnapshot> data) {
145121
return data.get(data.size() - 1);
146122
}
147123
}
124+
125+
/**
126+
* Error listener that just logs and sets the error state.
127+
*/
128+
private class OnLoadFailureListener implements OnFailureListener {
129+
130+
@Override
131+
public void onFailure(@NonNull Exception e) {
132+
Log.w(TAG, "load:onFailure", e);
133+
134+
// On error we do NOT post any value to the PagedList, we just tell
135+
// the developer that we are now in the error state.
136+
mLoadingState.postValue(LoadingState.ERROR);
137+
}
138+
}
148139
}

0 commit comments

Comments
 (0)