From f2d2966b316666e04f70ea8db6fb3b6c6232efb5 Mon Sep 17 00:00:00 2001 From: Haresh Khanna Date: Fri, 3 Mar 2017 02:54:58 +0530 Subject: [PATCH] Squash : Adds number of unread messages for every conversation. Fixes #2181 --- .../ui/adapter/ConversationAdapter.java | 9 +++ .../ui/widget/UnreadCountCustomView.java | 76 +++++++++++++++++++ src/main/res/layout/conversation_list_row.xml | 20 ++++- src/main/res/values-v21/themes.xml | 4 + src/main/res/values/attrs.xml | 6 ++ src/main/res/values/colors.xml | 2 + src/main/res/values/themes.xml | 4 + 7 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/widget/UnreadCountCustomView.java diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java index 4576dea3c..030a57ddd 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java @@ -25,6 +25,7 @@ import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.XmppActivity; +import eu.siacs.conversations.ui.widget.UnreadCountCustomView; import eu.siacs.conversations.utils.UIHelper; public class ConversationAdapter extends ArrayAdapter { @@ -62,6 +63,7 @@ public class ConversationAdapter extends ArrayAdapter { ImageView notificationStatus = (ImageView) view.findViewById(R.id.notification_status); Message message = conversation.getLatestMessage(); + int unreadCount = conversation.unreadCount(); if (!conversation.isRead()) { convName.setTypeface(null, Typeface.BOLD); @@ -79,6 +81,13 @@ public class ConversationAdapter extends ArrayAdapter { Pair preview = UIHelper.getMessagePreview(activity,message); mLastMessage.setVisibility(View.VISIBLE); imagePreview.setVisibility(View.GONE); + UnreadCountCustomView unreadCountCustomView = (UnreadCountCustomView) view.findViewById(R.id.unread_count); + if (unreadCount > 0) { + unreadCountCustomView.setVisibility(View.VISIBLE); + unreadCountCustomView.setUnreadCount(unreadCount); + } else { + unreadCountCustomView.setVisibility(View.GONE); + } mLastMessage.setText(preview.first); if (preview.second) { if (conversation.isRead()) { diff --git a/src/main/java/eu/siacs/conversations/ui/widget/UnreadCountCustomView.java b/src/main/java/eu/siacs/conversations/ui/widget/UnreadCountCustomView.java new file mode 100644 index 000000000..36284546d --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/widget/UnreadCountCustomView.java @@ -0,0 +1,76 @@ +package eu.siacs.conversations.ui.widget; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Typeface; +import android.support.v4.content.ContextCompat; +import android.util.AttributeSet; +import android.view.View; + +import eu.siacs.conversations.R; + +public class UnreadCountCustomView extends View { + + private int unreadCount; + private Paint paint, textPaint; + private int backgroundColor = 0xff326130; + + public UnreadCountCustomView(Context context) { + super(context); + init(); + } + + public UnreadCountCustomView(Context context, AttributeSet attrs) { + super(context, attrs); + initXMLAttrs(context, attrs); + init(); + } + + public UnreadCountCustomView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + initXMLAttrs(context, attrs); + init(); + } + + private void initXMLAttrs(Context context, AttributeSet attrs) { + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UnreadCountCustomView); + setBackgroundColor(a.getColor(a.getIndex(0), ContextCompat.getColor(context, R.color.unreadcountlight))); + a.recycle(); + } + + void init() { + paint = new Paint(); + paint.setColor(backgroundColor); + paint.setAntiAlias(true); + textPaint = new Paint(); + textPaint.setColor(Color.WHITE); + textPaint.setTextAlign(Paint.Align.CENTER); + textPaint.setAntiAlias(true); + textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + float midx = canvas.getWidth() / 2.0f; + float midy = canvas.getHeight() / 2.0f; + float radius = Math.min(canvas.getWidth(), canvas.getHeight()) / 2.0f; + float textOffset = canvas.getWidth() / 6.0f; + textPaint.setTextSize(0.95f * radius); + canvas.drawCircle(midx, midy, radius * 0.94f, paint); + canvas.drawText(String.valueOf(unreadCount), midx, midy + textOffset, textPaint); + + } + + public void setUnreadCount(int unreadCount) { + this.unreadCount = unreadCount; + invalidate(); + } + + public void setBackgroundColor(int backgroundColor) { + this.backgroundColor = backgroundColor; + } +} \ No newline at end of file diff --git a/src/main/res/layout/conversation_list_row.xml b/src/main/res/layout/conversation_list_row.xml index 17456810e..157fdc72e 100644 --- a/src/main/res/layout/conversation_list_row.xml +++ b/src/main/res/layout/conversation_list_row.xml @@ -59,10 +59,11 @@ + android:id="@+id/txt_img_wrapper"> + + @drawable/message_bubble_sent @drawable/message_bubble_received + @color/unreadcountlight + @color/primary700 0.54 @@ -115,6 +117,8 @@ @drawable/message_bubble_sent_grey @drawable/message_bubble_received_dark + @color/unreadcountdark + @color/primary900 1.0 diff --git a/src/main/res/values/attrs.xml b/src/main/res/values/attrs.xml index da3aa3821..9ba50ef3a 100644 --- a/src/main/res/values/attrs.xml +++ b/src/main/res/values/attrs.xml @@ -31,6 +31,8 @@ + + @@ -66,4 +68,8 @@ + + + + \ No newline at end of file diff --git a/src/main/res/values/colors.xml b/src/main/res/values/colors.xml index 60db84dd2..51437478e 100644 --- a/src/main/res/values/colors.xml +++ b/src/main/res/values/colors.xml @@ -22,4 +22,6 @@ #ffff9800 #ff259b24 #ff4b9b4a + #ff4b9b4a + #ff326130 \ No newline at end of file diff --git a/src/main/res/values/themes.xml b/src/main/res/values/themes.xml index c999e7b87..e8f14a35b 100644 --- a/src/main/res/values/themes.xml +++ b/src/main/res/values/themes.xml @@ -46,6 +46,8 @@ @drawable/message_bubble_sent @drawable/message_bubble_received + @color/unreadcountlight + @color/primary700 1.0 @@ -108,6 +110,8 @@ @drawable/message_bubble_sent_grey @drawable/message_bubble_received_dark + @color/unreadcountdark + @color/primary900 1.0