Skip to content

Commit 07f95c1

Browse files
committed
Made notifications granular
This allows the recycler view to animate the changes.
1 parent ce2c441 commit 07f95c1

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private Bean getBean(FirebaseArray array, int index) {
154154
public static void runAndWaitUntil(final FirebaseArray array, Firebase ref, Runnable task, Callable<Boolean> done) throws InterruptedException {
155155
final java.util.concurrent.Semaphore semaphore = new java.util.concurrent.Semaphore(0);
156156
array.setOnChangedListener(new FirebaseArray.OnChangedListener() {
157-
public void onChanged() {
157+
public void onChanged(FirebaseArray.OnChangedListener.EventType type, int index, int oldIndex) {
158158
semaphore.release();
159159
}
160160
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private static void print(FirebaseArray array) {
125125
public static void runAndWaitUntil(final FirebaseArray array, Firebase ref, Runnable task, Callable<Boolean> done) throws InterruptedException {
126126
final java.util.concurrent.Semaphore semaphore = new java.util.concurrent.Semaphore(0);
127127
array.setOnChangedListener(new FirebaseArray.OnChangedListener() {
128-
public void onChanged() {
128+
public void onChanged(FirebaseArray.OnChangedListener.EventType type, int index, int oldIndex) {
129129
semaphore.release();
130130
}
131131
});

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import com.firebase.client.*;
44

55
import java.util.ArrayList;
6+
import java.util.Collections;
67

78
/**
89
* This class implements an array-like collection on top of a Firebase location.
910
*/
1011
class FirebaseArray implements ChildEventListener {
1112
public interface OnChangedListener {
12-
void onChanged();
13+
enum EventType { Added, Changed, Removed, Moved }
14+
void onChanged(EventType type, int index, int oldIndex);
1315
}
1416

1517
private Query mQuery;
@@ -53,25 +55,27 @@ public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
5355
index = getIndexForKey(previousChildKey) + 1;
5456
}
5557
mSnapshots.add(index, snapshot);
56-
notifyChangedListeners();
58+
notifyChangedListeners(OnChangedListener.EventType.Added, index);
5759
}
5860

5961
public void onChildChanged(DataSnapshot snapshot, String previousChildKey) {
6062
int index = getIndexForKey(snapshot.getKey());
6163
mSnapshots.set(index, snapshot);
62-
notifyChangedListeners();
64+
notifyChangedListeners(OnChangedListener.EventType.Changed, index);
6365
}
6466

6567
public void onChildRemoved(DataSnapshot snapshot) {
6668
int index = getIndexForKey(snapshot.getKey());
6769
mSnapshots.remove(index);
68-
notifyChangedListeners();
70+
notifyChangedListeners(OnChangedListener.EventType.Removed, index);
6971
}
7072

7173
public void onChildMoved(DataSnapshot snapshot, String previousChildKey) {
72-
onChildRemoved(snapshot);
73-
onChildAdded(snapshot, previousChildKey);
74-
// TODO: this will send two change notifications, which is wrong
74+
int oldIndex = getIndexForKey(snapshot.getKey());
75+
mSnapshots.remove(oldIndex);
76+
int newIndex = previousChildKey == null ? 0 : (getIndexForKey(previousChildKey) + 1);
77+
mSnapshots.add(newIndex, snapshot);
78+
notifyChangedListeners(OnChangedListener.EventType.Moved, newIndex, oldIndex);
7579
}
7680

7781
public void onCancelled(FirebaseError firebaseError) {
@@ -82,9 +86,12 @@ public void onCancelled(FirebaseError firebaseError) {
8286
public void setOnChangedListener(OnChangedListener listener) {
8387
mListener = listener;
8488
}
85-
protected void notifyChangedListeners() {
89+
protected void notifyChangedListeners(OnChangedListener.EventType type, int index) {
90+
notifyChangedListeners(type, index, -1);
91+
}
92+
protected void notifyChangedListeners(OnChangedListener.EventType type, int index, int oldIndex) {
8693
if (mListener != null) {
87-
mListener.onChanged();
94+
mListener.onChanged(type, index, oldIndex);
8895
}
8996
}
9097
}

library/src/main/java/com/firebase/ui/FirebaseListAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public FirebaseListAdapter(Class<T> modelClass, int layout, Activity activity, Q
4040
mSnapshots = new FirebaseArray(ref);
4141
mSnapshots.setOnChangedListener(new FirebaseArray.OnChangedListener() {
4242
@Override
43-
public void onChanged() {
43+
public void onChanged(EventType type, int index, int oldIndex) {
4444
notifyDataSetChanged();
4545
}
4646
});

library/src/main/java/com/firebase/ui/FirebaseRecyclerViewAdapter.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,23 @@ public FirebaseRecyclerViewAdapter(Query ref, Class<T> modelClass) {
3939
// TODO: implement separate notifications for added, removed, changed and moved
4040
mSnapshots.setOnChangedListener(new FirebaseArray.OnChangedListener() {
4141
@Override
42-
public void onChanged() {
43-
notifyDataSetChanged();
42+
public void onChanged(EventType type, int index, int oldIndex) {
43+
switch (type) {
44+
case Added:
45+
notifyItemInserted(index);
46+
break;
47+
case Changed:
48+
notifyItemChanged(index);
49+
break;
50+
case Removed:
51+
notifyItemRemoved(index);
52+
break;
53+
case Moved:
54+
notifyItemMoved(oldIndex, index);
55+
break;
56+
default:
57+
throw new IllegalStateException("Incomplete case statement");
58+
}
4459
}
4560
});
4661

0 commit comments

Comments
 (0)