Skip to content

db sample cleanup #545

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

Merged
merged 5 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app/src/main/java/com/firebase/uidemo/database/Chat.java
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;
}
}
145 changes: 21 additions & 124 deletions app/src/main/java/com/firebase/uidemo/database/ChatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,15 @@
package com.firebase.uidemo.database;

import android.content.Context;
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;

Expand All @@ -48,7 +40,6 @@
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

@SuppressWarnings("LogConditional")
public class ChatActivity extends AppCompatActivity implements FirebaseAuth.AuthStateListener {
private static final String TAG = "RecyclerViewDemo";

Expand All @@ -60,8 +51,8 @@ public class ChatActivity extends AppCompatActivity implements FirebaseAuth.Auth

private RecyclerView mMessages;
private LinearLayoutManager mManager;
private FirebaseRecyclerAdapter<Chat, ChatHolder> mRecyclerViewAdapter;
private View mEmptyListView;
private FirebaseRecyclerAdapter<Chat, ChatHolder> mAdapter;
private TextView mEmptyListMessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -73,8 +64,7 @@ protected void onCreate(Bundle savedInstanceState) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confusing name: empty listView or emptyList view.

mSendButton = (Button) findViewById(R.id.sendButton);
mMessageEdit = (EditText) findViewById(R.id.messageEdit);

mEmptyListView = findViewById(R.id.emptyTextView);
mEmptyListMessage = (TextView) findViewById(R.id.emptyTextView);

mRef = FirebaseDatabase.getInstance().getReference();
mChatRef = mRef.child("chats");
Expand Down Expand Up @@ -114,18 +104,18 @@ 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();
}
}

@Override
public void onStop() {
super.onStop();
if (mRecyclerViewAdapter != null) {
mRecyclerViewAdapter.cleanup();
if (mAdapter != null) {
mAdapter.cleanup();
}
}

Expand All @@ -144,38 +134,37 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

private void attachRecyclerViewAdapter() {
Query lastFifty = mChatRef.limitToLast(50);
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
mAdapter = 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());
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
holder.setName(chat.getName());
holder.setText(chat.getMessage());

FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null && chat.getUid().equals(currentUser.getUid())) {
chatView.setIsSender(true);
holder.setIsSender(true);
} else {
chatView.setIsSender(false);
holder.setIsSender(false);
}
}

@Override
protected void onDataChanged() {
// if there are no chat messages, show a view that invites the user to add a message
mEmptyListView.setVisibility(mRecyclerViewAdapter.getItemCount() == 0 ?
View.VISIBLE : View.INVISIBLE);
// If there are no chat messages, show a view that invites the user to add a message.
mEmptyListMessage.setVisibility(mAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
}
};

// Scroll to bottom on new messages
mRecyclerViewAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
mManager.smoothScrollToPosition(mMessages, null, mRecyclerViewAdapter.getItemCount());
mManager.smoothScrollToPosition(mMessages, null, mAdapter.getItemCount());
}
});

mMessages.setAdapter(mRecyclerViewAdapter);
mMessages.setAdapter(mAdapter);
}

private void signInAnonymously() {
Expand All @@ -190,108 +179,16 @@ public void onSuccess(AuthResult result) {
.addOnCompleteListener(new SignInResultNotifier(this));
}

public boolean isSignedIn() {
private boolean isSignedIn() {
return mAuth.getCurrentUser() != null;
}

public void updateUI() {
private void updateUI() {
// Sending only allowed when signed in
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);
}
}

/**
* Notifies the user of sign in successes or failures beyond the lifecycle of an activity.
*/
Expand Down
67 changes: 67 additions & 0 deletions app/src/main/java/com/firebase/uidemo/database/ChatHolder.java
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);
}
}
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
<string name="choose_image">Choose Image</string>
<string name="accessibility_downloaded_image">Downloaded image</string>
<string name="drive_file">Drive File</string>
<string name="allow_new_email_acccount">Allow account creation if email does not exist.</string>

<!-- strings for Auth UI demo activities -->
<!-- strings for database demo activities -->
<string name="start_chatting">No messages. Start chatting at the bottom!</string>
<string name="signed_in">Signed In</string>
<string name="sign_in_failed">Sign In Failed</string>
<string name="allow_new_email_acccount">Allow account creation if email does not exist.</string>
<string name="start_chatting">No messages. Start chatting at the bottom!</string>
</resources>