Skip to content

Commit c8f0d4a

Browse files
committed
Add more constructors
Change-Id: I5778ad28795bb97a56ff04aa70f53e3d553bbefb
1 parent 74c954f commit c8f0d4a

File tree

6 files changed

+103
-11
lines changed

6 files changed

+103
-11
lines changed

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ dependencies {
4848
compile('com.facebook.android:facebook-android-sdk:4.25.0')
4949
compile("com.twitter.sdk.android:twitter-core:3.0.0@aar") { transitive = true }
5050

51+
compile "android.arch.lifecycle:runtime:$architectureVersion"
52+
compile "android.arch.lifecycle:extensions:$architectureVersion"
53+
5154
// The following dependencies are not required to use the Firebase UI library.
5255
// They are used to make some aspects of the demo app implementation simpler for
5356
// demonstrative purposes, and you may find them useful in your own apps; YMMV.

common/src/main/java/com/firebase/ui/common/BaseCachingSnapshotParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public abstract class BaseCachingSnapshotParser<S, T> implements BaseSnapshotPar
1515
private Map<String, T> mObjectCache = new HashMap<>();
1616
private BaseSnapshotParser<S, T> mInnerParser;
1717

18-
// TODO(samstern): Can probably use this in RTDB module as well, CachingOSA is not necessary.
1918
public BaseCachingSnapshotParser(BaseSnapshotParser<S, T> innerParser) {
2019
mInnerParser = innerParser;
2120
}

constants.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ project.ext {
1212

1313
firebaseVersion = '11.0.8'
1414
supportLibraryVersion = '26.0.1'
15+
architectureVersion = '1.0.0-alpha8'
1516
}

database/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ dependencies {
2929
// Needed to override play services
3030
compile "com.android.support:support-v4:$supportLibraryVersion"
3131

32-
compile 'android.arch.lifecycle:runtime:1.0.0-alpha8'
33-
compile 'android.arch.lifecycle:extensions:1.0.0-alpha8'
34-
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-alpha8'
32+
compile "android.arch.lifecycle:runtime:$architectureVersion"
33+
compile "android.arch.lifecycle:extensions:$architectureVersion"
34+
annotationProcessor "android.arch.lifecycle:compiler:$architectureVersion"
3535

3636
compile "com.google.firebase:firebase-database:$firebaseVersion"
3737

firestore/build.gradle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ dependencies {
3131

3232
compile "com.google.firebase:firebase-firestore:$firebaseVersion"
3333

34+
compile "android.arch.lifecycle:runtime:$architectureVersion"
35+
compile "android.arch.lifecycle:extensions:$architectureVersion"
36+
annotationProcessor "android.arch.lifecycle:compiler:$architectureVersion"
37+
3438
androidTestCompile 'junit:junit:4.12'
35-
androidTestCompile 'com.android.support.test:runner:0.5'
36-
androidTestCompile 'com.android.support.test:rules:0.5'
39+
androidTestCompile 'com.android.support.test:runner:1.0.0'
40+
androidTestCompile 'com.android.support.test:rules:1.0.0'
3741
}

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

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.firebase.ui.firestore;
22

3+
import android.arch.lifecycle.Lifecycle;
4+
import android.arch.lifecycle.LifecycleObserver;
5+
import android.arch.lifecycle.LifecycleOwner;
6+
import android.arch.lifecycle.OnLifecycleEvent;
7+
import android.support.annotation.Nullable;
38
import android.support.v7.widget.RecyclerView;
9+
import android.util.Log;
410

511
import com.firebase.ui.common.ChangeEventType;
612
import com.google.firebase.firestore.DocumentSnapshot;
@@ -14,22 +20,102 @@
1420
* @param <VH> viewholder class.
1521
*/
1622
public abstract class FirestoreRecyclerAdapter<T, VH extends RecyclerView.ViewHolder>
17-
extends RecyclerView.Adapter<VH> implements ChangeEventListener {
23+
extends RecyclerView.Adapter<VH>
24+
implements ChangeEventListener, LifecycleObserver {
25+
26+
private static final String TAG = "FirestoreRecycler";
1827

1928
private ObservableSnapshotArray<T> mArray;
2029

30+
/**
31+
* Create a new RecyclerView adapter to bind data from a Firestore query where each
32+
* {@link DocumentSnapshot} is converted to the specified model class.
33+
*
34+
* See {@link #FirestoreRecyclerAdapter(ObservableSnapshotArray, LifecycleOwner)}.
35+
*
36+
* @param query the Firestore query.
37+
* @param modelClass the model class.
38+
*/
2139
public FirestoreRecyclerAdapter(Query query, Class<T> modelClass) {
40+
this(query, modelClass, null);
41+
}
42+
43+
/**
44+
* Create a new RecyclerView adapter bound to a LifecycleOwner.
45+
*
46+
* See {@link #FirestoreRecyclerAdapter(Query, Class)}
47+
*/
48+
public FirestoreRecyclerAdapter(Query query, Class<T> modelClass, LifecycleOwner owner) {
2249
mArray = new FirestoreArray<>(query, modelClass);
50+
if (owner != null) {
51+
owner.getLifecycle().addObserver(this);
52+
}
2353
}
2454

25-
public abstract void onBindViewHolder(VH vh, int i, T model);
55+
/**
56+
* Create a new RecyclerView adapter to bind data from a Firestore query where each
57+
* {@link DocumentSnapshot} is parsed by the specified parser.
58+
*
59+
* See {@link #FirestoreRecyclerAdapter(ObservableSnapshotArray, LifecycleOwner)}.
60+
*
61+
* @param query the Firestore query.
62+
* @param parser the snapshot parser.
63+
*/
64+
public FirestoreRecyclerAdapter(Query query, SnapshotParser<T> parser) {
65+
this(query, parser, null);
66+
}
67+
68+
/**
69+
* Create a new RecyclerView adapter bound to a LifecycleOwner.
70+
*
71+
* See {@link #FirestoreRecyclerAdapter(Query, SnapshotParser)}.
72+
*/
73+
public FirestoreRecyclerAdapter(Query query, SnapshotParser<T> parser, LifecycleOwner owner) {
74+
mArray = new FirestoreArray<T>(query, parser);
75+
if (owner != null) {
76+
owner.getLifecycle().addObserver(this);
77+
}
78+
}
2679

80+
/**
81+
* Create a new RecyclerView adapter to bind data from an {@link ObservableSnapshotArray}.
82+
*
83+
* @param array the observable array of data from Firestore.
84+
* @param owner (optional) a LifecycleOwner to observe.
85+
*/
86+
public FirestoreRecyclerAdapter(ObservableSnapshotArray<T> array,
87+
@Nullable LifecycleOwner owner) {
88+
mArray = array;
89+
if (owner != null) {
90+
owner.getLifecycle().addObserver(this);
91+
}
92+
}
93+
94+
/**
95+
* Called when data has been added/changed and an item needs to be displayed.
96+
*
97+
* @param vh the view to populate.
98+
* @param i the position in the list of the view being populated.
99+
* @param model the model object containing the data that should be used to populate the view.
100+
*/
101+
protected abstract void onBindViewHolder(VH vh, int i, T model);
102+
103+
@OnLifecycleEvent(Lifecycle.Event.ON_START)
27104
public void startListening() {
28-
mArray.addChangeEventListener(this);
105+
if (!mArray.isListening(this)) {
106+
mArray.addChangeEventListener(this);
107+
}
29108
}
30109

110+
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
31111
public void stopListening() {
32112
mArray.removeChangeEventListener(this);
113+
notifyDataSetChanged();
114+
}
115+
116+
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
117+
void cleanup(LifecycleOwner source) {
118+
source.getLifecycle().removeObserver(this);
33119
}
34120

35121
@Override
@@ -64,11 +150,10 @@ public void onChildChanged(ChangeEventType type, DocumentSnapshot snapshot,
64150

65151
@Override
66152
public void onDataChanged() {
67-
// No-op
68153
}
69154

70155
@Override
71156
public void onError(FirebaseFirestoreException e) {
72-
// No-op
157+
Log.w(TAG, "onError", e);
73158
}
74159
}

0 commit comments

Comments
 (0)