|
1 | 1 | package com.firebase.ui.firestore;
|
2 | 2 |
|
| 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; |
3 | 8 | import android.support.v7.widget.RecyclerView;
|
| 9 | +import android.util.Log; |
4 | 10 |
|
5 | 11 | import com.firebase.ui.common.ChangeEventType;
|
6 | 12 | import com.google.firebase.firestore.DocumentSnapshot;
|
|
14 | 20 | * @param <VH> viewholder class.
|
15 | 21 | */
|
16 | 22 | 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"; |
18 | 27 |
|
19 | 28 | private ObservableSnapshotArray<T> mArray;
|
20 | 29 |
|
| 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 | + */ |
21 | 39 | 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) { |
22 | 49 | mArray = new FirestoreArray<>(query, modelClass);
|
| 50 | + if (owner != null) { |
| 51 | + owner.getLifecycle().addObserver(this); |
| 52 | + } |
23 | 53 | }
|
24 | 54 |
|
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 | + } |
26 | 79 |
|
| 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) |
27 | 104 | public void startListening() {
|
28 |
| - mArray.addChangeEventListener(this); |
| 105 | + if (!mArray.isListening(this)) { |
| 106 | + mArray.addChangeEventListener(this); |
| 107 | + } |
29 | 108 | }
|
30 | 109 |
|
| 110 | + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) |
31 | 111 | public void stopListening() {
|
32 | 112 | mArray.removeChangeEventListener(this);
|
| 113 | + notifyDataSetChanged(); |
| 114 | + } |
| 115 | + |
| 116 | + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) |
| 117 | + void cleanup(LifecycleOwner source) { |
| 118 | + source.getLifecycle().removeObserver(this); |
33 | 119 | }
|
34 | 120 |
|
35 | 121 | @Override
|
@@ -64,11 +150,10 @@ public void onChildChanged(ChangeEventType type, DocumentSnapshot snapshot,
|
64 | 150 |
|
65 | 151 | @Override
|
66 | 152 | public void onDataChanged() {
|
67 |
| - // No-op |
68 | 153 | }
|
69 | 154 |
|
70 | 155 | @Override
|
71 | 156 | public void onError(FirebaseFirestoreException e) {
|
72 |
| - // No-op |
| 157 | + Log.w(TAG, "onError", e); |
73 | 158 | }
|
74 | 159 | }
|
0 commit comments