Skip to content

Commit e71ddc4

Browse files
SUPERCILEXsamtstern
authored andcommitted
Make FirebaseRecyclerAdapter use RecyclerView naming scheme and methods (#12)
1 parent 57d0de5 commit e71ddc4

File tree

6 files changed

+83
-163
lines changed

6 files changed

+83
-163
lines changed

app/src/main/java/com/firebase/uidemo/database/realtime/ChatActivity.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import android.support.v7.widget.LinearLayoutManager;
2323
import android.support.v7.widget.RecyclerView;
2424
import android.util.Log;
25+
import android.view.LayoutInflater;
2526
import android.view.View;
27+
import android.view.ViewGroup;
2628
import android.widget.Button;
2729
import android.widget.EditText;
2830
import android.widget.TextView;
@@ -142,18 +144,22 @@ public void onItemRangeInserted(int positionStart, int itemCount) {
142144
protected FirebaseRecyclerAdapter<Chat, ChatHolder> getAdapter() {
143145
Query lastFifty = mChatRef.limitToLast(50);
144146

145-
FirebaseRecyclerOptions<Chat, ChatHolder> options =
146-
new FirebaseRecyclerOptions.Builder<Chat, ChatHolder>()
147-
.setViewHolder(R.layout.message, ChatHolder.class)
147+
FirebaseRecyclerOptions<Chat> options =
148+
new FirebaseRecyclerOptions.Builder<Chat>()
148149
.setQuery(lastFifty, Chat.class)
149150
.setLifecycleOwner(this)
150151
.build();
151152

152153
return new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
154+
@Override
155+
public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
156+
return new ChatHolder(LayoutInflater.from(parent.getContext())
157+
.inflate(R.layout.message, parent, false));
158+
}
153159

154160
@Override
155-
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
156-
holder.bind(chat);
161+
protected void onBindViewHolder(ChatHolder holder, int position, Chat model) {
162+
holder.bind(model);
157163
}
158164

159165
@Override

app/src/main/java/com/firebase/uidemo/database/realtime/ChatIndexActivity.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.firebase.uidemo.database.realtime;
22

3+
import android.view.LayoutInflater;
34
import android.view.View;
5+
import android.view.ViewGroup;
46

57
import com.firebase.ui.database.FirebaseRecyclerAdapter;
68
import com.firebase.ui.database.FirebaseRecyclerOptions;
@@ -33,17 +35,22 @@ protected FirebaseRecyclerAdapter<Chat, ChatHolder> getAdapter() {
3335
.child("chatIndices")
3436
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
3537

36-
FirebaseRecyclerOptions<Chat, ChatHolder> options =
37-
new FirebaseRecyclerOptions.Builder<Chat, ChatHolder>()
38+
FirebaseRecyclerOptions<Chat> options =
39+
new FirebaseRecyclerOptions.Builder<Chat>()
3840
.setIndexedQuery(mChatIndicesRef.limitToFirst(50), mChatRef, Chat.class)
39-
.setViewHolder(R.layout.message, ChatHolder.class)
4041
.setLifecycleOwner(this)
4142
.build();
4243

4344
return new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
4445
@Override
45-
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
46-
holder.bind(chat);
46+
public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
47+
return new ChatHolder(LayoutInflater.from(parent.getContext())
48+
.inflate(R.layout.message, parent, false));
49+
}
50+
51+
@Override
52+
protected void onBindViewHolder(ChatHolder holder, int position, Chat model) {
53+
holder.bind(model);
4754
}
4855

4956
@Override

database/src/main/java/com/firebase/ui/database/FirebaseRecyclerAdapter.java

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@
55
import android.arch.lifecycle.OnLifecycleEvent;
66
import android.support.v7.widget.RecyclerView;
77
import android.util.Log;
8-
import android.view.LayoutInflater;
9-
import android.view.View;
10-
import android.view.ViewGroup;
118

129
import com.firebase.ui.common.ChangeEventType;
1310
import com.google.firebase.database.DataSnapshot;
1411
import com.google.firebase.database.DatabaseError;
1512
import com.google.firebase.database.DatabaseReference;
1613

17-
import java.lang.reflect.Constructor;
18-
import java.lang.reflect.InvocationTargetException;
19-
2014
/**
2115
* This class is a generic way of backing a {@link RecyclerView} with a Firebase location. It
2216
* handles all of the child events at the given Firebase location and marshals received data into
@@ -34,17 +28,13 @@ public abstract class FirebaseRecyclerAdapter<T, VH extends RecyclerView.ViewHol
3428
private static final String TAG = "FirebaseRecyclerAdapter";
3529

3630
protected final ObservableSnapshotArray<T> mSnapshots;
37-
protected final Class<VH> mViewHolderClass;
38-
protected final int mModelLayout;
3931

4032
/**
4133
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
4234
* {@link FirebaseRecyclerOptions} for configuration options.
4335
*/
44-
public FirebaseRecyclerAdapter(FirebaseRecyclerOptions<T, VH> options) {
36+
public FirebaseRecyclerAdapter(FirebaseRecyclerOptions<T> options) {
4537
mSnapshots = options.getSnapshots();
46-
mViewHolderClass = options.getViewHolderClass();
47-
mModelLayout = options.getModelLayout();
4838

4939
if (options.getOwner() != null) {
5040
options.getOwner().getLifecycle().addObserver(this);
@@ -123,44 +113,13 @@ public int getItemCount() {
123113
}
124114

125115
@Override
126-
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
127-
View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
128-
try {
129-
Constructor<VH> constructor = mViewHolderClass.getConstructor(View.class);
130-
return constructor.newInstance(view);
131-
} catch (NoSuchMethodException e) {
132-
throw new RuntimeException(e);
133-
} catch (InvocationTargetException e) {
134-
throw new RuntimeException(e);
135-
} catch (InstantiationException e) {
136-
throw new RuntimeException(e);
137-
} catch (IllegalAccessException e) {
138-
throw new RuntimeException(e);
139-
}
140-
}
141-
142-
@Override
143-
public int getItemViewType(int position) {
144-
return mModelLayout;
145-
}
146-
147-
@Override
148-
public void onBindViewHolder(VH viewHolder, int position) {
149-
T model = getItem(position);
150-
populateViewHolder(viewHolder, model, position);
116+
public void onBindViewHolder(VH holder, int position) {
117+
onBindViewHolder(holder, position, getItem(position));
151118
}
152119

153120
/**
154-
* Each time the data at the given Firebase location changes, this method will be called for
155-
* each item that needs to be displayed. The first two arguments correspond to the mLayout and
156-
* mModelClass given to the constructor of this class. The third argument is the item's position
157-
* in the list.
158-
* <p>
159-
* Your implementation should populate the view using the data contained in the model.
160-
*
161-
* @param viewHolder The view to populate
162-
* @param model The object containing the data used to populate the view
163-
* @param position The position in the list of the view being populated
121+
* @param model the model object containing the data that should be used to populate the view.
122+
* @see #onBindViewHolder(RecyclerView.ViewHolder, int)
164123
*/
165-
protected abstract void populateViewHolder(VH viewHolder, T model, int position);
124+
protected abstract void onBindViewHolder(VH holder, int position, T model);
166125
}

database/src/main/java/com/firebase/ui/database/FirebaseRecyclerOptions.java

Lines changed: 37 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.firebase.ui.database;
22

33
import android.arch.lifecycle.LifecycleOwner;
4-
import android.support.annotation.LayoutRes;
54
import android.support.annotation.Nullable;
6-
import android.support.v7.widget.RecyclerView;
75

86
import com.google.firebase.database.DatabaseReference;
97
import com.google.firebase.database.Query;
@@ -16,29 +14,19 @@
1614
*
1715
* @see Builder
1816
*/
19-
public class FirebaseRecyclerOptions<T, VH extends RecyclerView.ViewHolder> {
17+
public class FirebaseRecyclerOptions<T> {
2018

2119
private static final String ERR_SNAPSHOTS_SET = "Snapshot array already set. " +
2220
"Call only one of setSnapshotArray, setQuery, or setIndexedQuery.";
23-
24-
private static final String ERR_SNAPSHOTS_NULL = "Snapshot array cannot be null. " +
21+
private static final String ERR_SNAPSHOTS_NULL = "Snapshot array cannot be null. " +
2522
"Call one of setSnapshotArray, setQuery, or setIndexedQuery.";
2623

27-
private static final String ERR_VIEWHOLDER_NULL = "View holder class cannot be null. " +
28-
"Call setViewHolder.";
29-
3024
private final ObservableSnapshotArray<T> mSnapshots;
31-
private final Class<VH> mViewHolderClass;
32-
private final int mModelLayout;
3325
private final LifecycleOwner mOwner;
3426

3527
private FirebaseRecyclerOptions(ObservableSnapshotArray<T> snapshots,
36-
Class<VH> viewHolderClass,
37-
@LayoutRes int modelLayout,
38-
LifecycleOwner owner) {
28+
@Nullable LifecycleOwner owner) {
3929
mSnapshots = snapshots;
40-
mViewHolderClass = viewHolderClass;
41-
mModelLayout = modelLayout;
4230
mOwner = owner;
4331
}
4432

@@ -50,24 +38,8 @@ public ObservableSnapshotArray<T> getSnapshots() {
5038
}
5139

5240
/**
53-
* Get the class of the {@link android.support.v7.widget.RecyclerView.ViewHolder} for
54-
* each RecyclerView item.
55-
*/
56-
public Class<VH> getViewHolderClass() {
57-
return mViewHolderClass;
58-
}
59-
60-
/**
61-
* Get the resource ID of the layout for each RecyclerView item.
62-
*/
63-
@LayoutRes
64-
public int getModelLayout() {
65-
return mModelLayout;
66-
}
67-
68-
/**
69-
* Get the (optional) LifecycleOwner. Listening will start/stop after the appropriate
70-
* lifecycle events.
41+
* Get the (optional) LifecycleOwner. Listening will start/stop after the appropriate lifecycle
42+
* events.
7143
*/
7244
@Nullable
7345
public LifecycleOwner getOwner() {
@@ -78,109 +50,93 @@ public LifecycleOwner getOwner() {
7850
* Builder for a {@link FirebaseRecyclerOptions}.
7951
*
8052
* @param <T> the model class for the {@link FirebaseRecyclerAdapter}.
81-
* @param <VH> the ViewHolder class for the {@link FirebaseRecyclerAdapter}.
8253
*/
83-
public static class Builder<T, VH extends RecyclerView.ViewHolder> {
54+
public static class Builder<T> {
8455

8556
private ObservableSnapshotArray<T> mSnapshots;
86-
private Class<VH> mViewHolderClass;
87-
private int mModelLayout;
8857
private LifecycleOwner mOwner;
8958

9059
/**
9160
* Directly set the {@link ObservableSnapshotArray} to be listened to.
92-
*
61+
* <p>
9362
* Do not call this method after calling {@code setQuery}.
9463
*/
95-
public Builder<T, VH> setSnapshotArray(ObservableSnapshotArray<T> snapshots) {
64+
public Builder<T> setSnapshotArray(ObservableSnapshotArray<T> snapshots) {
9665
assertNull(mSnapshots, ERR_SNAPSHOTS_SET);
9766

9867
mSnapshots = snapshots;
9968
return this;
10069
}
10170

10271
/**
103-
* Set the Firebase query to listen to, along with a {@link SnapshotParser} to
104-
* parse snapshots into model objects.
105-
*
72+
* Set the Firebase query to listen to, along with a {@link SnapshotParser} to parse
73+
* snapshots into model objects.
74+
* <p>
10675
* Do not call this method after calling {@link #setSnapshotArray(ObservableSnapshotArray)}.
10776
*/
108-
public Builder<T, VH> setQuery(Query query, SnapshotParser<T> snapshotParser) {
77+
public Builder<T> setQuery(Query query, SnapshotParser<T> snapshotParser) {
10978
assertNull(mSnapshots, ERR_SNAPSHOTS_SET);
11079

111-
mSnapshots = new FirebaseArray<T>(query, snapshotParser);
80+
mSnapshots = new FirebaseArray<>(query, snapshotParser);
11281
return this;
11382
}
11483

11584
/**
116-
* Set the Firebase query to listen to, along with a {@link Class} to which snapshots
117-
* should be parsed.
118-
*
85+
* Set the Firebase query to listen to, along with a {@link Class} to which snapshots should
86+
* be parsed.
87+
* <p>
11988
* Do not call this method after calling {@link #setSnapshotArray(ObservableSnapshotArray)}.
12089
*/
121-
public Builder<T, VH> setQuery(Query query, Class<T> modelClass) {
122-
return setQuery(query, new ClassSnapshotParser<T>(modelClass));
90+
public Builder<T> setQuery(Query query, Class<T> modelClass) {
91+
return setQuery(query, new ClassSnapshotParser<>(modelClass));
12392
}
12493

12594

12695
/**
127-
* Set an indexed Firebase query to listen to, along with a {@link SnapshotParser} to
128-
* parse snapshots into model objects. Keys are identified by the {@code keyQuery} and then
129-
* data is fetched using those keys from the {@code dataRef}.
130-
*
96+
* Set an indexed Firebase query to listen to, along with a {@link SnapshotParser} to parse
97+
* snapshots into model objects. Keys are identified by the {@code keyQuery} and then data
98+
* is fetched using those keys from the {@code dataRef}.
99+
* <p>
131100
* Do not call this method after calling {@link #setSnapshotArray(ObservableSnapshotArray)}.
132101
*/
133-
public Builder<T, VH> setIndexedQuery(Query keyQuery,
134-
DatabaseReference dataRef,
135-
SnapshotParser<T> snapshotParser) {
102+
public Builder<T> setIndexedQuery(Query keyQuery,
103+
DatabaseReference dataRef,
104+
SnapshotParser<T> snapshotParser) {
136105
assertNull(mSnapshots, ERR_SNAPSHOTS_SET);
137106

138-
mSnapshots = new FirebaseIndexArray<T>(keyQuery, dataRef, snapshotParser);
107+
mSnapshots = new FirebaseIndexArray<>(keyQuery, dataRef, snapshotParser);
139108
return this;
140109
}
141110

142111
/**
143-
* Set an indexed Firebase query to listen to, along with a {@link Class} to which
144-
* snapshots should be parsed. Keys are identified by the {@code keyQuery} and then
145-
* data is fetched using those keys from the {@code dataRef}.
146-
*
112+
* Set an indexed Firebase query to listen to, along with a {@link Class} to which snapshots
113+
* should be parsed. Keys are identified by the {@code keyQuery} and then data is fetched
114+
* using those keys from the {@code dataRef}.
115+
* <p>
147116
* Do not call this method after calling {@link #setSnapshotArray(ObservableSnapshotArray)}.
148117
*/
149-
public Builder<T, VH> setIndexedQuery(Query keyQuery,
150-
DatabaseReference dataRef,
151-
Class<T> modelClass) {
152-
return setIndexedQuery(keyQuery, dataRef, new ClassSnapshotParser<T>(modelClass));
153-
}
154-
155-
/**
156-
* Set the layout resource ID and class for the
157-
* {@link android.support.v7.widget.RecyclerView.ViewHolder} for each RecyclerView item.
158-
*/
159-
public Builder<T, VH> setViewHolder(@LayoutRes int modelLayout, Class<VH> viewHolderClass) {
160-
mModelLayout = modelLayout;
161-
mViewHolderClass = viewHolderClass;
162-
163-
return this;
118+
public Builder<T> setIndexedQuery(Query keyQuery,
119+
DatabaseReference dataRef,
120+
Class<T> modelClass) {
121+
return setIndexedQuery(keyQuery, dataRef, new ClassSnapshotParser<>(modelClass));
164122
}
165123

166124
/**
167125
* Set the (optional) {@link LifecycleOwner}. Listens will start and stop after the
168126
* appropriate lifecycle events.
169127
*/
170-
public Builder<T, VH> setLifecycleOwner(LifecycleOwner owner) {
128+
public Builder<T> setLifecycleOwner(LifecycleOwner owner) {
171129
mOwner = owner;
172130
return this;
173131
}
174132

175133
/**
176134
* Build a {@link FirebaseRecyclerOptions} from the provided arguments.
177135
*/
178-
public FirebaseRecyclerOptions<T, VH> build() {
136+
public FirebaseRecyclerOptions<T> build() {
179137
assertNonNull(mSnapshots, ERR_SNAPSHOTS_NULL);
180-
assertNonNull(mViewHolderClass, ERR_VIEWHOLDER_NULL);
181138

182-
return new FirebaseRecyclerOptions<>(
183-
mSnapshots, mViewHolderClass, mModelLayout, mOwner);
139+
return new FirebaseRecyclerOptions<>(mSnapshots, mOwner);
184140
}
185141
}
186142

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,8 @@ public void onBindViewHolder(VH holder, int position) {
105105
}
106106

107107
/**
108-
* Called when data has been added/changed and an item needs to be displayed.
109-
*
110-
* @param holder the view to populate.
111-
* @param position the position in the list of the view being populated.
112-
* @param model the model object containing the data that should be used to populate the
113-
* view.
108+
* @param model the model object containing the data that should be used to populate the view.
109+
* @see #onBindViewHolder(RecyclerView.ViewHolder, int)
114110
*/
115111
protected abstract void onBindViewHolder(VH holder, int position, T model);
116112
}

0 commit comments

Comments
 (0)