package com.quickblox.sample.chat.ui.adapter; import android.content.Context; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.quickblox.chat.model.QBChatDialog; import com.quickblox.chat.model.QBDialogType; import com.quickblox.sample.chat.R; import com.quickblox.sample.chat.utils.qb.QbDialogUtils; import com.quickblox.sample.core.ui.adapter.BaseSelectableListAdapter; import com.quickblox.sample.core.utils.ResourceUtils; import com.quickblox.sample.core.utils.UiUtils; import java.util.List; public class DialogsAdapter extends BaseSelectableListAdapter<QBChatDialog> { private static final String EMPTY_STRING = ""; public DialogsAdapter(Context context, List<QBChatDialog> dialogs) { super(context, dialogs); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.list_item_dialog, parent, false); holder = new ViewHolder(); holder.rootLayout = (ViewGroup) convertView.findViewById(R.id.root); holder.nameTextView = (TextView) convertView.findViewById(R.id.text_dialog_name); holder.lastMessageTextView = (TextView) convertView.findViewById(R.id.text_dialog_last_message); holder.dialogImageView = (ImageView) convertView.findViewById(R.id.image_dialog_icon); holder.unreadCounterTextView = (TextView) convertView.findViewById(R.id.text_dialog_unread_count); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } QBChatDialog dialog = getItem(position); if (dialog.getType().equals(QBDialogType.GROUP)) { holder.dialogImageView.setBackgroundDrawable(UiUtils.getGreyCircleDrawable()); holder.dialogImageView.setImageResource(R.drawable.ic_chat_group); } else { holder.dialogImageView.setBackgroundDrawable(UiUtils.getColorCircleDrawable(position)); holder.dialogImageView.setImageDrawable(null); } holder.nameTextView.setText(QbDialogUtils.getDialogName(dialog)); holder.lastMessageTextView.setText(prepareTextLastMessage(dialog)); int unreadMessagesCount = getUnreadMsgCount(dialog); if (unreadMessagesCount == 0) { holder.unreadCounterTextView.setVisibility(View.GONE); } else { holder.unreadCounterTextView.setVisibility(View.VISIBLE); holder.unreadCounterTextView.setText(String.valueOf(unreadMessagesCount > 99 ? 99 : unreadMessagesCount)); } holder.rootLayout.setBackgroundColor(isItemSelected(position) ? ResourceUtils.getColor(R.color.selected_list_item_color) : ResourceUtils.getColor(android.R.color.transparent)); return convertView; } private int getUnreadMsgCount(QBChatDialog chatDialog){ Integer unreadMessageCount = chatDialog.getUnreadMessageCount(); if (unreadMessageCount == null) { return 0; } else { return unreadMessageCount; } } private boolean isLastMessageAttachment(QBChatDialog dialog) { String lastMessage = dialog.getLastMessage(); Integer lastMessageSenderId = dialog.getLastMessageUserId(); return TextUtils.isEmpty(lastMessage) && lastMessageSenderId != null; } private String prepareTextLastMessage(QBChatDialog chatDialog){ if (isLastMessageAttachment(chatDialog)){ return context.getString(R.string.chat_attachment); } else { return chatDialog.getLastMessage(); } } private static class ViewHolder { ViewGroup rootLayout; ImageView dialogImageView; TextView nameTextView; TextView lastMessageTextView; TextView unreadCounterTextView; } }