Skip to content

Commit 754f945

Browse files
committed
alpha7 update
Change-Id: I5da44eeb96a9acf4adf76e0ade70b9ae43a4308e
1 parent 4a684f3 commit 754f945

File tree

6 files changed

+43
-21
lines changed

6 files changed

+43
-21
lines changed

constants.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ project.ext {
1111
firebaseVersion = '11.8.0'
1212
firebaseVersion = '12.0.1'
1313
supportLibraryVersion = '27.1.0'
14-
architectureVersion = '1.1.0'
14+
architectureVersion = '1.1.1'
1515
kotlinVersion = '1.2.30'
1616
}

firestore/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ dependencies {
2323
annotationProcessor "android.arch.lifecycle:compiler:$architectureVersion"
2424

2525

26-
api "android.arch.paging:common:1.0.0-alpha6"
27-
api "android.arch.paging:runtime:1.0.0-alpha6"
26+
api "android.arch.paging:common:1.0.0-alpha7"
27+
api "android.arch.paging:runtime:1.0.0-alpha7"
2828

2929
androidTestImplementation 'junit:junit:4.12'
3030
androidTestImplementation 'com.android.support.test:runner:1.0.1'

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.arch.paging.DataSource;
44
import android.arch.paging.PageKeyedDataSource;
55
import android.support.annotation.NonNull;
6+
import android.support.annotation.Nullable;
7+
import android.support.annotation.RestrictTo;
68
import android.util.Log;
79

810
import com.google.android.gms.tasks.OnFailureListener;
@@ -11,12 +13,13 @@
1113
import com.google.firebase.firestore.Query;
1214
import com.google.firebase.firestore.QuerySnapshot;
1315

16+
import java.util.Collections;
1417
import java.util.List;
1518

1619
/**
17-
* Created by samstern on 3/15/18.
20+
* Data source to power a {@link FirestorePagingAdapter}.
1821
*/
19-
22+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
2023
public class FirestoreDataSource extends PageKeyedDataSource<PageKey, DocumentSnapshot> {
2124

2225
private static final String TAG = "FirestoreDataSource";
@@ -57,7 +60,13 @@ public void onSuccess(QuerySnapshot snapshots) {
5760
.addOnFailureListener(new OnFailureListener() {
5861
@Override
5962
public void onFailure(@NonNull Exception e) {
60-
// TODO: errors?
63+
Log.w(TAG, "loadInitial:failure", e);
64+
65+
// On error, return an empty page with the next page key being basically
66+
// equal to the initial query.
67+
PageKey nextPage = new PageKey(null, null);
68+
callback.onResult(Collections.<DocumentSnapshot>emptyList(),
69+
null, nextPage);
6170
}
6271
});
6372

@@ -74,7 +83,7 @@ public void loadBefore(@NonNull LoadParams<PageKey> params,
7483
@Override
7584
public void loadAfter(@NonNull LoadParams<PageKey> params,
7685
@NonNull final LoadCallback<PageKey, DocumentSnapshot> callback) {
77-
PageKey key = params.key;
86+
final PageKey key = params.key;
7887
Log.d(TAG, "loadAfter: " + key + ", " + params.requestedLoadSize);
7988

8089
key.getPageQuery(mBaseQuery, params.requestedLoadSize)
@@ -92,17 +101,22 @@ public void onSuccess(QuerySnapshot snapshots) {
92101
.addOnFailureListener(new OnFailureListener() {
93102
@Override
94103
public void onFailure(@NonNull Exception e) {
104+
Log.w(TAG, "loadAfter:failure", e);
95105

106+
// On error, return an empty page with the next page key being basically
107+
// equal to the initial query.
108+
callback.onResult(Collections.<DocumentSnapshot>emptyList(), key);
96109
}
97110
});
98111

99112
}
100113

114+
@Nullable
101115
private DocumentSnapshot getLast(List<DocumentSnapshot> data) {
102-
DocumentSnapshot last = (data == null || data.isEmpty())
103-
? null
104-
: data.get(data.size() - 1);
105-
106-
return last;
116+
if (data == null || data.isEmpty()) {
117+
return null;
118+
} else {
119+
return data.get(data.size() - 1);
120+
}
107121
}
108122
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.firebase.ui.firestore.paging;
22

3+
import android.arch.lifecycle.Lifecycle;
4+
import android.arch.lifecycle.LifecycleObserver;
35
import android.arch.lifecycle.LiveData;
46
import android.arch.lifecycle.Observer;
7+
import android.arch.lifecycle.OnLifecycleEvent;
58
import android.arch.paging.PagedList;
69
import android.arch.paging.PagedListAdapter;
710
import android.support.annotation.NonNull;
@@ -14,10 +17,13 @@
1417
import com.google.firebase.firestore.DocumentSnapshot;
1518

1619
/**
17-
* TODO(samstern): Document
20+
* Paginated RecyclerView Adapter for a Cloud Firestore query.
21+
*
22+
* Configured with {@link FirestorePagingOptions}.
1823
*/
1924
public abstract class FirestorePagingAdapter<T, VH extends RecyclerView.ViewHolder>
20-
extends PagedListAdapter<DocumentSnapshot, VH> {
25+
extends PagedListAdapter<DocumentSnapshot, VH>
26+
implements LifecycleObserver {
2127

2228
private final SnapshotParser<T> mParser;
2329
private final LiveData<PagedList<DocumentSnapshot>> mData;
@@ -62,14 +68,17 @@ public FirestorePagingAdapter(@NonNull FirestorePagingOptions<T> options) {
6268
mParser = options.getParser();
6369
mData = options.getData();
6470

65-
// TODO: Lifecycle owner
71+
if (options.getOwner() != null) {
72+
options.getOwner().getLifecycle().addObserver(this);
73+
}
6674
}
6775

68-
// TODO: Unify method names with other adapters
76+
@OnLifecycleEvent(Lifecycle.Event.ON_START)
6977
public void startListening() {
7078
mData.observeForever(mObserver);
7179
}
7280

81+
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
7382
public void stopListening() {
7483
mData.removeObserver(mObserver);
7584
}
@@ -84,7 +93,6 @@ public void onBindViewHolder(@NonNull VH holder, int position) {
8493
onBindViewHolder(holder, position, mParser.parseSnapshot(snapshot));
8594
}
8695

87-
// TODO: Check that this is the right visibility
8896
protected abstract void onBindViewHolder(@NonNull VH holder, int position, T model);
8997

9098
private void onListChanged(@NonNull PagedList<DocumentSnapshot> snapshots) {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
import com.google.firebase.firestore.Query;
1414

1515
/**
16-
* TODO: Document
16+
* Options to conifigure an {@link FirestorePagingAdapter}.
1717
*/
1818
public class FirestorePagingOptions<T> {
1919

20-
// TODO: Default config
21-
2220
private final LiveData<PagedList<DocumentSnapshot>> mData;
2321
private final SnapshotParser<T> mParser;
2422
private final LifecycleOwner mOwner;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.firebase.ui.firestore.paging;
22

33
import android.support.annotation.Nullable;
4+
import android.support.annotation.RestrictTo;
45

56
import com.google.firebase.firestore.DocumentSnapshot;
67
import com.google.firebase.firestore.Query;
78

89
/**
9-
* TODO: Document.
10+
* Key for Firestore pagination. Holds the DocumentSnapshot(s) that bound the page.
1011
*/
12+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
1113
public class PageKey {
1214

1315
private final DocumentSnapshot mStartAfter;

0 commit comments

Comments
 (0)