Skip to content

Commit 57d0de5

Browse files
authored
Replace adapter constructors with options (#7)
1 parent fea9eb3 commit 57d0de5

24 files changed

+589
-453
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import android.widget.Toast;
1616

1717
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
18+
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
1819
import com.firebase.uidemo.R;
1920
import com.firebase.uidemo.database.ChatHolder;
2021
import com.google.android.gms.tasks.OnCompleteListener;
@@ -68,7 +69,12 @@ protected void onCreate(Bundle savedInstanceState) {
6869
Query query = mFirestore.collection("chats").orderBy("timestamp").limit(50);
6970

7071
LinearLayoutManager manager = new LinearLayoutManager(this);
71-
mAdapter = new FirestoreRecyclerAdapter<Chat, ChatHolder>(query, Chat.class) {
72+
73+
FirestoreRecyclerOptions<Chat> options = new FirestoreRecyclerOptions.Builder<Chat>()
74+
.setQuery(query, Chat.class)
75+
.build();
76+
77+
mAdapter = new FirestoreRecyclerAdapter<Chat, ChatHolder>(options) {
7278
@Override
7379
public void onBindViewHolder(ChatHolder holder, int position, Chat model) {
7480
holder.bind(model);

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import android.widget.Toast;
3030

3131
import com.firebase.ui.database.FirebaseRecyclerAdapter;
32+
import com.firebase.ui.database.FirebaseRecyclerOptions;
3233
import com.firebase.uidemo.R;
3334
import com.firebase.uidemo.database.ChatHolder;
3435
import com.firebase.uidemo.util.SignInResultNotifier;
@@ -140,12 +141,16 @@ public void onItemRangeInserted(int positionStart, int itemCount) {
140141

141142
protected FirebaseRecyclerAdapter<Chat, ChatHolder> getAdapter() {
142143
Query lastFifty = mChatRef.limitToLast(50);
143-
return new FirebaseRecyclerAdapter<Chat, ChatHolder>(
144-
Chat.class,
145-
R.layout.message,
146-
ChatHolder.class,
147-
lastFifty,
148-
this) {
144+
145+
FirebaseRecyclerOptions<Chat, ChatHolder> options =
146+
new FirebaseRecyclerOptions.Builder<Chat, ChatHolder>()
147+
.setViewHolder(R.layout.message, ChatHolder.class)
148+
.setQuery(lastFifty, Chat.class)
149+
.setLifecycleOwner(this)
150+
.build();
151+
152+
return new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
153+
149154
@Override
150155
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
151156
holder.bind(chat);

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import android.view.View;
44

5-
import com.firebase.ui.database.FirebaseIndexRecyclerAdapter;
65
import com.firebase.ui.database.FirebaseRecyclerAdapter;
6+
import com.firebase.ui.database.FirebaseRecyclerOptions;
77
import com.firebase.uidemo.R;
88
import com.firebase.uidemo.database.ChatHolder;
99
import com.google.firebase.auth.FirebaseAuth;
@@ -33,13 +33,14 @@ protected FirebaseRecyclerAdapter<Chat, ChatHolder> getAdapter() {
3333
.child("chatIndices")
3434
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
3535

36-
return new FirebaseIndexRecyclerAdapter<Chat, ChatHolder>(
37-
Chat.class,
38-
R.layout.message,
39-
ChatHolder.class,
40-
mChatIndicesRef.limitToLast(50),
41-
mChatRef,
42-
this) {
36+
FirebaseRecyclerOptions<Chat, ChatHolder> options =
37+
new FirebaseRecyclerOptions.Builder<Chat, ChatHolder>()
38+
.setIndexedQuery(mChatIndicesRef.limitToFirst(50), mChatRef, Chat.class)
39+
.setViewHolder(R.layout.message, ChatHolder.class)
40+
.setLifecycleOwner(this)
41+
.build();
42+
43+
return new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
4344
@Override
4445
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
4546
holder.bind(chat);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,21 @@
77
*/
88
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
99
public class Preconditions {
10+
1011
public static <T> T checkNotNull(T o) {
1112
if (o == null) throw new IllegalArgumentException("Argument cannot be null.");
1213
return o;
1314
}
15+
16+
public static void assertNull(Object object, String message) {
17+
if (object != null) {
18+
throw new RuntimeException(message);
19+
}
20+
}
21+
22+
public static void assertNonNull(Object object, String message) {
23+
if (object == null) {
24+
throw new RuntimeException(message);
25+
}
26+
}
1427
}

database/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ android {
2525
dependencies {
2626
compile project(path: ':common')
2727
compile "com.google.firebase:firebase-database:$firebaseVersion"
28+
29+
compile "android.arch.lifecycle:runtime:$architectureVersion"
30+
compile "android.arch.lifecycle:extensions:$architectureVersion"
2831
annotationProcessor "android.arch.lifecycle:compiler:$architectureVersion"
2932

3033
androidTestCompile 'junit:junit:4.12'

database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayOfObjectsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void setUp() throws Exception {
4646
.getReference()
4747
.child("firebasearray")
4848
.child("objects");
49-
mArray = new FirebaseArray<>(mRef, Bean.class);
49+
mArray = new FirebaseArray<>(mRef, new ClassSnapshotParser<>(Bean.class));
5050
mRef.removeValue();
5151
mListener = runAndWaitUntil(mArray, new Runnable() {
5252
@Override

database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class FirebaseArrayTest {
4343
public void setUp() throws Exception {
4444
FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext());
4545
mRef = FirebaseDatabase.getInstance(app).getReference().child("firebasearray");
46-
mArray = new FirebaseArray<>(mRef, Integer.class);
46+
mArray = new FirebaseArray<>(mRef, new ClassSnapshotParser<>(Integer.class));
4747
mRef.removeValue();
4848
mListener = runAndWaitUntil(mArray, new Runnable() {
4949
@Override

database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayOfObjectsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void setUp() throws Exception {
4646
mRef = databaseInstance.getReference().child("firebasearray").child("objects");
4747
mKeyRef = databaseInstance.getReference().child("firebaseindexarray").child("objects");
4848

49-
mArray = new FirebaseIndexArray<>(mKeyRef, mRef, Bean.class);
49+
mArray = new FirebaseIndexArray<>(mKeyRef, mRef, new ClassSnapshotParser<>(Bean.class));
5050
mRef.removeValue();
5151
mKeyRef.removeValue();
5252

database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void setUp() throws Exception {
4747
mRef = databaseInstance.getReference().child("firebasearray");
4848
mKeyRef = databaseInstance.getReference().child("firebaseindexarray");
4949

50-
mArray = new FirebaseIndexArray<>(mKeyRef, mRef, Integer.class);
50+
mArray = new FirebaseIndexArray<>(mKeyRef, mRef, new ClassSnapshotParser<>(Integer.class));
5151
mRef.removeValue();
5252
mKeyRef.removeValue();
5353

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,12 @@ public class FirebaseArray<T> extends ObservableSnapshotArray<T>
3535
private List<DataSnapshot> mSnapshots = new ArrayList<>();
3636

3737
/**
38-
* Create a new FirebaseArray that parses snapshots as members of a given class.
38+
* Create a new FirebaseArray with a custom {@link SnapshotParser}.
3939
*
4040
* @param query The Firebase location to watch for data changes. Can also be a slice of a
4141
* location, using some combination of {@code limit()}, {@code startAt()}, and
4242
* {@code endAt()}.
43-
* @see ObservableSnapshotArray#ObservableSnapshotArray(Class)
44-
*/
45-
public FirebaseArray(Query query, Class<T> tClass) {
46-
super(tClass);
47-
init(query);
48-
}
49-
50-
/**
51-
* Create a new FirebaseArray with a custom {@link SnapshotParser}.
52-
*
5343
* @see ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)
54-
* @see FirebaseArray#FirebaseArray(Query, Class)
5544
*/
5645
public FirebaseArray(Query query, SnapshotParser<T> parser) {
5746
super(parser);

0 commit comments

Comments
 (0)