-
-
Save wirasetiawan29/35f8e464245e72587369174053f43861 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FirebaseArray implements ChildEventListener { | |
public interface OnChangedListener { | |
enum EventType { Added, Changed, Removed, Moved } | |
void onChanged(EventType type, int index, int oldIndex); | |
} | |
private Query mQuery; | |
private OnChangedListener mListener; | |
private ArrayList<DataSnapshot> mSnapshots; | |
public FirebaseArray(Query ref) { | |
mQuery = ref; | |
mSnapshots = new ArrayList<DataSnapshot>(); | |
mQuery.addChildEventListener(this); | |
} | |
public void cleanup() { | |
mQuery.removeEventListener(this); | |
} | |
public int getCount() { | |
return mSnapshots.size(); | |
} | |
public DataSnapshot getItem(int index) { | |
return mSnapshots.get(index); | |
} | |
private int getIndexForKey(String key) { | |
int index = 0; | |
for (DataSnapshot snapshot : mSnapshots) { | |
if (snapshot.getKey().equals(key)) { | |
return index; | |
} else { | |
index++; | |
} | |
} | |
throw new IllegalArgumentException("Key not found"); | |
} | |
// Start of ChildEventListener methods | |
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) { | |
int index = 0; | |
if (previousChildKey != null) { | |
index = getIndexForKey(previousChildKey) + 1; | |
} | |
mSnapshots.add(index, snapshot); | |
notifyChangedListeners(OnChangedListener.EventType.Added, index); | |
} | |
public void onChildChanged(DataSnapshot snapshot, String previousChildKey) { | |
int index = getIndexForKey(snapshot.getKey()); | |
mSnapshots.set(index, snapshot); | |
notifyChangedListeners(OnChangedListener.EventType.Changed, index); | |
} | |
public void onChildRemoved(DataSnapshot snapshot) { | |
int index = getIndexForKey(snapshot.getKey()); | |
mSnapshots.remove(index); | |
notifyChangedListeners(OnChangedListener.EventType.Removed, index); | |
} | |
public void onChildMoved(DataSnapshot snapshot, String previousChildKey) { | |
int oldIndex = getIndexForKey(snapshot.getKey()); | |
mSnapshots.remove(oldIndex); | |
int newIndex = previousChildKey == null ? 0 : (getIndexForKey(previousChildKey) + 1); | |
mSnapshots.add(newIndex, snapshot); | |
notifyChangedListeners(OnChangedListener.EventType.Moved, newIndex, oldIndex); | |
} | |
public void onCancelled(FirebaseError firebaseError) { | |
// TODO: what do we do with this? | |
} | |
// End of ChildEventListener methods | |
public void setOnChangedListener(OnChangedListener listener) { | |
mListener = listener; | |
} | |
protected void notifyChangedListeners(OnChangedListener.EventType type, int index) { | |
notifyChangedListeners(type, index, -1); | |
} | |
protected void notifyChangedListeners(OnChangedListener.EventType type, int index, int oldIndex) { | |
if (mListener != null) { | |
mListener.onChanged(type, index, oldIndex); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment