Skip to content

Commit a683a1b

Browse files
committed
Include the rest of the new API methods
1 parent 5aee5bb commit a683a1b

File tree

5 files changed

+771
-248
lines changed

5 files changed

+771
-248
lines changed

app/src/main/java/durdinapps/rxfirebase2/DocumentSnapshotMapper.java

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import android.support.annotation.NonNull;
55

66
import com.google.firebase.firestore.DocumentSnapshot;
7+
import com.google.firebase.firestore.QuerySnapshot;
8+
9+
import java.util.ArrayList;
10+
import java.util.LinkedHashMap;
11+
import java.util.List;
712

813
import io.reactivex.functions.Function;
914
import io.reactivex.functions.Predicate;
@@ -14,18 +19,30 @@ private DocumentSnapshotMapper() {
1419
}
1520

1621
public static <U> DocumentSnapshotMapper<DocumentSnapshot, U> of(Class<U> clazz) {
17-
return new DocumentSnapshotMapper.TypedDataSnapshotMapper<U>(clazz);
22+
return new TypedDocumentSnapshotMapper<U>(clazz);
23+
}
24+
25+
public static <U> DocumentSnapshotMapper<QuerySnapshot, List<U>> listOf(Class<U> clazz) {
26+
return new DocumentSnapshotMapper.TypedListDocumentSnapshotMapper<>(clazz);
27+
}
28+
29+
public static <U> DocumentSnapshotMapper<QuerySnapshot, List<U>> listOf(Class<U> clazz, Function<DocumentSnapshot, U> mapper) {
30+
return new DocumentSnapshotMapper.TypedListDocumentSnapshotMapper<>(clazz, mapper);
31+
}
32+
33+
public static <U> TypedMapDocumentSnapshotMapper<U> mapOf(Class<U> clazz) {
34+
return new DocumentSnapshotMapper.TypedMapDocumentSnapshotMapper<>(clazz);
1835
}
1936

2037
private static <U> U getDataSnapshotTypedValue(DocumentSnapshot documentSnapshot, Class<U> clazz) {
2138
return documentSnapshot.toObject(clazz);
2239
}
2340

24-
private static class TypedDataSnapshotMapper<U> extends DocumentSnapshotMapper<DocumentSnapshot, U> {
41+
private static class TypedDocumentSnapshotMapper<U> extends DocumentSnapshotMapper<DocumentSnapshot, U> {
2542

2643
private final Class<U> clazz;
2744

28-
public TypedDataSnapshotMapper(final Class<U> clazz) {
45+
public TypedDocumentSnapshotMapper(final Class<U> clazz) {
2946
this.clazz = clazz;
3047
}
3148

@@ -35,7 +52,59 @@ public U apply(final DocumentSnapshot documentSnapshot) {
3552
}
3653
}
3754

38-
static final Predicate<DocumentSnapshot> DOCUMENT_EXISTENCE_PREDICATE= new Predicate<DocumentSnapshot>() {
55+
private static class TypedListDocumentSnapshotMapper<U> extends DocumentSnapshotMapper<QuerySnapshot, List<U>> {
56+
57+
private final Class<U> clazz;
58+
private final Function<DocumentSnapshot, U> mapper;
59+
60+
TypedListDocumentSnapshotMapper(final Class<U> clazz) {
61+
this(clazz, null);
62+
}
63+
64+
TypedListDocumentSnapshotMapper(final Class<U> clazz, Function<DocumentSnapshot, U> mapper) {
65+
this.clazz = clazz;
66+
this.mapper = mapper;
67+
}
68+
69+
@Override
70+
public List<U> apply(final QuerySnapshot querySnapshot) throws Exception {
71+
List<U> items = new ArrayList<>();
72+
for (DocumentSnapshot documentSnapshot : querySnapshot) {
73+
items.add(mapper != null
74+
? mapper.apply(documentSnapshot)
75+
: getDataSnapshotTypedValue(documentSnapshot, clazz));
76+
}
77+
return items;
78+
}
79+
}
80+
81+
82+
private static class TypedMapDocumentSnapshotMapper<U> extends DocumentSnapshotMapper<QuerySnapshot, LinkedHashMap<String, U>> {
83+
84+
private final Class<U> clazz;
85+
86+
TypedMapDocumentSnapshotMapper(final Class<U> clazz) {
87+
this.clazz = clazz;
88+
}
89+
90+
@Override
91+
public LinkedHashMap<String, U> apply(final QuerySnapshot querySnapshot) {
92+
LinkedHashMap<String, U> items = new LinkedHashMap<>();
93+
for (DocumentSnapshot documentSnapshot : querySnapshot) {
94+
items.put(documentSnapshot.getId(), getDataSnapshotTypedValue(documentSnapshot, clazz));
95+
}
96+
return items;
97+
}
98+
}
99+
100+
static final Predicate<QuerySnapshot> QUERY_EXISTENCE_PREDICATE = new Predicate<QuerySnapshot>() {
101+
@Override
102+
public boolean test(@NonNull QuerySnapshot querySnapshot) throws Exception {
103+
return querySnapshot.isEmpty();
104+
}
105+
};
106+
107+
static final Predicate<DocumentSnapshot> DOCUMENT_EXISTENCE_PREDICATE = new Predicate<DocumentSnapshot>() {
39108
@Override
40109
public boolean test(@NonNull DocumentSnapshot documentSnapshot) throws Exception {
41110
return documentSnapshot.exists();

app/src/main/java/durdinapps/rxfirebase2/RxFirebaseDatabase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public Transaction.Result doTransaction(MutableData mutableData) {
132132
}
133133

134134
@Override
135-
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
135+
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
136136
if (databaseError != null) {
137137
emitter.onError(new RxFirebaseDataException(databaseError));
138138
} else {

0 commit comments

Comments
 (0)