Skip to content

Commit 6af779c

Browse files
committed
Some cleanup
Change-Id: I4102d360594aeb75183f0ac8f1d300826de3b47f
1 parent 4d68a56 commit 6af779c

File tree

6 files changed

+149
-62
lines changed

6 files changed

+149
-62
lines changed

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

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package com.firebase.uidemo.database.firestore;
22

33
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
45
import android.support.v7.app.AppCompatActivity;
56
import android.support.v7.widget.LinearLayoutManager;
67
import android.support.v7.widget.RecyclerView;
8+
import android.util.Log;
79
import android.view.LayoutInflater;
10+
import android.view.Menu;
11+
import android.view.MenuItem;
812
import android.view.View;
913
import android.view.ViewGroup;
14+
import android.widget.ProgressBar;
1015
import android.widget.TextView;
16+
import android.widget.Toast;
1117

1218
import com.firebase.ui.firestore.FirestoreInfiniteScrollListener;
1319
import com.firebase.ui.firestore.FirestorePagingAdapter;
1420
import com.firebase.ui.firestore.FirestorePagingOptions;
1521
import com.firebase.uidemo.R;
22+
import com.google.android.gms.tasks.OnCompleteListener;
1623
import com.google.android.gms.tasks.Task;
1724
import com.google.firebase.firestore.CollectionReference;
1825
import com.google.firebase.firestore.FirebaseFirestore;
@@ -26,28 +33,41 @@
2633

2734
public class FirestorePagingActivity extends AppCompatActivity {
2835

36+
private static final String TAG = "FirestorePagingActivity";
37+
2938
@BindView(R.id.paging_recycler)
3039
RecyclerView mRecycler;
3140

41+
@BindView(R.id.paging_loading)
42+
ProgressBar mProgressBar;
43+
44+
private FirebaseFirestore mFirestore;
45+
3246
@Override
3347
protected void onCreate(Bundle savedInstanceState) {
3448
super.onCreate(savedInstanceState);
3549
setContentView(R.layout.activity_firestore_paging);
3650
ButterKnife.bind(this);
3751

52+
mFirestore = FirebaseFirestore.getInstance();
53+
mProgressBar.setIndeterminate(true);
54+
3855
setUpAdapter();
3956
}
4057

4158
private void setUpAdapter() {
42-
Query baseQuery = FirebaseFirestore.getInstance()
43-
.collection("items")
59+
Query baseQuery = mFirestore.collection("items")
4460
.orderBy("value", Query.Direction.ASCENDING);
4561

62+
// Paging options:
63+
// * pageSize - number of items to load in each 'page'.
64+
// * loadTriggerDistance - how far from the bottom/top of the data set to trigger a load.
65+
// * maxPages - maximum number of pages to keep in memory at a time.
4666
FirestorePagingOptions<Item> options = new FirestorePagingOptions.Builder<Item>()
4767
.setQuery(baseQuery, Item.class)
48-
.setPageSize(10)
49-
.setLoadTriggerDistance(5)
50-
.setMaxPages(3)
68+
.setPageSize(20)
69+
.setLoadTriggerDistance(10)
70+
.setMaxPages(5)
5171
.build();
5272

5373
FirestorePagingAdapter<Item, ItemViewHolder> adapter =
@@ -64,10 +84,22 @@ public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
6484
protected void onBindViewHolder(ItemViewHolder holder, int position, Item model) {
6585
holder.bind(model);
6686
}
87+
88+
@Override
89+
protected void onLoadingStateChanged(boolean isLoading) {
90+
super.onLoadingStateChanged(isLoading);
91+
if (isLoading) {
92+
mProgressBar.setVisibility(View.VISIBLE);
93+
} else {
94+
mProgressBar.setVisibility(View.INVISIBLE);
95+
}
96+
}
6797
};
6898

6999
LinearLayoutManager manager = new LinearLayoutManager(this);
70100

101+
// The infinite scroll listener will instruct the adapter to page up and down at the
102+
// appropriate times.
71103
FirestoreInfiniteScrollListener listener =
72104
new FirestoreInfiniteScrollListener(manager, adapter);
73105

@@ -76,11 +108,37 @@ protected void onBindViewHolder(ItemViewHolder holder, int position, Item model)
76108
mRecycler.addOnScrollListener(listener);
77109
}
78110

79-
// TODO: Bind this to a menu item
80-
public Task<Void> createItems() {
111+
@Override
112+
public boolean onCreateOptionsMenu(Menu menu) {
113+
getMenuInflater().inflate(R.menu.menu_firestore_paging, menu);
114+
return super.onCreateOptionsMenu(menu);
115+
}
116+
117+
@Override
118+
public boolean onOptionsItemSelected(MenuItem item) {
119+
if (item.getItemId() == R.id.item_add_data) {
120+
showToast("Adding data...");
121+
createItems().addOnCompleteListener(this, new OnCompleteListener<Void>() {
122+
@Override
123+
public void onComplete(@NonNull Task<Void> task) {
124+
if (task.isSuccessful()) {
125+
showToast("Data added.");
126+
} else {
127+
Log.w(TAG, "addData", task.getException());
128+
showToast("Error adding data.");
129+
}
130+
}
131+
});
81132

82-
WriteBatch writeBatch = FirebaseFirestore.getInstance().batch();
83-
CollectionReference collRef = FirebaseFirestore.getInstance().collection("items");
133+
return true;
134+
}
135+
return super.onOptionsItemSelected(item);
136+
}
137+
138+
private Task<Void> createItems() {
139+
140+
WriteBatch writeBatch = mFirestore.batch();
141+
CollectionReference collRef = mFirestore.collection("items");
84142

85143
for (int i = 0; i < 500; i++) {
86144
String title = "Item " + i;
@@ -94,6 +152,10 @@ public Task<Void> createItems() {
94152
return writeBatch.commit();
95153
}
96154

155+
private void showToast(String message) {
156+
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
157+
}
158+
97159
public static class Item {
98160

99161
public String text;

app/src/main/res/layout/activity_firestore_paging.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@
66
android:layout_height="match_parent"
77
tools:context="com.firebase.uidemo.database.firestore.FirestorePagingActivity">
88

9+
<ProgressBar
10+
android:id="@+id/paging_loading"
11+
style="?android:attr/progressBarStyleHorizontal"
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content"
14+
android:layout_marginTop="-6dp"
15+
android:background="@android:color/transparent" />
16+
917
<android.support.v7.widget.RecyclerView
1018
android:id="@+id/paging_recycler"
1119
android:layout_width="match_parent"
1220
android:layout_height="wrap_content"
13-
tools:listitem="@layout/item_item"
21+
android:layout_below="@+id/paging_loading"
1422
android:paddingLeft="16dp"
1523
android:paddingRight="16dp"
1624
android:paddingTop="8dp"
17-
android:clipToPadding="false" />
25+
android:clipToPadding="false"
26+
tools:listitem="@layout/item_item" />
1827

1928
</RelativeLayout>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto">
5+
6+
<item
7+
android:id="@+id/item_add_data"
8+
android:title="@string/menu_add_data"
9+
app:showAsAction="never" />
10+
</menu>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,5 @@
101101
Make sure your device is online and that Anonymous Auth is configured in your Firebase project
102102
(https://console.firebase.google.com/project/_/authentication/providers)
103103
</string>
104+
<string name="menu_add_data">Add Data</string>
104105
</resources>

firestore/src/main/java/com/firebase/ui/firestore/FirestoreInfiniteScrollListener.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import android.support.v7.widget.LinearLayoutManager;
55
import android.support.v7.widget.RecyclerView;
66

7+
/**
8+
* TODO(samstern): Document
9+
*/
710
public class FirestoreInfiniteScrollListener extends RecyclerView.OnScrollListener {
811

912
private final LinearLayoutManager mManager;
@@ -42,12 +45,6 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
4245
public void run() {
4346
// Load one page down
4447
mAdapter.loadNextPage();
45-
46-
// Unload top page.
47-
// TODO: This should probably be inside the paging adapter
48-
if (mAdapter.getPagesLoaded() > mAdapter.getOptions().getMaxPages() ) {
49-
mAdapter.unloadTopPage();
50-
}
5148
}
5249
});
5350

@@ -57,11 +54,6 @@ public void run() {
5754
public void run() {
5855
// Load one page up
5956
mAdapter.loadPrevPage();
60-
61-
// Unload bottom page
62-
if (mAdapter.getPagesLoaded() > mAdapter.getOptions().getMaxPages()) {
63-
mAdapter.unloadBottomPage();
64-
}
6557
}
6658
});
6759
}

0 commit comments

Comments
 (0)