-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add a callback in FirebaseIndexArray when a Key doesn't exist in a ref #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
28c366c
641297c
a62a025
703f85d
f1e702b
af39a1e
25fc4df
5486e8c
23806d7
06fb7d0
0b56e99
331b4bf
f396fec
cfdb305
cc3729c
f5d06b2
de52c1d
97d9cb0
e7297d4
9a03342
8f87492
dcf96bf
48ea75a
4a5a1be
dbbf0dc
b37c8bd
d25245b
7e01eb1
f09383f
03c14d2
63d5ca5
14e3443
ab613bd
64bfb37
ae83d83
371c211
dbc1a43
0a1d2b5
53920a2
b3c137f
c23eec1
d2962fa
52b7c10
f236047
2933598
a2909e4
a11975c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.firebase.uidemo.database; | ||
|
||
public class Chat { | ||
private String mName; | ||
private String mMessage; | ||
private String mUid; | ||
|
||
public Chat() { | ||
// Needed for Firebase | ||
} | ||
|
||
public Chat(String name, String message, String uid) { | ||
mName = name; | ||
mMessage = message; | ||
mUid = uid; | ||
} | ||
|
||
public String getName() { | ||
return mName; | ||
} | ||
|
||
public void setName(String name) { | ||
mName = name; | ||
} | ||
|
||
public String getMessage() { | ||
return mMessage; | ||
} | ||
|
||
public void setMessage(String message) { | ||
mMessage = message; | ||
} | ||
|
||
public String getUid() { | ||
return mUid; | ||
} | ||
|
||
public void setUid(String uid) { | ||
mUid = uid; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,29 +14,22 @@ | |
|
||
package com.firebase.uidemo.database; | ||
|
||
import android.graphics.PorterDuff; | ||
import android.graphics.drawable.GradientDrawable; | ||
import android.graphics.drawable.RotateDrawable; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.util.Log; | ||
import android.view.Gravity; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.FrameLayout; | ||
import android.widget.LinearLayout; | ||
import android.widget.RelativeLayout; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.firebase.ui.database.FirebaseIndexRecyclerAdapter; | ||
import com.firebase.ui.database.FirebaseRecyclerAdapter; | ||
import com.firebase.uidemo.R; | ||
import com.google.android.gms.tasks.OnCompleteListener; | ||
import com.google.android.gms.tasks.OnSuccessListener; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.firebase.auth.AuthResult; | ||
import com.google.firebase.auth.FirebaseAuth; | ||
|
@@ -50,6 +43,7 @@ public class ChatActivity extends AppCompatActivity implements FirebaseAuth.Auth | |
private static final String TAG = "RecyclerViewDemo"; | ||
|
||
private FirebaseAuth mAuth; | ||
private DatabaseReference mChatIndicesRef; | ||
private DatabaseReference mChatRef; | ||
private Button mSendButton; | ||
private EditText mMessageEdit; | ||
|
@@ -69,7 +63,9 @@ protected void onCreate(Bundle savedInstanceState) { | |
mSendButton = (Button) findViewById(R.id.sendButton); | ||
mMessageEdit = (EditText) findViewById(R.id.messageEdit); | ||
|
||
mChatRef = FirebaseDatabase.getInstance().getReference().child("chats"); | ||
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); | ||
mChatIndicesRef = ref.child("chatIndices"); | ||
mChatRef = ref.child("chats"); | ||
|
||
mSendButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
|
@@ -78,11 +74,13 @@ public void onClick(View v) { | |
String name = "User " + uid.substring(0, 6); | ||
|
||
Chat chat = new Chat(name, mMessageEdit.getText().toString(), uid); | ||
mChatRef.push().setValue(chat, new DatabaseReference.CompletionListener() { | ||
DatabaseReference chatRef = mChatRef.push(); | ||
mChatIndicesRef.child(chatRef.getKey()).setValue(true); | ||
chatRef.setValue(chat, new DatabaseReference.CompletionListener() { | ||
@Override | ||
public void onComplete(DatabaseError databaseError, DatabaseReference reference) { | ||
if (databaseError != null) { | ||
Log.e(TAG, "Failed to write message", databaseError.toException()); | ||
public void onComplete(DatabaseError error, DatabaseReference reference) { | ||
if (error != null) { | ||
Log.e(TAG, "Failed to write message", error.toException()); | ||
} | ||
} | ||
}); | ||
|
@@ -92,11 +90,7 @@ public void onComplete(DatabaseError databaseError, DatabaseReference reference) | |
}); | ||
|
||
mMessages = (RecyclerView) findViewById(R.id.messagesList); | ||
|
||
mManager = new LinearLayoutManager(this); | ||
mManager.setReverseLayout(false); | ||
|
||
mMessages.setHasFixedSize(false); | ||
mMessages.setLayoutManager(mManager); | ||
} | ||
|
||
|
@@ -107,10 +101,10 @@ public void onStart() { | |
// Default Database rules do not allow unauthenticated reads, so we need to | ||
// sign in before attaching the RecyclerView adapter otherwise the Adapter will | ||
// not be able to read any data from the Database. | ||
if (!isSignedIn()) { | ||
signInAnonymously(); | ||
} else { | ||
if (isSignedIn()) { | ||
attachRecyclerViewAdapter(); | ||
} else { | ||
signInAnonymously(); | ||
} | ||
} | ||
|
||
|
@@ -137,28 +131,34 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | |
|
||
private void attachRecyclerViewAdapter() { | ||
Query lastFifty = mChatRef.limitToLast(50); | ||
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>( | ||
Chat.class, R.layout.message, ChatHolder.class, lastFifty) { | ||
|
||
@Override | ||
public void populateViewHolder(ChatHolder chatView, Chat chat, int position) { | ||
chatView.setName(chat.getName()); | ||
chatView.setText(chat.getMessage()); | ||
mRecyclerViewAdapter = | ||
new FirebaseIndexRecyclerAdapter<Chat, ChatHolder>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
Chat.class, | ||
R.layout.message, | ||
ChatHolder.class, | ||
mChatIndicesRef, | ||
lastFifty) { | ||
@Override | ||
public void populateViewHolder(ChatHolder chatView, Chat chat, int position) { | ||
chatView.setName(chat.getName()); | ||
chatView.setText(chat.getMessage()); | ||
|
||
FirebaseUser currentUser = mAuth.getCurrentUser(); | ||
if (currentUser != null && chat.getUid().equals(currentUser.getUid())) { | ||
chatView.setIsSender(true); | ||
} else { | ||
chatView.setIsSender(false); | ||
} | ||
} | ||
}; | ||
FirebaseUser currentUser = mAuth.getCurrentUser(); | ||
if (currentUser != null && chat.getUid().equals(currentUser.getUid())) { | ||
chatView.setIsSender(true); | ||
} else { | ||
chatView.setIsSender(false); | ||
} | ||
} | ||
}; | ||
|
||
// Scroll to bottom on new messages | ||
mRecyclerViewAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { | ||
@Override | ||
public void onItemRangeInserted(int positionStart, int itemCount) { | ||
mManager.smoothScrollToPosition(mMessages, null, mRecyclerViewAdapter.getItemCount()); | ||
mManager.smoothScrollToPosition(mMessages, | ||
null, | ||
mRecyclerViewAdapter.getItemCount()); | ||
} | ||
}); | ||
|
||
|
@@ -168,17 +168,25 @@ public void onItemRangeInserted(int positionStart, int itemCount) { | |
private void signInAnonymously() { | ||
Toast.makeText(this, "Signing in...", Toast.LENGTH_SHORT).show(); | ||
mAuth.signInAnonymously() | ||
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | ||
.addOnSuccessListener(this, new OnSuccessListener<AuthResult>() { | ||
@Override | ||
public void onSuccess(AuthResult result) { | ||
attachRecyclerViewAdapter(); | ||
} | ||
}) | ||
.addOnCompleteListener(new OnCompleteListener<AuthResult>() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making the Toast survive activity rotation. |
||
@Override | ||
public void onComplete(@NonNull Task<AuthResult> task) { | ||
Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful()); | ||
if (task.isSuccessful()) { | ||
Toast.makeText(ChatActivity.this, "Signed In", | ||
Toast.LENGTH_SHORT).show(); | ||
attachRecyclerViewAdapter(); | ||
Toast.makeText(getApplicationContext(), | ||
R.string.signed_in, | ||
Toast.LENGTH_SHORT) | ||
.show(); | ||
} else { | ||
Toast.makeText(ChatActivity.this, "Sign In Failed", | ||
Toast.LENGTH_SHORT).show(); | ||
Toast.makeText(getApplicationContext(), | ||
R.string.sign_in_failed, | ||
Toast.LENGTH_SHORT) | ||
.show(); | ||
} | ||
} | ||
}); | ||
|
@@ -193,96 +201,4 @@ public void updateUI() { | |
mSendButton.setEnabled(isSignedIn()); | ||
mMessageEdit.setEnabled(isSignedIn()); | ||
} | ||
|
||
public static class Chat { | ||
private String mName; | ||
private String mMessage; | ||
private String mUid; | ||
|
||
public Chat() { | ||
// Needed for Firebase | ||
} | ||
|
||
public Chat(String name, String message, String uid) { | ||
mName = name; | ||
mMessage = message; | ||
mUid = uid; | ||
} | ||
|
||
public String getName() { | ||
return mName; | ||
} | ||
|
||
public void setName(String name) { | ||
mName = name; | ||
} | ||
|
||
public String getMessage() { | ||
return mMessage; | ||
} | ||
|
||
public void setMessage(String message) { | ||
mMessage = message; | ||
} | ||
|
||
public String getUid() { | ||
return mUid; | ||
} | ||
|
||
public void setUid(String uid) { | ||
mUid = uid; | ||
} | ||
} | ||
|
||
public static class ChatHolder extends RecyclerView.ViewHolder { | ||
private final TextView mNameField; | ||
private final TextView mTextField; | ||
private final FrameLayout mLeftArrow; | ||
private final FrameLayout mRightArrow; | ||
private final RelativeLayout mMessageContainer; | ||
private final LinearLayout mMessage; | ||
private final int mGreen300; | ||
private final int mGray300; | ||
|
||
public ChatHolder(View itemView) { | ||
super(itemView); | ||
mNameField = (TextView) itemView.findViewById(R.id.name_text); | ||
mTextField = (TextView) itemView.findViewById(R.id.message_text); | ||
mLeftArrow = (FrameLayout) itemView.findViewById(R.id.left_arrow); | ||
mRightArrow = (FrameLayout) itemView.findViewById(R.id.right_arrow); | ||
mMessageContainer = (RelativeLayout) itemView.findViewById(R.id.message_container); | ||
mMessage = (LinearLayout) itemView.findViewById(R.id.message); | ||
mGreen300 = ContextCompat.getColor(itemView.getContext(), R.color.material_green_300); | ||
mGray300 = ContextCompat.getColor(itemView.getContext(), R.color.material_gray_300); | ||
} | ||
|
||
public void setIsSender(boolean isSender) { | ||
final int color; | ||
if (isSender) { | ||
color = mGreen300; | ||
mLeftArrow.setVisibility(View.GONE); | ||
mRightArrow.setVisibility(View.VISIBLE); | ||
mMessageContainer.setGravity(Gravity.END); | ||
} else { | ||
color = mGray300; | ||
mLeftArrow.setVisibility(View.VISIBLE); | ||
mRightArrow.setVisibility(View.GONE); | ||
mMessageContainer.setGravity(Gravity.START); | ||
} | ||
|
||
((GradientDrawable) mMessage.getBackground()).setColor(color); | ||
((RotateDrawable) mLeftArrow.getBackground()).getDrawable() | ||
.setColorFilter(color, PorterDuff.Mode.SRC); | ||
((RotateDrawable) mRightArrow.getBackground()).getDrawable() | ||
.setColorFilter(color, PorterDuff.Mode.SRC); | ||
} | ||
|
||
public void setName(String name) { | ||
mNameField.setText(name); | ||
} | ||
|
||
public void setText(String text) { | ||
mTextField.setText(text); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.firebase.uidemo.database; | ||
|
||
import android.graphics.PorterDuff; | ||
import android.graphics.drawable.GradientDrawable; | ||
import android.graphics.drawable.RotateDrawable; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.Gravity; | ||
import android.view.View; | ||
import android.widget.FrameLayout; | ||
import android.widget.LinearLayout; | ||
import android.widget.RelativeLayout; | ||
import android.widget.TextView; | ||
|
||
import com.firebase.uidemo.R; | ||
|
||
public class ChatHolder extends RecyclerView.ViewHolder { | ||
private final TextView mNameField; | ||
private final TextView mTextField; | ||
private final FrameLayout mLeftArrow; | ||
private final FrameLayout mRightArrow; | ||
private final RelativeLayout mMessageContainer; | ||
private final LinearLayout mMessage; | ||
private final int mGreen300; | ||
private final int mGray300; | ||
|
||
public ChatHolder(View itemView) { | ||
super(itemView); | ||
mNameField = (TextView) itemView.findViewById(R.id.name_text); | ||
mTextField = (TextView) itemView.findViewById(R.id.message_text); | ||
mLeftArrow = (FrameLayout) itemView.findViewById(R.id.left_arrow); | ||
mRightArrow = (FrameLayout) itemView.findViewById(R.id.right_arrow); | ||
mMessageContainer = (RelativeLayout) itemView.findViewById(R.id.message_container); | ||
mMessage = (LinearLayout) itemView.findViewById(R.id.message); | ||
mGreen300 = ContextCompat.getColor(itemView.getContext(), R.color.material_green_300); | ||
mGray300 = ContextCompat.getColor(itemView.getContext(), R.color.material_gray_300); | ||
} | ||
|
||
public void setIsSender(boolean isSender) { | ||
final int color; | ||
if (isSender) { | ||
color = mGreen300; | ||
mLeftArrow.setVisibility(View.GONE); | ||
mRightArrow.setVisibility(View.VISIBLE); | ||
mMessageContainer.setGravity(Gravity.END); | ||
} else { | ||
color = mGray300; | ||
mLeftArrow.setVisibility(View.VISIBLE); | ||
mRightArrow.setVisibility(View.GONE); | ||
mMessageContainer.setGravity(Gravity.START); | ||
} | ||
|
||
((GradientDrawable) mMessage.getBackground()).setColor(color); | ||
((RotateDrawable) mLeftArrow.getBackground()).getDrawable() | ||
.setColorFilter(color, PorterDuff.Mode.SRC); | ||
((RotateDrawable) mRightArrow.getBackground()).getDrawable() | ||
.setColorFilter(color, PorterDuff.Mode.SRC); | ||
} | ||
|
||
public void setName(String name) { | ||
mNameField.setText(name); | ||
} | ||
|
||
public void setText(String text) { | ||
mTextField.setText(text); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I extracted
Chat
andChatHolder
to simplifyChatActivity
.