Skip to content

Ability to get occurred exception - Issue 1592 #1615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ protected void onLoadingStateChanged(@NonNull LoadingState state) {
break;
}
}

@Override
protected void onError(@NonNull Exception e) {
mSwipeRefreshLayout.setRefreshing(false);
Log.e(TAG, e.getMessage(), e);
}
};

mRecycler.setLayoutManager(new LinearLayoutManager(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public DataSource<PageKey, DocumentSnapshot> create() {
}

private final MutableLiveData<LoadingState> mLoadingState = new MutableLiveData<>();
private final MutableLiveData<Exception> mException = new MutableLiveData<>();

private final Query mBaseQuery;
private final Source mSource;
Expand Down Expand Up @@ -133,6 +134,11 @@ public LiveData<LoadingState> getLoadingState() {
return mLoadingState;
}

@NonNull
public LiveData<Exception> getLastError() {
return mException;
}

public void retry() {
LoadingState currentState = mLoadingState.getValue();
if (currentState != LoadingState.ERROR) {
Expand Down Expand Up @@ -216,6 +222,9 @@ public void onFailure(@NonNull Exception e) {

// Set the retry action
mRetryRunnable = getRetryRunnable();

//Set to the MutableLiveData to determine Latest Error
mException.postValue(e);
}

protected abstract Runnable getRetryRunnable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class FirestorePagingAdapter<T, VH extends RecyclerView.ViewHold

private final LiveData<PagedList<DocumentSnapshot>> mSnapshots;
private final LiveData<LoadingState> mLoadingState;
private final LiveData<Exception> mException;
private final LiveData<FirestoreDataSource> mDataSource;

/*
Expand All @@ -45,6 +46,14 @@ public void onChanged(@Nullable FirestoreDataSource source) {
}
};

//Error observer to determine last occurred Error
private final Observer<Exception> mErrorObserver = new Observer<Exception>() {
@Override
public void onChanged(@Nullable Exception e) {
onError(e);
}
};

private final Observer<LoadingState> mStateObserver =
new Observer<LoadingState>() {
@Override
Expand Down Expand Up @@ -94,6 +103,15 @@ public FirestoreDataSource apply(PagedList<DocumentSnapshot> input) {
}
});

mException = Transformations.switchMap(mSnapshots,
new Function<PagedList<DocumentSnapshot>, LiveData<Exception>>() {
@Override
public LiveData<Exception> apply(PagedList<DocumentSnapshot> input) {
FirestoreDataSource dataSource = (FirestoreDataSource) input.getDataSource();
return dataSource.getLastError();
}
});

mParser = options.getParser();

if (options.getOwner() != null) {
Expand Down Expand Up @@ -135,6 +153,7 @@ public void startListening() {
mSnapshots.observeForever(mDataObserver);
mLoadingState.observeForever(mStateObserver);
mDataSource.observeForever(mDataSourceObserver);
mException.observeForever(mErrorObserver);
}

/**
Expand All @@ -146,6 +165,7 @@ public void stopListening() {
mSnapshots.removeObserver(mDataObserver);
mLoadingState.removeObserver(mStateObserver);
mDataSource.removeObserver(mDataSourceObserver);
mException.removeObserver(mErrorObserver);
}

@Override
Expand All @@ -169,4 +189,13 @@ public void onBindViewHolder(@NonNull VH holder, int position) {
protected void onLoadingStateChanged(@NonNull LoadingState state) {
// For overriding
}

/**
* Called whenever the {@link Exception} is caught.
*
* When {@link Exception} is caught the adapter will stop loading any data
*/
protected void onError(@NonNull Exception e) {
// For overriding
}
}