|
| 1 | +package com.gsoltis.androidchat; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.util.Log; |
| 5 | +import android.view.LayoutInflater; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.BaseAdapter; |
| 9 | +import com.firebase.client.ChildEventListener; |
| 10 | +import com.firebase.client.DataSnapshot; |
| 11 | +import com.firebase.client.Query; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +/** |
| 19 | + * User: greg |
| 20 | + * Date: 6/21/13 |
| 21 | + * Time: 1:47 PM |
| 22 | + */ |
| 23 | +public abstract class FirebaseListAdapter<T> extends BaseAdapter { |
| 24 | + |
| 25 | + private Query ref; |
| 26 | + private Class<T> modelClass; |
| 27 | + private int layout; |
| 28 | + private LayoutInflater inflater; |
| 29 | + private List<T> models; |
| 30 | + private Map<String, T> modelNames; |
| 31 | + private ChildEventListener listener; |
| 32 | + |
| 33 | + |
| 34 | + public FirebaseListAdapter(Query ref, Class<T> modelClass, int layout, Activity activity) { |
| 35 | + this.ref = ref; |
| 36 | + this.modelClass = modelClass; |
| 37 | + this.layout = layout; |
| 38 | + inflater = activity.getLayoutInflater(); |
| 39 | + models = new ArrayList<T>(); |
| 40 | + modelNames = new HashMap<String, T>(); |
| 41 | + listener = this.ref.addChildEventListener(new ChildEventListener() { |
| 42 | + @Override |
| 43 | + public void onChildAdded(DataSnapshot dataSnapshot, String s) { |
| 44 | + System.out.println("out: " + System.currentTimeMillis()); |
| 45 | + long start = System.currentTimeMillis(); |
| 46 | + T model = dataSnapshot.getValue(FirebaseListAdapter.this.modelClass); |
| 47 | + long postParse = System.currentTimeMillis(); |
| 48 | + models.add(model); |
| 49 | + modelNames.put(dataSnapshot.getName(), model); |
| 50 | + |
| 51 | + notifyDataSetChanged(); |
| 52 | + long end = System.currentTimeMillis(); |
| 53 | + Log.i("FirebaseListAdapter", "Child added took " + (end - start) + "ms, " + (postParse - start) + "ms parsing, " + |
| 54 | + (end - postParse) + "ms notifying" |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onChildChanged(DataSnapshot dataSnapshot, String s) { |
| 60 | + String modelName = dataSnapshot.getName(); |
| 61 | + T oldModel = modelNames.get(modelName); |
| 62 | + T newModel = dataSnapshot.getValue(FirebaseListAdapter.this.modelClass); |
| 63 | + int index = models.indexOf(oldModel); |
| 64 | + |
| 65 | + models.set(index, newModel); |
| 66 | + |
| 67 | + notifyDataSetChanged(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onChildRemoved(DataSnapshot dataSnapshot) { |
| 72 | + String modelName = dataSnapshot.getName(); |
| 73 | + T oldModel = modelNames.get(modelName); |
| 74 | + models.remove(oldModel); |
| 75 | + modelNames.remove(modelName); |
| 76 | + notifyDataSetChanged(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) { |
| 81 | + String modelName = dataSnapshot.getName(); |
| 82 | + T oldModel = modelNames.get(modelName); |
| 83 | + T newModel = dataSnapshot.getValue(FirebaseListAdapter.this.modelClass); |
| 84 | + int index = models.indexOf(oldModel); |
| 85 | + models.remove(index); |
| 86 | + if (previousChildName == null) { |
| 87 | + models.add(0, newModel); |
| 88 | + } else { |
| 89 | + T previousModel = modelNames.get(previousChildName); |
| 90 | + int previousIndex = models.indexOf(previousModel); |
| 91 | + int nextIndex = previousIndex + 1; |
| 92 | + if (nextIndex == models.size()) { |
| 93 | + models.add(newModel); |
| 94 | + } else { |
| 95 | + models.add(nextIndex, newModel); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void onCancelled() { |
| 102 | + Log.e("FirebaseListAdapter", "Listen was cancelled, no more updates will occur"); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + public void cleanup() { |
| 108 | + ref.removeEventListener(listener); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int getCount() { |
| 113 | + return models.size(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Object getItem(int i) { |
| 118 | + return models.get(i); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public long getItemId(int i) { |
| 123 | + return i; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public View getView(int i, View view, ViewGroup viewGroup) { |
| 128 | + if (view == null) { |
| 129 | + view = inflater.inflate(layout, viewGroup, false); |
| 130 | + } |
| 131 | + |
| 132 | + T model = models.get(i); |
| 133 | + populateView(view, model); |
| 134 | + return view; |
| 135 | + } |
| 136 | + |
| 137 | + protected abstract void populateView(View v, T model); |
| 138 | +} |
0 commit comments