Skip to content

Commit 2ba9de0

Browse files
committed
Merge branch 'dev' into firestore
Change-Id: Ide7735cf4a4114e4d9fac9120c8a479385507bda
2 parents c8f0d4a + d871c7a commit 2ba9de0

File tree

5 files changed

+55
-29
lines changed

5 files changed

+55
-29
lines changed

app/src/main/java/com/firebase/uidemo/database/ChatActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected FirebaseRecyclerAdapter<Chat, ChatHolder> getAdapter() {
146146
lastFifty,
147147
this) {
148148
@Override
149-
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
149+
public void populateViewHolder(ChatHolder holder, Cgit hat chat, int position) {
150150
holder.bind(chat);
151151
}
152152

app/src/main/java/com/firebase/uidemo/database/ChatIndexActivity.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.firebase.uidemo.database;
22

3-
import android.os.Bundle;
43
import android.view.View;
54

65
import com.firebase.ui.database.FirebaseIndexRecyclerAdapter;
@@ -13,12 +12,6 @@
1312
public class ChatIndexActivity extends ChatActivity {
1413
private DatabaseReference mChatIndicesRef;
1514

16-
@Override
17-
protected void onCreate(Bundle savedInstanceState) {
18-
super.onCreate(savedInstanceState);
19-
mChatIndicesRef = FirebaseDatabase.getInstance().getReference().child("chatIndices");
20-
}
21-
2215
@Override
2316
public void onClick(View v) {
2417
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
@@ -34,6 +27,11 @@ public void onClick(View v) {
3427

3528
@Override
3629
protected FirebaseRecyclerAdapter<Chat, ChatHolder> getAdapter() {
30+
mChatIndicesRef = FirebaseDatabase.getInstance()
31+
.getReference()
32+
.child("chatIndices")
33+
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
34+
3735
return new FirebaseIndexRecyclerAdapter<Chat, ChatHolder>(
3836
Chat.class,
3937
R.layout.message,

auth/src/main/java/com/firebase/ui/auth/ui/ImeHelper.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ public static void setImeOnDoneListener(EditText doneEditText,
1717
doneEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
1818
@Override
1919
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
20-
if (event != null
21-
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER
22-
|| actionId == EditorInfo.IME_ACTION_DONE) {
20+
if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
21+
if (event.getAction() == KeyEvent.ACTION_UP) {
22+
listener.onDonePressed();
23+
}
24+
25+
// We need to return true even if we didn't handle the event to continue
26+
// receiving future callbacks.
27+
return true;
28+
} else if (actionId == EditorInfo.IME_ACTION_DONE) {
2329
listener.onDonePressed();
2430
return true;
2531
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,10 @@ public void onChildMoved(DataSnapshot snapshot, String previousChildKey) {
107107
int oldIndex = getIndexForKey(snapshot.getKey());
108108
mSnapshots.remove(oldIndex);
109109

110-
int newIndex = previousChildKey == null ? 0 : (getIndexForKey(previousChildKey) + 1);
110+
int newIndex = previousChildKey == null ? 0 : getIndexForKey(previousChildKey) + 1;
111111
mSnapshots.add(newIndex, snapshot);
112112

113-
notifyListenersOnChildChanged(ChangeEventType.MOVED, snapshot,
114-
newIndex, oldIndex);
113+
notifyListenersOnChildChanged(ChangeEventType.MOVED, snapshot, newIndex, oldIndex);
115114
}
116115

117116
@Override

database/src/main/java/com/firebase/ui/database/FirebaseIndexArray.java

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,32 @@ protected List<DataSnapshot> getSnapshots() {
141141
return mDataSnapshots;
142142
}
143143

144-
private int getIndexForKey(String key) {
145-
int dataCount = size();
146-
int index = 0;
147-
for (int keyIndex = 0; index < dataCount; keyIndex++) {
148-
String superKey = mKeySnapshots.getObject(keyIndex);
149-
if (key.equals(superKey)) {
150-
break;
151-
} else if (mDataSnapshots.get(index).getKey().equals(superKey)) {
152-
index++;
144+
private int returnOrFindIndexForKey(int index, String key) {
145+
int realIndex;
146+
if (isKeyAtIndex(key, index)) {
147+
// To optimize this query, if the expected item position is accurate, we simply return
148+
// it instead of searching for it in our keys all over again. This ensures developers
149+
// correctly indexing their data (i.e. no null values) don't take a performance hit.
150+
realIndex = index;
151+
} else {
152+
int dataCount = size();
153+
int dataIndex = 0;
154+
int keyIndex = 0;
155+
156+
while (dataIndex < dataCount && keyIndex < mKeySnapshots.size()) {
157+
String superKey = mKeySnapshots.getObject(keyIndex);
158+
if (key.equals(superKey)) {
159+
break;
160+
} else if (mDataSnapshots.get(dataIndex).getKey().equals(superKey)) {
161+
// Only increment the data index if we aren't passing over a null value snapshot.
162+
dataIndex++;
163+
}
164+
keyIndex++;
153165
}
166+
167+
realIndex = dataIndex;
154168
}
155-
return index;
169+
return realIndex;
156170
}
157171

158172
/**
@@ -174,10 +188,15 @@ protected void onKeyAdded(DataSnapshot data) {
174188
protected void onKeyMoved(DataSnapshot data, int index, int oldIndex) {
175189
String key = data.getKey();
176190

191+
// We can't use `returnOrFindIndexForKey(...)` for `oldIndex` or it might find the updated
192+
// index instead of the old one. Unfortunately, this does mean move events will be
193+
// incorrectly ignored if our list is a subset of the key list e.g. a key has null data.
177194
if (isKeyAtIndex(key, oldIndex)) {
178195
DataSnapshot snapshot = removeData(oldIndex);
196+
int realIndex = returnOrFindIndexForKey(index, key);
179197
mHasPendingMoveOrDelete = true;
180-
mDataSnapshots.add(index, snapshot);
198+
199+
mDataSnapshots.add(realIndex, snapshot);
181200
notifyListenersOnChildChanged(ChangeEventType.MOVED, snapshot, index, oldIndex);
182201
}
183202
}
@@ -187,10 +206,11 @@ protected void onKeyRemoved(DataSnapshot data, int index) {
187206
ValueEventListener listener = mRefs.remove(mDataRef.getRef().child(key));
188207
if (listener != null) mDataRef.child(key).removeEventListener(listener);
189208

190-
if (isKeyAtIndex(key, index)) {
191-
DataSnapshot snapshot = removeData(index);
209+
int realIndex = returnOrFindIndexForKey(index, key);
210+
if (isKeyAtIndex(key, realIndex)) {
211+
DataSnapshot snapshot = removeData(realIndex);
192212
mHasPendingMoveOrDelete = true;
193-
notifyListenersOnChildChanged(ChangeEventType.REMOVED, snapshot, index, -1);
213+
notifyListenersOnChildChanged(ChangeEventType.REMOVED, snapshot, realIndex, -1);
194214
}
195215
}
196216

@@ -236,10 +256,13 @@ public String toString() {
236256
* A ValueEventListener attached to the joined child data.
237257
*/
238258
protected class DataRefListener implements ValueEventListener {
259+
/** Cached index to skip searching for the current index on each update */
260+
private int currentIndex;
261+
239262
@Override
240263
public void onDataChange(DataSnapshot snapshot) {
241264
String key = snapshot.getKey();
242-
int index = getIndexForKey(key);
265+
int index = currentIndex = returnOrFindIndexForKey(currentIndex, key);
243266

244267
if (snapshot.getValue() != null) {
245268
if (isKeyAtIndex(key, index)) {

0 commit comments

Comments
 (0)