|
| 1 | +package com.firebase.uidemo; |
| 2 | + |
| 3 | +import android.graphics.PorterDuff; |
| 4 | +import android.graphics.drawable.GradientDrawable; |
| 5 | +import android.graphics.drawable.RotateDrawable; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.support.annotation.NonNull; |
| 8 | +import android.support.v4.content.ContextCompat; |
| 9 | +import android.support.v7.app.AppCompatActivity; |
| 10 | +import android.support.v7.widget.LinearLayoutManager; |
| 11 | +import android.support.v7.widget.RecyclerView; |
| 12 | +import android.util.Log; |
| 13 | +import android.view.Gravity; |
| 14 | +import android.view.Menu; |
| 15 | +import android.view.MenuInflater; |
| 16 | +import android.view.MenuItem; |
| 17 | +import android.view.View; |
| 18 | +import android.widget.Button; |
| 19 | +import android.widget.EditText; |
| 20 | +import android.widget.FrameLayout; |
| 21 | +import android.widget.LinearLayout; |
| 22 | +import android.widget.RelativeLayout; |
| 23 | +import android.widget.TextView; |
| 24 | +import android.widget.Toast; |
| 25 | + |
| 26 | +import com.firebase.ui.database.FirebaseRecyclerAdapter; |
| 27 | +import com.google.android.gms.tasks.OnCompleteListener; |
| 28 | +import com.google.android.gms.tasks.Task; |
| 29 | +import com.google.firebase.auth.AuthResult; |
| 30 | +import com.google.firebase.auth.FirebaseAuth; |
| 31 | +import com.google.firebase.auth.FirebaseUser; |
| 32 | +import com.google.firebase.database.DatabaseError; |
| 33 | +import com.google.firebase.database.DatabaseReference; |
| 34 | +import com.google.firebase.database.FirebaseDatabase; |
| 35 | +import com.google.firebase.database.Query; |
| 36 | + |
| 37 | +public class ChatActivity extends AppCompatActivity { |
| 38 | + |
| 39 | + public static final String TAG = "RecyclerViewDemo"; |
| 40 | + |
| 41 | + private FirebaseAuth mAuth; |
| 42 | + private DatabaseReference mRef; |
| 43 | + private Query mChatRef; |
| 44 | + private Button mSendButton; |
| 45 | + private EditText mMessageEdit; |
| 46 | + |
| 47 | + private RecyclerView mMessages; |
| 48 | + private FirebaseRecyclerAdapter<Chat, ChatHolder> mRecyclerViewAdapter; |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void onCreate(Bundle savedInstanceState) { |
| 52 | + super.onCreate(savedInstanceState); |
| 53 | + setContentView(R.layout.activity_chat); |
| 54 | + |
| 55 | + mAuth = FirebaseAuth.getInstance(); |
| 56 | + mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() { |
| 57 | + @Override |
| 58 | + public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { |
| 59 | + invalidateOptionsMenu(); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + mSendButton = (Button) findViewById(R.id.sendButton); |
| 64 | + mMessageEdit = (EditText) findViewById(R.id.messageEdit); |
| 65 | + |
| 66 | + mRef = FirebaseDatabase.getInstance().getReference(); |
| 67 | + mChatRef = mRef.limitToLast(50); |
| 68 | + |
| 69 | + mSendButton.setOnClickListener(new View.OnClickListener() { |
| 70 | + @Override |
| 71 | + public void onClick(View v) { |
| 72 | + String uid = mAuth.getCurrentUser().getUid(); |
| 73 | + String name = "User " + uid.substring(0, 6); |
| 74 | + |
| 75 | + Chat chat = new Chat(name, uid, mMessageEdit.getText().toString()); |
| 76 | + mRef.push().setValue(chat, new DatabaseReference.CompletionListener() { |
| 77 | + @Override |
| 78 | + public void onComplete(DatabaseError databaseError, DatabaseReference reference) { |
| 79 | + if (databaseError != null) { |
| 80 | + Log.e(TAG, "Failed to write message", databaseError.toException()); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + mMessageEdit.setText(""); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + mMessages = (RecyclerView) findViewById(R.id.messagesList); |
| 90 | + |
| 91 | + LinearLayoutManager manager = new LinearLayoutManager(this); |
| 92 | + manager.setReverseLayout(false); |
| 93 | + |
| 94 | + mMessages.setHasFixedSize(false); |
| 95 | + mMessages.setLayoutManager(manager); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onStart() { |
| 100 | + super.onStart(); |
| 101 | + |
| 102 | + mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>( |
| 103 | + Chat.class, R.layout.message, ChatHolder.class, mChatRef) { |
| 104 | + |
| 105 | + @Override |
| 106 | + public void populateViewHolder(ChatHolder chatView, Chat chat, int position) { |
| 107 | + chatView.setName(chat.getName()); |
| 108 | + chatView.setText(chat.getText()); |
| 109 | + |
| 110 | + FirebaseUser currentUser = mAuth.getCurrentUser(); |
| 111 | + if (currentUser != null && chat.getUid().equals(currentUser.getUid())) { |
| 112 | + chatView.setIsSender(true); |
| 113 | + } else { |
| 114 | + chatView.setIsSender(false); |
| 115 | + } |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + mMessages.setAdapter(mRecyclerViewAdapter); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onStop() { |
| 124 | + super.onStop(); |
| 125 | + |
| 126 | + mRecyclerViewAdapter.cleanup(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 131 | + MenuInflater inflater = getMenuInflater(); |
| 132 | + inflater.inflate(R.menu.chat_login_menu, menu); |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public boolean onPrepareOptionsMenu(Menu menu) { |
| 138 | + menu.findItem(R.id.login_menu_item).setVisible(!isSignedIn()); |
| 139 | + menu.findItem(R.id.logout_menu_item).setVisible(isSignedIn()); |
| 140 | + |
| 141 | + mSendButton.setEnabled(isSignedIn()); |
| 142 | + mMessageEdit.setEnabled(isSignedIn()); |
| 143 | + |
| 144 | + return true; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 149 | + switch (item.getItemId()) { |
| 150 | + case R.id.login_menu_item: |
| 151 | + signInAnonymously(); |
| 152 | + return true; |
| 153 | + case R.id.logout_menu_item: |
| 154 | + mAuth.signOut(); |
| 155 | + return true; |
| 156 | + } |
| 157 | + |
| 158 | + return super.onOptionsItemSelected(item); |
| 159 | + } |
| 160 | + |
| 161 | + private void signInAnonymously() { |
| 162 | + mAuth.signInAnonymously() |
| 163 | + .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { |
| 164 | + @Override |
| 165 | + public void onComplete(@NonNull Task<AuthResult> task) { |
| 166 | + Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful()); |
| 167 | + if (task.isSuccessful()) { |
| 168 | + Toast.makeText(ChatActivity.this, "Signed In", |
| 169 | + Toast.LENGTH_SHORT).show(); |
| 170 | + } else { |
| 171 | + Toast.makeText(ChatActivity.this, "Sign In Failed", |
| 172 | + Toast.LENGTH_SHORT).show(); |
| 173 | + } |
| 174 | + } |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + public boolean isSignedIn() { |
| 179 | + return (mAuth.getCurrentUser() != null); |
| 180 | + } |
| 181 | + |
| 182 | + public static class Chat { |
| 183 | + |
| 184 | + String name; |
| 185 | + String text; |
| 186 | + String uid; |
| 187 | + |
| 188 | + public Chat() { |
| 189 | + } |
| 190 | + |
| 191 | + public Chat(String name, String uid, String message) { |
| 192 | + this.name = name; |
| 193 | + this.text = message; |
| 194 | + this.uid = uid; |
| 195 | + } |
| 196 | + |
| 197 | + public String getName() { |
| 198 | + return name; |
| 199 | + } |
| 200 | + |
| 201 | + public String getUid() { |
| 202 | + return uid; |
| 203 | + } |
| 204 | + |
| 205 | + public String getText() { |
| 206 | + return text; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + public static class ChatHolder extends RecyclerView.ViewHolder { |
| 211 | + View mView; |
| 212 | + |
| 213 | + public ChatHolder(View itemView) { |
| 214 | + super(itemView); |
| 215 | + mView = itemView; |
| 216 | + } |
| 217 | + |
| 218 | + public void setIsSender(Boolean isSender) { |
| 219 | + FrameLayout left_arrow = (FrameLayout) mView.findViewById(R.id.left_arrow); |
| 220 | + FrameLayout right_arrow = (FrameLayout) mView.findViewById(R.id.right_arrow); |
| 221 | + RelativeLayout messageContainer = (RelativeLayout) mView.findViewById(R.id.message_container); |
| 222 | + LinearLayout message = (LinearLayout) mView.findViewById(R.id.message); |
| 223 | + |
| 224 | + int color; |
| 225 | + if (isSender) { |
| 226 | + color = ContextCompat.getColor(mView.getContext(), R.color.green_300); |
| 227 | + |
| 228 | + left_arrow.setVisibility(View.GONE); |
| 229 | + right_arrow.setVisibility(View.VISIBLE); |
| 230 | + messageContainer.setGravity(Gravity.RIGHT); |
| 231 | + } else { |
| 232 | + color = ContextCompat.getColor(mView.getContext(), R.color.grey_300); |
| 233 | + |
| 234 | + left_arrow.setVisibility(View.VISIBLE); |
| 235 | + right_arrow.setVisibility(View.GONE); |
| 236 | + messageContainer.setGravity(Gravity.LEFT); |
| 237 | + } |
| 238 | + |
| 239 | + ((GradientDrawable) message.getBackground()).setColor(color); |
| 240 | + ((RotateDrawable) left_arrow.getBackground()).getDrawable() |
| 241 | + .setColorFilter(color, PorterDuff.Mode.SRC); |
| 242 | + ((RotateDrawable) right_arrow.getBackground()).getDrawable() |
| 243 | + .setColorFilter(color, PorterDuff.Mode.SRC); |
| 244 | + } |
| 245 | + |
| 246 | + public void setName(String name) { |
| 247 | + TextView field = (TextView) mView.findViewById(R.id.name_text); |
| 248 | + field.setText(name); |
| 249 | + } |
| 250 | + |
| 251 | + public void setText(String text) { |
| 252 | + TextView field = (TextView) mView.findViewById(R.id.message_text); |
| 253 | + field.setText(text); |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments