Skip to content

Commit c91cc64

Browse files
SUPERCILEXsamtstern
authored andcommitted
Database sample cleanup (#545)
1 parent 4bc45af commit c91cc64

File tree

4 files changed

+132
-127
lines changed

4 files changed

+132
-127
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.firebase.uidemo.database;
2+
3+
public class Chat {
4+
private String mName;
5+
private String mMessage;
6+
private String mUid;
7+
8+
public Chat() {
9+
// Needed for Firebase
10+
}
11+
12+
public Chat(String name, String message, String uid) {
13+
mName = name;
14+
mMessage = message;
15+
mUid = uid;
16+
}
17+
18+
public String getName() {
19+
return mName;
20+
}
21+
22+
public void setName(String name) {
23+
mName = name;
24+
}
25+
26+
public String getMessage() {
27+
return mMessage;
28+
}
29+
30+
public void setMessage(String message) {
31+
mMessage = message;
32+
}
33+
34+
public String getUid() {
35+
return mUid;
36+
}
37+
38+
public void setUid(String uid) {
39+
mUid = uid;
40+
}
41+
}

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

Lines changed: 21 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,15 @@
1515
package com.firebase.uidemo.database;
1616

1717
import android.content.Context;
18-
import android.graphics.PorterDuff;
19-
import android.graphics.drawable.GradientDrawable;
20-
import android.graphics.drawable.RotateDrawable;
2118
import android.os.Bundle;
2219
import android.support.annotation.NonNull;
23-
import android.support.v4.content.ContextCompat;
2420
import android.support.v7.app.AppCompatActivity;
2521
import android.support.v7.widget.LinearLayoutManager;
2622
import android.support.v7.widget.RecyclerView;
2723
import android.util.Log;
28-
import android.view.Gravity;
2924
import android.view.View;
3025
import android.widget.Button;
3126
import android.widget.EditText;
32-
import android.widget.FrameLayout;
33-
import android.widget.LinearLayout;
34-
import android.widget.RelativeLayout;
3527
import android.widget.TextView;
3628
import android.widget.Toast;
3729

@@ -48,7 +40,6 @@
4840
import com.google.firebase.database.FirebaseDatabase;
4941
import com.google.firebase.database.Query;
5042

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

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

6152
private RecyclerView mMessages;
6253
private LinearLayoutManager mManager;
63-
private FirebaseRecyclerAdapter<Chat, ChatHolder> mRecyclerViewAdapter;
64-
private View mEmptyListView;
54+
private FirebaseRecyclerAdapter<Chat, ChatHolder> mAdapter;
55+
private TextView mEmptyListMessage;
6556

6657
@Override
6758
protected void onCreate(Bundle savedInstanceState) {
@@ -73,8 +64,7 @@ protected void onCreate(Bundle savedInstanceState) {
7364

7465
mSendButton = (Button) findViewById(R.id.sendButton);
7566
mMessageEdit = (EditText) findViewById(R.id.messageEdit);
76-
77-
mEmptyListView = findViewById(R.id.emptyTextView);
67+
mEmptyListMessage = (TextView) findViewById(R.id.emptyTextView);
7868

7969
mRef = FirebaseDatabase.getInstance().getReference();
8070
mChatRef = mRef.child("chats");
@@ -114,18 +104,18 @@ public void onStart() {
114104
// Default Database rules do not allow unauthenticated reads, so we need to
115105
// sign in before attaching the RecyclerView adapter otherwise the Adapter will
116106
// not be able to read any data from the Database.
117-
if (!isSignedIn()) {
118-
signInAnonymously();
119-
} else {
107+
if (isSignedIn()) {
120108
attachRecyclerViewAdapter();
109+
} else {
110+
signInAnonymously();
121111
}
122112
}
123113

124114
@Override
125115
public void onStop() {
126116
super.onStop();
127-
if (mRecyclerViewAdapter != null) {
128-
mRecyclerViewAdapter.cleanup();
117+
if (mAdapter != null) {
118+
mAdapter.cleanup();
129119
}
130120
}
131121

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

145135
private void attachRecyclerViewAdapter() {
146136
Query lastFifty = mChatRef.limitToLast(50);
147-
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
137+
mAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
148138
Chat.class, R.layout.message, ChatHolder.class, lastFifty) {
149139
@Override
150-
public void populateViewHolder(ChatHolder chatView, Chat chat, int position) {
151-
chatView.setName(chat.getName());
152-
chatView.setText(chat.getMessage());
140+
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
141+
holder.setName(chat.getName());
142+
holder.setText(chat.getMessage());
153143

154144
FirebaseUser currentUser = mAuth.getCurrentUser();
155145
if (currentUser != null && chat.getUid().equals(currentUser.getUid())) {
156-
chatView.setIsSender(true);
146+
holder.setIsSender(true);
157147
} else {
158-
chatView.setIsSender(false);
148+
holder.setIsSender(false);
159149
}
160150
}
161151

162152
@Override
163153
protected void onDataChanged() {
164-
// if there are no chat messages, show a view that invites the user to add a message
165-
mEmptyListView.setVisibility(mRecyclerViewAdapter.getItemCount() == 0 ?
166-
View.VISIBLE : View.INVISIBLE);
154+
// If there are no chat messages, show a view that invites the user to add a message.
155+
mEmptyListMessage.setVisibility(mAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
167156
}
168157
};
169158

170159
// Scroll to bottom on new messages
171-
mRecyclerViewAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
160+
mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
172161
@Override
173162
public void onItemRangeInserted(int positionStart, int itemCount) {
174-
mManager.smoothScrollToPosition(mMessages, null, mRecyclerViewAdapter.getItemCount());
163+
mManager.smoothScrollToPosition(mMessages, null, mAdapter.getItemCount());
175164
}
176165
});
177166

178-
mMessages.setAdapter(mRecyclerViewAdapter);
167+
mMessages.setAdapter(mAdapter);
179168
}
180169

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

193-
public boolean isSignedIn() {
182+
private boolean isSignedIn() {
194183
return mAuth.getCurrentUser() != null;
195184
}
196185

197-
public void updateUI() {
186+
private void updateUI() {
198187
// Sending only allowed when signed in
199188
mSendButton.setEnabled(isSignedIn());
200189
mMessageEdit.setEnabled(isSignedIn());
201190
}
202191

203-
public static class Chat {
204-
private String mName;
205-
private String mMessage;
206-
private String mUid;
207-
208-
public Chat() {
209-
// Needed for Firebase
210-
}
211-
212-
public Chat(String name, String message, String uid) {
213-
mName = name;
214-
mMessage = message;
215-
mUid = uid;
216-
}
217-
218-
public String getName() {
219-
return mName;
220-
}
221-
222-
public void setName(String name) {
223-
mName = name;
224-
}
225-
226-
public String getMessage() {
227-
return mMessage;
228-
}
229-
230-
public void setMessage(String message) {
231-
mMessage = message;
232-
}
233-
234-
public String getUid() {
235-
return mUid;
236-
}
237-
238-
public void setUid(String uid) {
239-
mUid = uid;
240-
}
241-
}
242-
243-
public static class ChatHolder extends RecyclerView.ViewHolder {
244-
private final TextView mNameField;
245-
private final TextView mTextField;
246-
private final FrameLayout mLeftArrow;
247-
private final FrameLayout mRightArrow;
248-
private final RelativeLayout mMessageContainer;
249-
private final LinearLayout mMessage;
250-
private final int mGreen300;
251-
private final int mGray300;
252-
253-
public ChatHolder(View itemView) {
254-
super(itemView);
255-
mNameField = (TextView) itemView.findViewById(R.id.name_text);
256-
mTextField = (TextView) itemView.findViewById(R.id.message_text);
257-
mLeftArrow = (FrameLayout) itemView.findViewById(R.id.left_arrow);
258-
mRightArrow = (FrameLayout) itemView.findViewById(R.id.right_arrow);
259-
mMessageContainer = (RelativeLayout) itemView.findViewById(R.id.message_container);
260-
mMessage = (LinearLayout) itemView.findViewById(R.id.message);
261-
mGreen300 = ContextCompat.getColor(itemView.getContext(), R.color.material_green_300);
262-
mGray300 = ContextCompat.getColor(itemView.getContext(), R.color.material_gray_300);
263-
}
264-
265-
public void setIsSender(boolean isSender) {
266-
final int color;
267-
if (isSender) {
268-
color = mGreen300;
269-
mLeftArrow.setVisibility(View.GONE);
270-
mRightArrow.setVisibility(View.VISIBLE);
271-
mMessageContainer.setGravity(Gravity.END);
272-
} else {
273-
color = mGray300;
274-
mLeftArrow.setVisibility(View.VISIBLE);
275-
mRightArrow.setVisibility(View.GONE);
276-
mMessageContainer.setGravity(Gravity.START);
277-
}
278-
279-
((GradientDrawable) mMessage.getBackground()).setColor(color);
280-
((RotateDrawable) mLeftArrow.getBackground()).getDrawable()
281-
.setColorFilter(color, PorterDuff.Mode.SRC);
282-
((RotateDrawable) mRightArrow.getBackground()).getDrawable()
283-
.setColorFilter(color, PorterDuff.Mode.SRC);
284-
}
285-
286-
public void setName(String name) {
287-
mNameField.setText(name);
288-
}
289-
290-
public void setText(String text) {
291-
mTextField.setText(text);
292-
}
293-
}
294-
295192
/**
296193
* Notifies the user of sign in successes or failures beyond the lifecycle of an activity.
297194
*/
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.firebase.uidemo.database;
2+
3+
import android.graphics.PorterDuff;
4+
import android.graphics.drawable.GradientDrawable;
5+
import android.graphics.drawable.RotateDrawable;
6+
import android.support.v4.content.ContextCompat;
7+
import android.support.v7.widget.RecyclerView;
8+
import android.view.Gravity;
9+
import android.view.View;
10+
import android.widget.FrameLayout;
11+
import android.widget.LinearLayout;
12+
import android.widget.RelativeLayout;
13+
import android.widget.TextView;
14+
15+
import com.firebase.uidemo.R;
16+
17+
public class ChatHolder extends RecyclerView.ViewHolder {
18+
private final TextView mNameField;
19+
private final TextView mTextField;
20+
private final FrameLayout mLeftArrow;
21+
private final FrameLayout mRightArrow;
22+
private final RelativeLayout mMessageContainer;
23+
private final LinearLayout mMessage;
24+
private final int mGreen300;
25+
private final int mGray300;
26+
27+
public ChatHolder(View itemView) {
28+
super(itemView);
29+
mNameField = (TextView) itemView.findViewById(R.id.name_text);
30+
mTextField = (TextView) itemView.findViewById(R.id.message_text);
31+
mLeftArrow = (FrameLayout) itemView.findViewById(R.id.left_arrow);
32+
mRightArrow = (FrameLayout) itemView.findViewById(R.id.right_arrow);
33+
mMessageContainer = (RelativeLayout) itemView.findViewById(R.id.message_container);
34+
mMessage = (LinearLayout) itemView.findViewById(R.id.message);
35+
mGreen300 = ContextCompat.getColor(itemView.getContext(), R.color.material_green_300);
36+
mGray300 = ContextCompat.getColor(itemView.getContext(), R.color.material_gray_300);
37+
}
38+
39+
public void setIsSender(boolean isSender) {
40+
final int color;
41+
if (isSender) {
42+
color = mGreen300;
43+
mLeftArrow.setVisibility(View.GONE);
44+
mRightArrow.setVisibility(View.VISIBLE);
45+
mMessageContainer.setGravity(Gravity.END);
46+
} else {
47+
color = mGray300;
48+
mLeftArrow.setVisibility(View.VISIBLE);
49+
mRightArrow.setVisibility(View.GONE);
50+
mMessageContainer.setGravity(Gravity.START);
51+
}
52+
53+
((GradientDrawable) mMessage.getBackground()).setColor(color);
54+
((RotateDrawable) mLeftArrow.getBackground()).getDrawable()
55+
.setColorFilter(color, PorterDuff.Mode.SRC);
56+
((RotateDrawable) mRightArrow.getBackground()).getDrawable()
57+
.setColorFilter(color, PorterDuff.Mode.SRC);
58+
}
59+
60+
public void setName(String name) {
61+
mNameField.setText(name);
62+
}
63+
64+
public void setText(String text) {
65+
mTextField.setText(text);
66+
}
67+
}

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464
<string name="choose_image">Choose Image</string>
6565
<string name="accessibility_downloaded_image">Downloaded image</string>
6666
<string name="drive_file">Drive File</string>
67+
<string name="allow_new_email_acccount">Allow account creation if email does not exist.</string>
6768

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

0 commit comments

Comments
 (0)