/* * Copyright 2016 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing permissions and * limitations under the License. */ package com.mikepenz.fastadapter_extensions.firebase; import com.google.firebase.database.ChildEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.Query; import java.util.ArrayList; import java.util.List; /** * This class implements an array-like collection on top of a Firebase location. */ 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<>(); } public void attach() { mQuery.addChildEventListener(this); } public void cleanup() { mQuery.removeEventListener(this); } public int getCount() { return mSnapshots.size(); } public List<DataSnapshot> getItems() { return mSnapshots; } 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(DatabaseError 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); } } }