Skip to content

Commit 43ee4e5

Browse files
committed
Move errors up to base class
Change-Id: I5fddbe75f2c99c5ae832db2e44f09105634cbef3
1 parent 981f7ef commit 43ee4e5

File tree

6 files changed

+41
-29
lines changed

6 files changed

+41
-29
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
import java.util.concurrent.CopyOnWriteArrayList;
99

1010
/**
11-
* Exposes a collection of {@link S} items in a database as a {@link List} of {@link E} objects.
11+
* Exposes a collection of {@link S} items in a database as a {@link List} of {@link T} objects.
1212
* To observe the list attach a {@link L} listener.
1313
*
1414
* @param <S> the snapshot class.
15+
* @param <E> the error type raised for the listener.
1516
* @param <L> the listener class.
16-
* @param <E> the model object class.
17+
* @param <T> the model object class.
1718
*/
18-
public abstract class BaseObservableSnapshotArray<S, L extends BaseChangeEventListener<S,?>, E>
19+
public abstract class BaseObservableSnapshotArray<S, E, L extends BaseChangeEventListener<S, E>, T>
1920
extends AbstractList<S> {
2021

2122
private final List<L> mListeners = new CopyOnWriteArrayList<>();
22-
private BaseSnapshotParser<S, E> mParser;
23+
private BaseSnapshotParser<S, T> mParser;
2324

2425
/**
2526
* True if there has been a "data changed" event since the array was created, false otherwise.
@@ -37,7 +38,7 @@ public BaseObservableSnapshotArray() {}
3738
*
3839
* @param parser the {@link BaseSnapshotParser} to use
3940
*/
40-
public BaseObservableSnapshotArray(@NonNull BaseSnapshotParser<S, E> parser) {
41+
public BaseObservableSnapshotArray(@NonNull BaseSnapshotParser<S, T> parser) {
4142
mParser = Preconditions.checkNotNull(parser);
4243
}
4344

@@ -146,7 +147,7 @@ public final boolean isListening(L listener) {
146147
* type. This uses the {@link BaseSnapshotParser} passed to the constructor. If the parser was not
147148
* initialized this will throw an unchecked exception.
148149
*/
149-
public E getObject(int index) {
150+
public T getObject(int index) {
150151
if (mParser == null) {
151152
throw new IllegalStateException("getObject() called before snapshot parser set.");
152153
}
@@ -171,11 +172,17 @@ protected void notifyListenersOnDataChanged() {
171172
}
172173
}
173174

174-
protected BaseSnapshotParser<S, E> getSnapshotParser() {
175+
protected void notifyListenersOnError(E e) {
176+
for (L listener : getListeners()) {
177+
listener.onError(e);
178+
}
179+
}
180+
181+
protected BaseSnapshotParser<S, T> getSnapshotParser() {
175182
return mParser;
176183
}
177184

178-
protected void setSnapshotParser(BaseSnapshotParser<S, E> parser) {
185+
protected void setSnapshotParser(BaseSnapshotParser<S, T> parser) {
179186
mParser = parser;
180187
}
181188
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {
121121

122122
@Override
123123
public void onCancelled(DatabaseError error) {
124-
notifyListenersOnCancelled(error);
124+
notifyListenersOnError(error);
125125
}
126126

127127
private int getIndexForKey(String key) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ protected void onKeyRemoved(DataSnapshot data, int index) {
194194
}
195195
}
196196

197+
@Override
198+
public DataSnapshot get(int i) {
199+
return mDataSnapshots.get(i);
200+
}
201+
202+
@Override
203+
public int size() {
204+
return mKeySnapshots.size();
205+
}
206+
197207
@Override
198208
public boolean equals(Object obj) {
199209
if (this == obj) return true;
@@ -260,7 +270,7 @@ public void onDataChange(DataSnapshot snapshot) {
260270

261271
@Override
262272
public void onCancelled(DatabaseError error) {
263-
notifyListenersOnCancelled(error);
273+
notifyListenersOnError(error);
264274
}
265275
}
266276
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
* Exposes a collection of items in Firebase as a {@link List} of {@link DataSnapshot}. To observe
1313
* the list attach a {@link com.google.firebase.database.ChildEventListener}.
1414
*
15-
* @param <E> a POJO class to which the DataSnapshots can be converted.
15+
* @param <T> a POJO class to which the DataSnapshots can be converted.
1616
*/
17-
public abstract class ObservableSnapshotArray<E>
18-
extends BaseObservableSnapshotArray<DataSnapshot, ChangeEventListener, E> {
17+
public abstract class ObservableSnapshotArray<T>
18+
extends BaseObservableSnapshotArray<DataSnapshot, DatabaseError, ChangeEventListener, T> {
1919

2020
/**
2121
* Default constructor. Must set the snapshot parser before user.
@@ -31,7 +31,7 @@ public ObservableSnapshotArray() {
3131
* @param clazz the class as which DataSnapshots should be parsed.
3232
* @see ClassSnapshotParser
3333
*/
34-
public ObservableSnapshotArray(@NonNull Class<E> clazz) {
34+
public ObservableSnapshotArray(@NonNull Class<T> clazz) {
3535
super(new ClassSnapshotParser<>(clazz));
3636
}
3737

@@ -40,13 +40,15 @@ public ObservableSnapshotArray(@NonNull Class<E> clazz) {
4040
*
4141
* @param parser the {@link SnapshotParser} to use
4242
*/
43-
public ObservableSnapshotArray(@NonNull SnapshotParser<E> parser) {
43+
public ObservableSnapshotArray(@NonNull SnapshotParser<T> parser) {
4444
super(parser);
4545
}
4646

47+
/**
48+
* Use {@link BaseObservableSnapshotArray#notifyListenersOnError(Object)}.
49+
*/
50+
@Deprecated
4751
protected void notifyListenersOnCancelled(DatabaseError error) {
48-
for (ChangeEventListener listener : getListeners()) {
49-
listener.onError(error);
50-
}
52+
notifyListenersOnError(error);
5153
}
5254
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected void onDestroy() {
6767
public void onEvent(QuerySnapshot snapshots, FirebaseFirestoreException e) {
6868
if (e != null) {
6969
Log.w(TAG, "Error in snapshot listener", e);
70-
notifyOnError(e);
70+
notifyListenersOnError(e);
7171
return;
7272
}
7373

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
/**
1111
* Subclass of {@link BaseObservableSnapshotArray} for Firestore data.
1212
*/
13-
public abstract class ObservableSnapshotArray<E>
14-
extends BaseObservableSnapshotArray<DocumentSnapshot, ChangeEventListener, E> {
13+
public abstract class ObservableSnapshotArray<T>
14+
extends BaseObservableSnapshotArray<DocumentSnapshot, FirebaseFirestoreException, ChangeEventListener, T> {
1515

1616
public ObservableSnapshotArray() {
1717
super();
@@ -20,14 +20,7 @@ public ObservableSnapshotArray() {
2020
/**
2121
* See {@link BaseObservableSnapshotArray#BaseObservableSnapshotArray(BaseSnapshotParser)}
2222
*/
23-
public ObservableSnapshotArray(@NonNull SnapshotParser<E> parser) {
23+
public ObservableSnapshotArray(@NonNull SnapshotParser<T> parser) {
2424
super(parser);
2525
}
26-
27-
// TODO: There should be a way to move this into the base class
28-
protected void notifyOnError(FirebaseFirestoreException e) {
29-
for (ChangeEventListener listener : getListeners()) {
30-
listener.onError(e);
31-
}
32-
}
3326
}

0 commit comments

Comments
 (0)