NekoX/TMessagesProj/src/main/java/org/telegram/ui/Cells/HintDialogCell.java

160 lines
6.4 KiB
Java
Raw Normal View History

2016-05-25 23:49:47 +02:00
/*
* This is the source code of Telegram for Android v. 3.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2017-03-31 01:58:05 +02:00
* Copyright Nikolai Kudashov, 2013-2017.
2016-05-25 23:49:47 +02:00
*/
package org.telegram.ui.Cells;
import android.content.Context;
import android.graphics.Canvas;
2017-03-31 01:58:05 +02:00
import android.graphics.RectF;
2016-05-25 23:49:47 +02:00
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ContactsController;
import org.telegram.messenger.MessagesController;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.UserConfig;
2016-05-25 23:49:47 +02:00
import org.telegram.tgnet.TLRPC;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
2016-05-25 23:49:47 +02:00
import org.telegram.ui.Components.AvatarDrawable;
import org.telegram.ui.Components.BackupImageView;
import org.telegram.ui.Components.LayoutHelper;
public class HintDialogCell extends FrameLayout {
private BackupImageView imageView;
private TextView nameTextView;
private AvatarDrawable avatarDrawable = new AvatarDrawable();
2017-03-31 01:58:05 +02:00
private RectF rect = new RectF();
2016-05-25 23:49:47 +02:00
private int lastUnreadCount;
private int countWidth;
private StaticLayout countLayout;
private long dialog_id;
2018-07-30 04:07:02 +02:00
private int currentAccount = UserConfig.selectedAccount;
2016-05-25 23:49:47 +02:00
public HintDialogCell(Context context) {
super(context);
imageView = new BackupImageView(context);
imageView.setRoundRadius(AndroidUtilities.dp(27));
addView(imageView, LayoutHelper.createFrame(54, 54, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 7, 0, 0));
nameTextView = new TextView(context);
2017-03-31 01:58:05 +02:00
nameTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
2016-05-25 23:49:47 +02:00
nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
nameTextView.setMaxLines(2);
nameTextView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
nameTextView.setLines(2);
nameTextView.setEllipsize(TextUtils.TruncateAt.END);
addView(nameTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP, 6, 64, 6, 0));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2017-03-31 01:58:05 +02:00
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(100), MeasureSpec.EXACTLY));
2016-05-25 23:49:47 +02:00
}
public void checkUnreadCounter(int mask) {
if (mask != 0 && (mask & MessagesController.UPDATE_MASK_READ_DIALOG_MESSAGE) == 0 && (mask & MessagesController.UPDATE_MASK_NEW_MESSAGE) == 0) {
return;
}
2018-07-30 04:07:02 +02:00
TLRPC.TL_dialog dialog = MessagesController.getInstance(currentAccount).dialogs_dict.get(dialog_id);
2016-05-25 23:49:47 +02:00
if (dialog != null && dialog.unread_count != 0) {
if (lastUnreadCount != dialog.unread_count) {
lastUnreadCount = dialog.unread_count;
String countString = String.format("%d", dialog.unread_count);
2017-03-31 01:58:05 +02:00
countWidth = Math.max(AndroidUtilities.dp(12), (int) Math.ceil(Theme.dialogs_countTextPaint.measureText(countString)));
countLayout = new StaticLayout(countString, Theme.dialogs_countTextPaint, countWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
2016-05-25 23:49:47 +02:00
if (mask != 0) {
invalidate();
}
}
} else if (countLayout != null) {
if (mask != 0) {
invalidate();
}
lastUnreadCount = 0;
countLayout = null;
}
}
2017-03-31 01:58:05 +02:00
public void update() {
int uid = (int) dialog_id;
TLRPC.FileLocation photo = null;
if (uid > 0) {
2018-07-30 04:07:02 +02:00
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(uid);
2017-03-31 01:58:05 +02:00
avatarDrawable.setInfo(user);
} else {
2018-07-30 04:07:02 +02:00
TLRPC.Chat chat = MessagesController.getInstance(currentAccount).getChat(-uid);
2017-03-31 01:58:05 +02:00
avatarDrawable.setInfo(chat);
}
}
2016-10-11 13:57:01 +02:00
public void setDialog(int uid, boolean counter, CharSequence name) {
2016-05-25 23:49:47 +02:00
dialog_id = uid;
TLRPC.FileLocation photo = null;
if (uid > 0) {
2018-07-30 04:07:02 +02:00
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(uid);
2016-05-25 23:49:47 +02:00
if (name != null) {
nameTextView.setText(name);
} else if (user != null) {
nameTextView.setText(ContactsController.formatName(user.first_name, user.last_name));
} else {
nameTextView.setText("");
}
avatarDrawable.setInfo(user);
if (user != null && user.photo != null) {
photo = user.photo.photo_small;
}
} else {
2018-07-30 04:07:02 +02:00
TLRPC.Chat chat = MessagesController.getInstance(currentAccount).getChat(-uid);
2016-05-25 23:49:47 +02:00
if (name != null) {
nameTextView.setText(name);
} else if (chat != null) {
nameTextView.setText(chat.title);
} else {
nameTextView.setText("");
}
avatarDrawable.setInfo(chat);
if (chat != null && chat.photo != null) {
photo = chat.photo.photo_small;
}
}
imageView.setImage(photo, "50_50", avatarDrawable);
2016-10-11 13:57:01 +02:00
if (counter) {
checkUnreadCounter(0);
} else {
countLayout = null;
}
2016-05-25 23:49:47 +02:00
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
boolean result = super.drawChild(canvas, child, drawingTime);
2017-03-31 01:58:05 +02:00
if (child == imageView && countLayout != null) {
int top = AndroidUtilities.dp(6);
int left = AndroidUtilities.dp(54);
int x = left - AndroidUtilities.dp(5.5f);
rect.set(x, top, x + countWidth + AndroidUtilities.dp(11), top + AndroidUtilities.dp(23));
2018-07-30 04:07:02 +02:00
canvas.drawRoundRect(rect, 11.5f * AndroidUtilities.density, 11.5f * AndroidUtilities.density, MessagesController.getInstance(currentAccount).isDialogMuted(dialog_id) ? Theme.dialogs_countGrayPaint : Theme.dialogs_countPaint);
2017-03-31 01:58:05 +02:00
canvas.save();
canvas.translate(left, top + AndroidUtilities.dp(4));
countLayout.draw(canvas);
canvas.restore();
2016-05-25 23:49:47 +02:00
}
return result;
}
}