Skip to content

Fix Issue #1608 - retry() failing in FirestorePagingAdapter #1609

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 3 commits into from
Mar 28, 2019
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 @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -40,8 +41,8 @@ public class FirestorePagingActivity extends AppCompatActivity {
@BindView(R.id.paging_recycler)
RecyclerView mRecycler;

@BindView(R.id.paging_loading)
ProgressBar mProgressBar;
@BindView(R.id.swipe_refresh_layout)
SwipeRefreshLayout mSwipeRefreshLayout;

private FirebaseFirestore mFirestore;
private CollectionReference mItemsCollection;
Expand Down Expand Up @@ -72,7 +73,7 @@ private void setUpAdapter() {
.setQuery(baseQuery, config, Item.class)
.build();

FirestorePagingAdapter<Item, ItemViewHolder> adapter =
final FirestorePagingAdapter<Item, ItemViewHolder> adapter =
new FirestorePagingAdapter<Item, ItemViewHolder>(options) {
@NonNull
@Override
Expand All @@ -95,13 +96,13 @@ protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
case LOADING_MORE:
mProgressBar.setVisibility(View.VISIBLE);
mSwipeRefreshLayout.setRefreshing(true);
break;
case LOADED:
mProgressBar.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false);
break;
case FINISHED:
mProgressBar.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false);
showToast("Reached end of data set.");
break;
case ERROR:
Expand All @@ -114,6 +115,13 @@ protected void onLoadingStateChanged(@NonNull LoadingState state) {

mRecycler.setLayoutManager(new LinearLayoutManager(this));
mRecycler.setAdapter(adapter);

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
adapter.refresh();
}
});
}

@Override
Expand Down
15 changes: 3 additions & 12 deletions app/src/main/res/layout/activity_firestore_paging.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.firebase.uidemo.database.firestore.FirestorePagingActivity">

<ProgressBar
android:id="@+id/paging_loading"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-6dp"
android:background="@android:color/transparent"
android:indeterminate="true"
tools:ignore="NegativeMargin" />

<android.support.v7.widget.RecyclerView
android:id="@+id/paging_recycler"
android:layout_width="match_parent"
Expand All @@ -27,4 +18,4 @@
android:clipToPadding="false"
tools:listitem="@layout/item_item" />

</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public abstract class FirebaseRecyclerPagingAdapter<T, VH extends RecyclerView.V
private final LiveData<FirebaseDataSource> mDataSource;


//Data Source Observer
/*
LiveData created via Transformation do not have a value until an Observer is attached.
We attach this empty observer so that our getValue() calls return non-null later.
*/
private final Observer<FirebaseDataSource> mDataSourceObserver = new Observer<FirebaseDataSource>() {
@Override
public void onChanged(@Nullable FirebaseDataSource source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public abstract class FirestorePagingAdapter<T, VH extends RecyclerView.ViewHold
private final LiveData<LoadingState> mLoadingState;
private final LiveData<FirestoreDataSource> mDataSource;

/*
LiveData created via Transformation do not have a value until an Observer is attached.
We attach this empty observer so that our getValue() calls return non-null later.
*/
private final Observer<FirestoreDataSource> mDataSourceObserver = new Observer<FirestoreDataSource>() {
@Override
public void onChanged(@Nullable FirestoreDataSource source) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment inside here explaining why we need an empty observer so that future people who read this code know?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samtstern yes.
What comment should be added ?
Please suggest me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//Observer to hold the value for FirestoreDataSource ??

}
};

private final Observer<LoadingState> mStateObserver =
new Observer<LoadingState>() {
@Override
Expand Down Expand Up @@ -104,13 +115,26 @@ public void retry() {
source.retry();
}

/**
* To attempt to refresh the list. It will reload the list from beginning.
*/
public void refresh(){
FirestoreDataSource mFirebaseDataSource = mDataSource.getValue();
if (mFirebaseDataSource == null) {
Log.w(TAG, "Called refresh() when FirestoreDataSource is null!");
return;
}
mFirebaseDataSource.invalidate();
}

/**
* Start listening to paging / scrolling events and populating adapter data.
*/
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void startListening() {
mSnapshots.observeForever(mDataObserver);
mLoadingState.observeForever(mStateObserver);
mDataSource.observeForever(mDataSourceObserver);
}

/**
Expand All @@ -121,6 +145,7 @@ public void startListening() {
public void stopListening() {
mSnapshots.removeObserver(mDataObserver);
mLoadingState.removeObserver(mStateObserver);
mDataSource.removeObserver(mDataSourceObserver);
}

@Override
Expand Down