Skip to content

Commit f9d8aba

Browse files
committed
Add some comments, switch the order of indexes in events.
Change-Id: I27a584c7fec5ef89feb4a0e171a24e8babac99ea
1 parent 5ddf442 commit f9d8aba

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.firebase.ui.common;
22

3-
3+
/**
4+
* Common interface for snapshot parsers.
5+
*
6+
* @param <S> snapshot type.
7+
* @param <T> parsed object type.
8+
*/
49
public interface BaseSnapshotParser<S, T> {
510

611
/**

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,61 @@
44
import com.google.firebase.firestore.FirebaseFirestoreException;
55

66
/**
7+
* Listener for changes to a {@link FirestoreArray}.
78
* TODO: This could be a common interface, it just needs to know about the snapshot and error types.
89
*/
910
public interface ChangeEventListener {
1011

12+
/**
13+
* The type of change to an element of the array,
14+
*/
1115
enum Type {
16+
17+
/**
18+
* An element was added to the array.
19+
*/
1220
ADDED,
21+
22+
/**
23+
* An element was removed from the array.
24+
*/
1325
REMOVED,
26+
27+
/**
28+
* An element in the array has new content.
29+
*/
1430
MODIFIED,
31+
32+
/**
33+
* An element in the array has a new position, and also new content.
34+
*/
1535
MOVED
1636
}
1737

38+
/**
39+
* A callback for when a child event occurs in a FirestoreArray.
40+
* @param type The type of the event.
41+
* @param snapshot The {@link DocumentSnapshot} of the changed child.
42+
* @param newIndex The new index of the element, or -1 of it is no longer
43+
* @param oldIndex The previous index of the element, or -1 if it was not
44+
* previously tracked.
45+
*/
1846
void onChildChanged(Type type, DocumentSnapshot snapshot,
19-
int oldIndex, int newIndex);
47+
int newIndex, int oldIndex);
2048

49+
/**
50+
* Callback triggered after all child events in a particular snapshot have been
51+
* processed.
52+
* <p>
53+
* Useful for batch events, such as removing a loading indicator after initial load
54+
* or a large update batch.
55+
*/
2156
void onDataChanged();
2257

58+
/**
59+
* Callback when an error has been detected in the underlying Firestore query listener.
60+
* @param e the error that occurred.
61+
*/
2362
void onError(FirebaseFirestoreException e);
2463

2564
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.List;
1616

1717
/**
18-
* TODO(samstern): Document
18+
* Exposes a Firestore query as an observable list of objects.
1919
*/
2020
public class FirestoreArray<T>
2121
extends BaseObservableSnapshotArray<DocumentSnapshot, ChangeEventListener, T>
@@ -96,7 +96,7 @@ private void onDocumentAdded(DocumentChange change) {
9696
// Add the document to the set
9797
mSnapshots.add(change.getNewIndex(), change.getDocument());
9898
notifyOnChildChanged(ChangeEventListener.Type.ADDED, change.getDocument(),
99-
-1, change.getNewIndex());
99+
change.getNewIndex(), -1);
100100
}
101101

102102
private void onDocumentRemoved(DocumentChange change) {
@@ -108,7 +108,7 @@ private void onDocumentRemoved(DocumentChange change) {
108108
// Remove the document from the set
109109
mSnapshots.remove(change.getOldIndex());
110110
notifyOnChildChanged(ChangeEventListener.Type.REMOVED, change.getDocument(),
111-
change.getOldIndex(), -1);
111+
-1, change.getOldIndex());
112112
}
113113

114114
private void onDocumentModified(DocumentChange change) {
@@ -121,14 +121,14 @@ private void onDocumentModified(DocumentChange change) {
121121

122122
mSnapshots.set(change.getOldIndex(), change.getDocument());
123123
notifyOnChildChanged(ChangeEventListener.Type.MODIFIED, change.getDocument(),
124-
change.getOldIndex(), change.getNewIndex());
124+
change.getNewIndex(), change.getOldIndex());
125125
} else {
126126
Log.d(TAG, "Modified (moved): " + change.getOldIndex() + " --> " + change.getNewIndex());
127127

128128
mSnapshots.remove(change.getOldIndex());
129129
mSnapshots.add(change.getNewIndex(), change.getDocument());
130130
notifyOnChildChanged(ChangeEventListener.Type.MOVED, change.getDocument(),
131-
change.getOldIndex(), change.getNewIndex());
131+
change.getNewIndex(), change.getOldIndex());
132132
}
133133
}
134134

@@ -157,11 +157,10 @@ private void stopListening() {
157157

158158
private void notifyOnChildChanged(ChangeEventListener.Type type,
159159
DocumentSnapshot snapshot,
160-
int oldIndex,
161-
int newIndex) {
160+
int newIndex, int oldIndex) {
162161

163162
for (ChangeEventListener listener : mListeners) {
164-
listener.onChildChanged(type, snapshot, oldIndex, newIndex);
163+
listener.onChildChanged(type, snapshot, newIndex, oldIndex);
165164
}
166165
}
167166

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public int getItemCount() {
4141

4242
@Override
4343
public void onChildChanged(Type type, DocumentSnapshot snapshot,
44-
int oldIndex, int newIndex) {
44+
int newIndex, int oldIndex) {
4545

4646
switch (type) {
4747
case ADDED:

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import com.google.firebase.firestore.DocumentSnapshot;
55

66
/**
7-
* TODO
7+
* Base interface for a {@link BaseSnapshotParser} for {@link DocumentSnapshot}.
88
*/
9-
public interface SnapshotParser<T> extends BaseSnapshotParser<DocumentSnapshot, T> {
10-
11-
}
9+
public interface SnapshotParser<T> extends BaseSnapshotParser<DocumentSnapshot, T> {}

0 commit comments

Comments
 (0)