use of com.bumptech.glide.request.target.DrawableImageViewTarget in project xabber-android by redsolution.
the class CrowdfundingChatAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull CrowdMessageVH holder, int i) {
final CrowdfundingMessage message = getMessage(i);
if (message == null)
return;
// appearance
holder.messageText.setTextAppearance(context, appearanceStyle);
// text
String text = message.getMessageForCurrentLocale();
if (text == null)
text = "";
// Added .concat("‍")
// to avoid click by empty space after ClickableSpan
holder.messageText.setText(Html.fromHtml(text.concat("‍")));
// to avoid bug - https://issuetracker.google.com/issues/36907309
holder.messageText.setAutoLinkMask(0);
holder.messageText.setMovementMethod(CorrectlyMeasuringTextView.LocalLinkMovementMethod.getInstance());
// text or image
if (FileManager.isImageUrl(text)) {
holder.messageImage.setVisibility(View.VISIBLE);
holder.messageText.setVisibility(View.GONE);
final ImageView image = holder.messageImage;
final TextView textMessage = holder.messageText;
Glide.with(context).load(text).placeholder(R.drawable.ic_recent_image_placeholder).error(R.drawable.ic_recent_image_placeholder).into(new DrawableImageViewTarget(image) {
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
super.onLoadStarted(placeholder);
image.setImageDrawable(placeholder);
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
image.setImageDrawable(errorDrawable);
textMessage.setVisibility(View.VISIBLE);
}
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
image.setImageDrawable(resource);
}
});
} else {
holder.messageImage.setVisibility(View.GONE);
holder.messageText.setVisibility(View.VISIBLE);
}
// nickname
String nick = message.getNameForCurrentLocale();
if (nick != null) {
holder.messageHeader.setText(nick);
holder.messageHeader.setTextColor(ColorManager.changeColor(ColorGenerator.MATERIAL.getColor(nick), 0.8f));
holder.messageHeader.setVisibility(View.VISIBLE);
} else
holder.messageHeader.setVisibility(View.GONE);
// time
String time = StringUtils.getTimeText(new Date((long) message.getTimestamp() * 1000));
holder.messageTime.setText(time);
// status
holder.statusIcon.setVisibility(View.GONE);
holder.ivEncrypted.setVisibility(View.GONE);
// need tail
boolean needTail = false;
CrowdfundingMessage nextMessage = getMessage(i + 1);
if (nextMessage != null)
needTail = !message.getAuthorJid().equals(nextMessage.getAuthorJid());
else
needTail = true;
// date
boolean needDate;
CrowdfundingMessage previousMessage = getMessage(i - 1);
if (previousMessage != null) {
needDate = !Utils.isSameDay((long) message.getTimestamp() * 1000, (long) previousMessage.getTimestamp() * 1000);
} else
needDate = true;
if (holder.tvDate != null) {
if (needDate) {
holder.tvDate.setText(StringUtils.getDateStringForMessage((long) message.getTimestamp() * 1000));
holder.tvDate.setVisibility(View.VISIBLE);
} else
holder.tvDate.setVisibility(View.GONE);
}
// avatar
String avatarUrl = message.getAuthorAvatar();
if (needTail && avatarUrl != null) {
setupAvatar(holder.avatar, avatarUrl);
holder.avatar.setVisibility(View.VISIBLE);
holder.avatarBackground.setVisibility(View.VISIBLE);
} else {
holder.avatar.setVisibility(View.INVISIBLE);
holder.avatarBackground.setVisibility(View.INVISIBLE);
}
// setup BACKGROUND
Drawable balloonDrawable = context.getResources().getDrawable(needTail ? R.drawable.msg_in : R.drawable.msg);
Drawable shadowDrawable = context.getResources().getDrawable(needTail ? R.drawable.msg_in_shadow : R.drawable.msg_shadow);
shadowDrawable.setColorFilter(context.getResources().getColor(R.color.black), PorterDuff.Mode.MULTIPLY);
holder.messageBalloon.setBackgroundDrawable(balloonDrawable);
holder.messageShadow.setBackgroundDrawable(shadowDrawable);
// setup BALLOON margins
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(Utils.dipToPx(needTail ? 2f : 11f, context), Utils.dipToPx(2f, context), Utils.dipToPx(0f, context), Utils.dipToPx(2f, context));
holder.messageShadow.setLayoutParams(layoutParams);
// setup MESSAGE padding
holder.messageBalloon.setPadding(Utils.dipToPx(needTail ? 20f : 12f, context), Utils.dipToPx(8f, context), Utils.dipToPx(12f, context), Utils.dipToPx(8f, context));
holder.itemView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View view) {
listener.onBind(message);
}
@Override
public void onViewDetachedFromWindow(View v) {
}
});
}
Aggregations