Skip to content

Commit 5391e97

Browse files
committed
Add retry() in the data source
Change-Id: I435f0713b3cfa4d1dc9c242defa2a4f0c2d216e1
1 parent 154ecde commit 5391e97

File tree

1 file changed

+82
-13
lines changed

1 file changed

+82
-13
lines changed

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

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
/**
2121
* Data source to power a {@link FirestorePagingAdapter}.
22+
*
23+
* Note: although loadInitial, loadBefore, and loadAfter are not called on the main thread by the
24+
* paging library, we treat them as if they were so that we can facilitate retry without
25+
* managing our own thread pool or requiring the user to pass us an executor.
2226
*/
2327
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
2428
public class FirestoreDataSource extends PageKeyedDataSource<PageKey, DocumentSnapshot> {
@@ -43,29 +47,35 @@ public DataSource<PageKey, DocumentSnapshot> create() {
4347

4448
private final Query mBaseQuery;
4549

50+
private Runnable mRetryRunnable;
51+
4652
public FirestoreDataSource(Query baseQuery) {
4753
mBaseQuery = baseQuery;
4854
}
4955

5056
@Override
51-
public void loadInitial(@NonNull LoadInitialParams<PageKey> params,
57+
public void loadInitial(@NonNull final LoadInitialParams<PageKey> params,
5258
@NonNull final LoadInitialCallback<PageKey, DocumentSnapshot> callback) {
5359

5460
// Set initial loading state
5561
mLoadingState.postValue(LoadingState.LOADING_INITIAL);
5662

5763
mBaseQuery.limit(params.requestedLoadSize)
5864
.get()
59-
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
65+
.addOnSuccessListener(new OnLoadSuccessListener() {
6066
@Override
61-
public void onSuccess(QuerySnapshot snapshot) {
67+
protected void setResult(@NonNull QuerySnapshot snapshot) {
6268
PageKey nextPage = getNextPageKey(snapshot);
6369
callback.onResult(snapshot.getDocuments(), null, nextPage);
6470

65-
mLoadingState.postValue(LoadingState.LOADED);
6671
}
6772
})
68-
.addOnFailureListener(new OnLoadFailureListener());
73+
.addOnFailureListener(new OnLoadFailureListener() {
74+
@Override
75+
protected Runnable getRetryRunnable() {
76+
return getRetryLoadInitial(params, callback);
77+
}
78+
});
6979

7080
}
7181

@@ -80,7 +90,7 @@ public void loadBefore(@NonNull LoadParams<PageKey> params,
8090
}
8191

8292
@Override
83-
public void loadAfter(@NonNull LoadParams<PageKey> params,
93+
public void loadAfter(@NonNull final LoadParams<PageKey> params,
8494
@NonNull final LoadCallback<PageKey, DocumentSnapshot> callback) {
8595
final PageKey key = params.key;
8696

@@ -89,16 +99,19 @@ public void loadAfter(@NonNull LoadParams<PageKey> params,
8999

90100
key.getPageQuery(mBaseQuery, params.requestedLoadSize)
91101
.get()
92-
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
102+
.addOnSuccessListener(new OnLoadSuccessListener() {
93103
@Override
94-
public void onSuccess(QuerySnapshot snapshot) {
104+
protected void setResult(@NonNull QuerySnapshot snapshot) {
95105
PageKey nextPage = getNextPageKey(snapshot);
96106
callback.onResult(snapshot.getDocuments(), nextPage);
97-
98-
mLoadingState.postValue(LoadingState.LOADED);
99107
}
100108
})
101-
.addOnFailureListener(new OnLoadFailureListener());
109+
.addOnFailureListener(new OnLoadFailureListener() {
110+
@Override
111+
protected Runnable getRetryRunnable() {
112+
return getRetryLoadAfter(params, callback);
113+
}
114+
});
102115

103116
}
104117

@@ -113,6 +126,21 @@ public LiveData<LoadingState> getLoadingState() {
113126
return mLoadingState;
114127
}
115128

129+
public void retry() {
130+
LoadingState currentState = mLoadingState.getValue();
131+
if (currentState != LoadingState.ERROR) {
132+
Log.w(TAG, "retry() not valid when in state: " + currentState);
133+
return;
134+
}
135+
136+
if (mRetryRunnable == null) {
137+
Log.w(TAG, "retry() called with no eligible retry runnable.");
138+
return;
139+
}
140+
141+
mRetryRunnable.run();
142+
}
143+
116144
@Nullable
117145
private DocumentSnapshot getLast(@NonNull List<DocumentSnapshot> data) {
118146
if (data.isEmpty()) {
@@ -122,10 +150,46 @@ private DocumentSnapshot getLast(@NonNull List<DocumentSnapshot> data) {
122150
}
123151
}
124152

153+
@NonNull
154+
private Runnable getRetryLoadAfter(@NonNull final LoadParams<PageKey> params,
155+
@NonNull final LoadCallback<PageKey, DocumentSnapshot> callback) {
156+
return new Runnable() {
157+
@Override
158+
public void run() {
159+
loadAfter(params, callback);
160+
}
161+
};
162+
}
163+
164+
@NonNull
165+
private Runnable getRetryLoadInitial(@NonNull final LoadInitialParams<PageKey> params,
166+
@NonNull final LoadInitialCallback<PageKey, DocumentSnapshot> callback) {
167+
return new Runnable() {
168+
@Override
169+
public void run() {
170+
loadInitial(params, callback);
171+
}
172+
};
173+
}
174+
175+
/**
176+
* Success listener that sets success state and nullifies the retry runnable.
177+
*/
178+
private abstract class OnLoadSuccessListener implements OnSuccessListener<QuerySnapshot> {
179+
180+
@Override
181+
public void onSuccess(QuerySnapshot snapshots) {
182+
mLoadingState.postValue(LoadingState.LOADED);
183+
mRetryRunnable = null;
184+
}
185+
186+
protected abstract void setResult(@NonNull QuerySnapshot snapshot);
187+
}
188+
125189
/**
126-
* Error listener that just logs and sets the error state.
190+
* Error listener that logs, sets the error state, and sets up retry.
127191
*/
128-
private class OnLoadFailureListener implements OnFailureListener {
192+
private abstract class OnLoadFailureListener implements OnFailureListener {
129193

130194
@Override
131195
public void onFailure(@NonNull Exception e) {
@@ -134,6 +198,11 @@ public void onFailure(@NonNull Exception e) {
134198
// On error we do NOT post any value to the PagedList, we just tell
135199
// the developer that we are now in the error state.
136200
mLoadingState.postValue(LoadingState.ERROR);
201+
202+
// Set the retry action
203+
mRetryRunnable = getRetryRunnable();
137204
}
205+
206+
protected abstract Runnable getRetryRunnable();
138207
}
139208
}

0 commit comments

Comments
 (0)