Skip to content

Commit dba6df5

Browse files
PatilShreyassamtstern
authored andcommitted
Ability to get occurred exception (#1615)
1 parent 154904e commit dba6df5

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ protected void onLoadingStateChanged(@NonNull LoadingState state) {
111111
break;
112112
}
113113
}
114+
115+
@Override
116+
protected void onError(@NonNull Exception e) {
117+
mSwipeRefreshLayout.setRefreshing(false);
118+
Log.e(TAG, e.getMessage(), e);
119+
}
114120
};
115121

116122
mRecycler.setLayoutManager(new LinearLayoutManager(this));

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public DataSource<PageKey, DocumentSnapshot> create() {
4848
}
4949

5050
private final MutableLiveData<LoadingState> mLoadingState = new MutableLiveData<>();
51+
private final MutableLiveData<Exception> mException = new MutableLiveData<>();
5152

5253
private final Query mBaseQuery;
5354
private final Source mSource;
@@ -133,6 +134,11 @@ public LiveData<LoadingState> getLoadingState() {
133134
return mLoadingState;
134135
}
135136

137+
@NonNull
138+
public LiveData<Exception> getLastError() {
139+
return mException;
140+
}
141+
136142
public void retry() {
137143
LoadingState currentState = mLoadingState.getValue();
138144
if (currentState != LoadingState.ERROR) {
@@ -216,6 +222,9 @@ public void onFailure(@NonNull Exception e) {
216222

217223
// Set the retry action
218224
mRetryRunnable = getRetryRunnable();
225+
226+
//Set to the MutableLiveData to determine Latest Error
227+
mException.postValue(e);
219228
}
220229

221230
protected abstract Runnable getRetryRunnable();

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public abstract class FirestorePagingAdapter<T, VH extends RecyclerView.ViewHold
3232

3333
private final LiveData<PagedList<DocumentSnapshot>> mSnapshots;
3434
private final LiveData<LoadingState> mLoadingState;
35+
private final LiveData<Exception> mException;
3536
private final LiveData<FirestoreDataSource> mDataSource;
3637

3738
/*
@@ -45,6 +46,14 @@ public void onChanged(@Nullable FirestoreDataSource source) {
4546
}
4647
};
4748

49+
//Error observer to determine last occurred Error
50+
private final Observer<Exception> mErrorObserver = new Observer<Exception>() {
51+
@Override
52+
public void onChanged(@Nullable Exception e) {
53+
onError(e);
54+
}
55+
};
56+
4857
private final Observer<LoadingState> mStateObserver =
4958
new Observer<LoadingState>() {
5059
@Override
@@ -94,6 +103,15 @@ public FirestoreDataSource apply(PagedList<DocumentSnapshot> input) {
94103
}
95104
});
96105

106+
mException = Transformations.switchMap(mSnapshots,
107+
new Function<PagedList<DocumentSnapshot>, LiveData<Exception>>() {
108+
@Override
109+
public LiveData<Exception> apply(PagedList<DocumentSnapshot> input) {
110+
FirestoreDataSource dataSource = (FirestoreDataSource) input.getDataSource();
111+
return dataSource.getLastError();
112+
}
113+
});
114+
97115
mParser = options.getParser();
98116

99117
if (options.getOwner() != null) {
@@ -135,6 +153,7 @@ public void startListening() {
135153
mSnapshots.observeForever(mDataObserver);
136154
mLoadingState.observeForever(mStateObserver);
137155
mDataSource.observeForever(mDataSourceObserver);
156+
mException.observeForever(mErrorObserver);
138157
}
139158

140159
/**
@@ -146,6 +165,7 @@ public void stopListening() {
146165
mSnapshots.removeObserver(mDataObserver);
147166
mLoadingState.removeObserver(mStateObserver);
148167
mDataSource.removeObserver(mDataSourceObserver);
168+
mException.removeObserver(mErrorObserver);
149169
}
150170

151171
@Override
@@ -169,4 +189,13 @@ public void onBindViewHolder(@NonNull VH holder, int position) {
169189
protected void onLoadingStateChanged(@NonNull LoadingState state) {
170190
// For overriding
171191
}
192+
193+
/**
194+
* Called whenever the {@link Exception} is caught.
195+
*
196+
* When {@link Exception} is caught the adapter will stop loading any data
197+
*/
198+
protected void onError(@NonNull Exception e) {
199+
// For overriding
200+
}
172201
}

0 commit comments

Comments
 (0)