Skip to content

Instantly share code, notes, and snippets.

Created September 22, 2017 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4fb4f0bab87a47ed9c97758ccf585b28 to your computer and use it in GitHub Desktop.
Save anonymous/4fb4f0bab87a47ed9c97758ccf585b28 to your computer and use it in GitHub Desktop.
public class MyChatsAdapter extends ArrayAdapter<ChatInfo> {
private FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
private DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference();
final private String currentUID = currentUser.getUid();
public MyChatsAdapter(Activity context, List<ChatInfo> chatItems) {
super(context, 0, chatItems); // 2nd par is 0 because we inflate manually
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.my_chats_item, parent, false);
}
final ChatInfo currentChatInfo = getItem(position);
if (currentChatInfo != null) {
String chatID = currentChatInfo.getChatRoomName();
if (chatID != null) {
final TextView lastMessageTextView = (TextView) listItemView.findViewById(R.id.last_message_text_view);
final ImageView newMessageCircleImageView = (ImageView) listItemView.findViewById(R.id.new_message_circle_image_view);
final TextView lastMessageTimeStampTextView = (TextView) listItemView.findViewById(R.id.last_message_stamp);
newMessageCircleImageView.setVisibility(View.INVISIBLE);
mDatabaseReference.child("chat_info").child(chatID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String last_message = dataSnapshot.child("lastMessage").getValue().toString();
lastMessageTextView.setText(last_message);
long lastMessageTimeStamp = currentChatInfo.getLastMessageTimeStamp();
long currentTimeStamp = System.currentTimeMillis();
lastMessageTimeStampTextView.setText(TimestampUtils.toDuration(currentTimeStamp - lastMessageTimeStamp));
if (dataSnapshot.hasChild(currentUID)) {
long lastUserTimeStamp = (long) dataSnapshot.child(currentUID).getValue();
// long lastMessageTimeStamp = (long) dataSnapshot.child("lastStamp").getValue();
if (lastUserTimeStamp >= lastMessageTimeStamp) {
newMessageCircleImageView.setVisibility(View.INVISIBLE);
lastMessageTextView.setTypeface(Typeface.DEFAULT);
} else {
lastMessageTextView.setTypeface(Typeface.DEFAULT_BOLD);
newMessageCircleImageView.setVisibility(View.VISIBLE);
}
} else {
lastMessageTextView.setTypeface(Typeface.DEFAULT_BOLD);
newMessageCircleImageView.setVisibility(View.VISIBLE);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
String chatRoomEdit = chatID.replace(currentUID, "");
final String partnerID = chatRoomEdit.replace("_", "");
final TextView PartnerUsernameTextView = (TextView) listItemView.findViewById(R.id.my_chat_partner);
final ImageView userIconImageview = (ImageView) listItemView.findViewById(R.id.user_icon);
mDatabaseReference.child("user-books").child(partnerID).child("userName").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
String PartenerUserName = dataSnapshot.getValue().toString();
PartnerUsernameTextView.setText(PartenerUserName);
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
int color = generator.getColor(PartenerUserName);
TextDrawable drawable = TextDrawable.builder()
.buildRound(PartenerUserName.substring(0, 1).toUpperCase(), color);
userIconImageview.setImageDrawable(drawable);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
final RelativeLayout myChatLayout = (RelativeLayout) listItemView.findViewById(R.id.my_chats_linear_layout);
myChatLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ChatActivity.class);
intent.putExtra("OWNER_ID", partnerID);
v.getContext().startActivity(intent);
}
});
}
}
return listItemView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment