Search in sources :

Example 16 with CrowdfundingMessage

use of com.xabber.android.data.database.realm.CrowdfundingMessage 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) {
        }
    });
}
Also used : Drawable(android.graphics.drawable.Drawable) DrawableImageViewTarget(com.bumptech.glide.request.target.DrawableImageViewTarget) ImageView(android.widget.ImageView) CorrectlyMeasuringTextView(com.xabber.android.ui.widget.CorrectlyMeasuringTextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) Date(java.util.Date) CorrectlyMeasuringTextView(com.xabber.android.ui.widget.CorrectlyMeasuringTextView) TextView(android.widget.TextView) ImageView(android.widget.ImageView) LinearLayout(android.widget.LinearLayout) CrowdfundingMessage(com.xabber.android.data.database.realm.CrowdfundingMessage)

Aggregations

CrowdfundingMessage (com.xabber.android.data.database.realm.CrowdfundingMessage)16 Realm (io.realm.Realm)7 AbstractChat (com.xabber.android.data.message.AbstractChat)3 NewMessageEvent (com.xabber.android.data.message.NewMessageEvent)3 CrowdfundingContact (com.xabber.android.data.roster.CrowdfundingContact)3 RealmList (io.realm.RealmList)3 ArrayList (java.util.ArrayList)3 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)2 CrowdfundingChat (com.xabber.android.data.message.CrowdfundingChat)2 AbstractContact (com.xabber.android.data.roster.AbstractContact)2 GroupConfiguration (com.xabber.android.ui.adapter.contactlist.GroupConfiguration)2 List (java.util.List)2 Action1 (rx.functions.Action1)2 Drawable (android.graphics.drawable.Drawable)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 DrawableImageViewTarget (com.bumptech.glide.request.target.DrawableImageViewTarget)1