-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Feature - Realtime Database Pagination #1603
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
samtstern
merged 5 commits into
firebase:version-4.4.0-dev
from
PatilShreyas:version-4.3.2-dev
Mar 26, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f479b6f
Added support for Firebase Database Pagination as #1601
PatilShreyas 460b726
Added features as suggested in #1603 and issue 1601
PatilShreyas ba4fdae
Minor changes in code #1603 issue #1601
PatilShreyas c090b91
Added 'retry()' and 'refresh()' methods PR #1603 issue #1601
PatilShreyas 20e9276
Minor Fix #1603 issue #1601
PatilShreyas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
app/src/main/java/com/firebase/uidemo/database/realtime/FirebaseDbPagingActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package com.firebase.uidemo.database.realtime; | ||
|
||
import android.arch.paging.PagedList; | ||
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; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.firebase.ui.database.paging.DatabasePagingOptions; | ||
import com.firebase.ui.database.paging.FirebaseRecyclerPagingAdapter; | ||
import com.firebase.ui.database.paging.LoadingState; | ||
import com.firebase.uidemo.R; | ||
import com.google.firebase.database.DatabaseError; | ||
import com.google.firebase.database.FirebaseDatabase; | ||
import com.google.firebase.database.Query; | ||
|
||
import butterknife.BindView; | ||
import butterknife.ButterKnife; | ||
|
||
public class FirebaseDbPagingActivity extends AppCompatActivity { | ||
|
||
private final String TAG = "PagingActivity"; | ||
|
||
@BindView(R.id.paging_recycler) | ||
RecyclerView mRecycler; | ||
|
||
@BindView(R.id.swipe_refresh_layout) | ||
SwipeRefreshLayout mSwipeRefreshLayout; | ||
|
||
private Query mQuery; | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_database_paging); | ||
ButterKnife.bind(this); | ||
|
||
mQuery = FirebaseDatabase.getInstance().getReference().child("posts"); | ||
|
||
setUpAdapter(); | ||
} | ||
|
||
private void setUpAdapter() { | ||
|
||
//Initialize Paging Configurations | ||
PagedList.Config config = new PagedList.Config.Builder() | ||
.setEnablePlaceholders(false) | ||
.setPrefetchDistance(5) | ||
.setPageSize(30) | ||
.build(); | ||
|
||
//Initialize Firebase Paging Options | ||
DatabasePagingOptions<Post> options = new DatabasePagingOptions.Builder<Post>() | ||
.setLifecycleOwner(this) | ||
.setQuery(mQuery, config, Post.class) | ||
.build(); | ||
|
||
//Initializing Adapter | ||
final FirebaseRecyclerPagingAdapter<Post, PostViewHolder> mAdapter = | ||
new FirebaseRecyclerPagingAdapter<Post, PostViewHolder>(options) { | ||
@NonNull | ||
@Override | ||
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, | ||
int viewType) { | ||
View view = LayoutInflater.from(parent.getContext()) | ||
.inflate(R.layout.item_post, parent, false); | ||
return new PostViewHolder(view); | ||
} | ||
|
||
@Override | ||
protected void onBindViewHolder(@NonNull PostViewHolder holder, | ||
int position, | ||
@NonNull Post model) { | ||
holder.bind(model); | ||
} | ||
|
||
@Override | ||
protected void onLoadingStateChanged(@NonNull LoadingState state) { | ||
switch (state) { | ||
case LOADING_INITIAL: | ||
case LOADING_MORE: | ||
mSwipeRefreshLayout.setRefreshing(true); | ||
break; | ||
|
||
case LOADED: | ||
mSwipeRefreshLayout.setRefreshing(false); | ||
break; | ||
|
||
case FINISHED: | ||
mSwipeRefreshLayout.setRefreshing(false); | ||
Toast.makeText(getApplicationContext(), getString(R.string.paging_finished_message), Toast.LENGTH_SHORT).show(); | ||
break; | ||
|
||
case ERROR: | ||
retry(); | ||
break; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onError(DatabaseError databaseError) { | ||
mSwipeRefreshLayout.setRefreshing(false); | ||
Log.e(TAG, databaseError.getMessage()); | ||
} | ||
}; | ||
|
||
mRecycler.setLayoutManager(new LinearLayoutManager(this)); | ||
mRecycler.setAdapter(mAdapter); | ||
|
||
// Reload data on swipe | ||
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | ||
@Override | ||
public void onRefresh() { | ||
//Reload Data | ||
mAdapter.refresh(); | ||
} | ||
}); | ||
} | ||
|
||
public static class Post { | ||
|
||
@Nullable public String title; | ||
@Nullable public String body; | ||
|
||
public Post(){} | ||
|
||
public Post(@Nullable String title, @Nullable String body) { | ||
this.title = title; | ||
this.body = body; | ||
} | ||
} | ||
|
||
public static class PostViewHolder extends RecyclerView.ViewHolder { | ||
|
||
@BindView(R.id.textViewTitle) | ||
TextView mTitleView; | ||
|
||
@BindView(R.id.textViewBody) | ||
TextView mBodyView; | ||
|
||
PostViewHolder(@NonNull View itemView) { | ||
super(itemView); | ||
ButterKnife.bind(this, itemView); | ||
} | ||
|
||
void bind(@NonNull Post post) { | ||
mTitleView.setText(post.title); | ||
mBodyView.setText(post.body); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<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.realtime.FirebaseDbPagingActivity"> | ||
|
||
<android.support.v7.widget.RecyclerView | ||
android:id="@+id/paging_recycler" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_below="@+id/paging_loading" | ||
android:clipToPadding="false" | ||
android:paddingLeft="16dp" | ||
android:paddingTop="8dp" | ||
android:paddingRight="16dp" | ||
tools:listitem="@layout/item_post" /> | ||
|
||
</android.support.v4.widget.SwipeRefreshLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:orientation="vertical" | ||
android:layout_height="wrap_content"> | ||
|
||
<android.support.v7.widget.CardView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="4dp" | ||
android:layout_marginTop="2dp" | ||
android:layout_marginRight="4dp" | ||
android:layout_marginBottom="2dp" | ||
android:clickable="true" | ||
android:foreground="?android:selectableItemBackground" | ||
app:cardElevation="2dp" | ||
android:focusable="true"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
android:padding="8dp"> | ||
|
||
<TextView | ||
android:id="@+id/textViewTitle" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/post_text_title" | ||
android:textColor="#000000" | ||
android:textSize="18sp" | ||
android:textStyle="bold" /> | ||
|
||
<TextView | ||
android:id="@+id/textViewBody" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="4dp" | ||
android:text="@string/post_text_body" | ||
android:textColor="#000000" | ||
android:textSize="15sp" /> | ||
</LinearLayout> | ||
</android.support.v7.widget.CardView> | ||
|
||
|
||
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-dontwarn com.firebase.ui.database.paging.** |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.