ChatRoomAdpter.java 16.2 KB
package com.cnlive.im.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cnlive.im.R;
import com.cnlive.im.adapter.base.BaseRecyclerAdapter;
import com.cnlive.im.adapter.base.ItemClickListener;
import com.cnlive.im.mode.ImagMessage;
import com.cnlive.im.mode.StringMessage;
import com.cnlive.im.mode.VideoMessage;
import com.cnlive.im.mode.VoiceMessage;
import com.cnlive.im.util.FileUtil;
import com.cnlive.libs.util.chat.base.IChat;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author 陈硕
 * @time 2017/7/14  14:35
 * @desc ${TODD}
 */
public class ChatRoomAdpter extends BaseRecyclerAdapter {

    private int stringUser = 1;
    private int stringOppsite = 2;
    private int imaUser = 3;
    private int imaOppsite = 4;
    private int voiceUser = 5;
    private int voiceOppsite = 6;
    private int videoUser = 7;
    private int videoOppsite = 8;
    private Context mContext;
    private ItemClickListener mItemClickListener;

    private boolean isPrivate;

    public ChatRoomAdpter(Context context, boolean isPrivate) {
        super(context);
        this.mContext = context;
        this.isPrivate = isPrivate;
    }


    public void setonVoiceItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        Object o = mItems.get(position);
        if (o instanceof StringMessage) {
            StringMessage o1 = (StringMessage) o;
            boolean userSend = o1.isUserSend();
            if (userSend) return stringUser;
            else return stringOppsite;
        } else if (o instanceof ImagMessage) {
            ImagMessage o1 = (ImagMessage) o;
            if (o1.isUserSend()) return imaUser;
            else return imaOppsite;
        } else if (o instanceof VoiceMessage) {
            VoiceMessage o1 = (VoiceMessage) o;
            if (o1.isUserSend())
                return voiceUser;
            else return voiceOppsite;

        } else if (o instanceof VideoMessage) {
            VideoMessage o1 = (VideoMessage) o;
            if (o1.isUserSend())
                return videoUser;
            else return videoOppsite;

        }
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == imaUser) {
            return new ImaMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.imag_layout_item_user, null));
        } else if (viewType == imaOppsite) {
            return new ImaMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.imag_layout_item_oppsite, null));
        } else if (viewType == stringUser) {
            return new StringMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.string_layout_item_user, null));
        } else if (viewType == stringOppsite) {
            return new StringMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.string_layout_item_oppsite, null));
        } else if (viewType == voiceUser) {
            return new VoiceMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.voice_layout_item_user, null));
        } else if (viewType == voiceOppsite) {
            return new VoiceMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.voice_layout_item_oppsite, null));
        } else if (viewType == videoUser) {
            return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_user, null));
        } else if (viewType == videoOppsite) {
            return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_oppsite, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof StringMessageHolder) {
            StringMessageHolder holder1 = (StringMessageHolder) holder;
            Object o = mItems.get(position);
            StringMessage o1 = (StringMessage) o;
            holder1.showMessage(o1);
        } else if (holder instanceof ImaMessageHolder) {
            ImaMessageHolder holder1 = (ImaMessageHolder) holder;
            Object o = mItems.get(position);
            ImagMessage o1 = (ImagMessage) o;
            holder1.showMessage(o1);
        } else if (holder instanceof VoiceMessageHolder) {
            final VoiceMessageHolder holder1 = (VoiceMessageHolder) holder;
            final Object o = mItems.get(position);
            final VoiceMessage o1 = (VoiceMessage) o;
            holder1.showMessage(o1);
        } else if (holder instanceof VideoMessageHolder) {
            final VideoMessageHolder holder1 = (VideoMessageHolder) holder;
            final Object o = mItems.get(position);
            final VideoMessage o1 = (VideoMessage) o;
            holder1.showMessage(o1);
        }

    }

    public class StringMessageHolder extends RecyclerView.ViewHolder {
        private TextView user_name;
        private TextView msg_text;
        private TextView status;

        public StringMessageHolder(View itemView) {
            super(itemView);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            msg_text = (TextView) itemView.findViewById(R.id.msg_text);
            status = (TextView) itemView.findViewById(R.id.status);
        }

        public void showMessage(StringMessage item) {
            user_name.setText(item.getCnMessage().getUserInfo().getUserName());

            switch (item.getCnMessage().getStatus()) {
                case IChat.CN_MSG_STATUS_SENDING:
                    if (status != null)
                        status.setText("发送中");
                    sendMsg(item);
                    break;
                case IChat.CN_MSG_STATUS_SEND_FAIL:
                    if (status != null)
                        status.setText("发送失败");
                    sendMsg(item);
                    break;
                case IChat.CN_MSG_STATUS_SEND_SUCCESS:
                    if (status != null)
                        status.setText("发送成功");
                    sendMsg(item);
                    break;
            }
        }

        private void sendMsg(StringMessage item) {
            if (isPrivate) {
                try {
                    msg_text.setText((String) (new JSONObject(item.getCnMessage().getContent()).get("message")));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                msg_text.setText(item.getCnMessage().getContent());
            }
            msg_text.setTextColor(Color.BLACK);
        }
    }

    public class ImaMessageHolder extends RecyclerView.ViewHolder {

        private TextView ima_user_name;
        private ImageView img_content;
        private TextView status;

        public ImaMessageHolder(View itemView) {
            super(itemView);
            ima_user_name = (TextView) itemView.findViewById(R.id.img_user_name);
            img_content = (ImageView) itemView.findViewById(R.id.img_content);
            status = (TextView) itemView.findViewById(R.id.status);
        }

        public void showMessage(final ImagMessage imagMessage) {
            ima_user_name.setText(imagMessage.getCnImgMessage().getUserInfo().getUserName());
            img_content.getLayoutParams().height = 200;
            img_content.getLayoutParams().width = 200;
            img_content.setLayoutParams(img_content.getLayoutParams());
            switch (imagMessage.getCnImgMessage().getStatus()) {
                case IChat.CN_MSG_STATUS_SENDING:
                    if (status != null)
                        status.setText("发送中");
                    Glide.with(mContext).load(imagMessage.getCnImgMessage().getImagePath()).into(img_content);
                    break;
                case IChat.CN_MSG_STATUS_SEND_FAIL:
                    if (status != null)
                        status.setText("发送失败");
                    Glide.with(mContext).load(imagMessage.getCnImgMessage().getImagePath()).into(img_content);
                    break;
                case IChat.CN_MSG_STATUS_SEND_SUCCESS:
                    if (status != null)
                        status.setText("发送成功");
                    sendSuccess(imagMessage);
                    break;
            }
        }

        private void sendSuccess(final ImagMessage imagMessage) {
            if (imagMessage.getCnImgMessage().isSelf()) {
                Glide.with(mContext).load(imagMessage.getCnImgMessage().getImagePath()).into(img_content);
            } else {
                String thumbUuid = imagMessage.getCnImgMessage().getThumbUuid();
                final String imagePath = FileUtil.getCacheFilePath(thumbUuid);
                if (FileUtil.isCacheFileExist(thumbUuid)) {
                    if (img_content != null && mContext != null)
                        Glide.with(mContext).load(imagePath).into(img_content);
                } else {
                    if (!imagMessage.isDownloading()) {
                        imagMessage.setDownloading(true);
                        imagMessage.getCnImgMessage().getThumbImage(imagePath, new IChat.OnDownloadListener() {
                            @Override
                            public void onSuccess() {
                                imagMessage.setDownloading(false);
                                if (img_content != null && mContext != null)
                                    Glide.with(mContext).load(imagePath).into(img_content);
                            }

                            @Override
                            public void onError(int i, String s) {
                                imagMessage.setDownloading(false);
                            }
                        });
                    }
                }
            }
            img_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null)
                        mItemClickListener.onImageClick(imagMessage);
                }
            });
        }
    }

    public class VoiceMessageHolder extends RecyclerView.ViewHolder {


        private TextView voice_user_name;
        private ImageView voiceAnim;
        private TextView voice_duration;
        private View voice_none_read;
        private TextView status;

        public VoiceMessageHolder(View itemView) {
            super(itemView);
            voice_user_name = (TextView) itemView.findViewById(R.id.voice_user_name);
            voiceAnim = (ImageView) itemView.findViewById(R.id.voiceAnim);
            voice_duration = (TextView) itemView.findViewById(R.id.voice_duration);
            voice_none_read = itemView.findViewById(R.id.noneRead);
            status = (TextView) itemView.findViewById(R.id.status);
        }

        public void showMessage(final VoiceMessage voiceMessage) {
            voice_user_name.setText(voiceMessage.getCnVoiceMessage().getUserInfo().getUserName());
            int duration = voiceMessage.getCnVoiceMessage().getVoiceTime();
            voice_duration.setText(String.valueOf(duration).concat("\""));
            switch (voiceMessage.getCnVoiceMessage().getStatus()) {
                case IChat.CN_MSG_STATUS_SENDING:
                    if (status != null)
                        status.setText("发送中");
                    break;
                case IChat.CN_MSG_STATUS_SEND_FAIL:
                    if (status != null)
                        status.setText("发送失败");
                    break;
                case IChat.CN_MSG_STATUS_SEND_SUCCESS:
                    if (status != null)
                        status.setText("发送成功");
                    sendSuccess(voiceMessage);
                    break;
            }
        }

        private void sendSuccess(final VoiceMessage voiceMessage) {
            if (voice_none_read != null)
                voice_none_read.setVisibility(voiceMessage.getCnVoiceMessage().isRead() ? View.GONE : View.VISIBLE);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null)
                        mItemClickListener.onItemClick(voiceMessage, (AnimationDrawable) voiceAnim.getBackground());
                    if (voice_none_read != null)
                        voice_none_read.setVisibility(View.GONE);
                }
            });
        }
    }

    public class VideoMessageHolder extends RecyclerView.ViewHolder {

        private TextView video_user_name;
        private ImageView video_snapshot;
        private View videoLayout;
        private TextView status;

        public VideoMessageHolder(View itemView) {
            super(itemView);
            video_user_name = (TextView) itemView.findViewById(R.id.user_name);
            video_snapshot = (ImageView) itemView.findViewById(R.id.img_content);
            videoLayout = itemView.findViewById(R.id.videoLayout);
            status = (TextView) itemView.findViewById(R.id.status);
        }

        public void showMessage(final VideoMessage videoMessage) {
            video_user_name.setText(videoMessage.getCnVideoMessage().getUserInfo().getUserName());
            switch (videoMessage.getCnVideoMessage().getStatus()) {
                case IChat.CN_MSG_STATUS_SENDING:
                    if (status != null)
                        status.setText("发送中");
                    Glide.with(mContext).load(videoMessage.getCnVideoMessage().getSnapshotPath()).into(video_snapshot);
                    break;
                case IChat.CN_MSG_STATUS_SEND_FAIL:
                    if (status != null)
                        status.setText("发送失败");
                    Glide.with(mContext).load(videoMessage.getCnVideoMessage().getSnapshotPath()).into(video_snapshot);
                    break;
                case IChat.CN_MSG_STATUS_SEND_SUCCESS:
                    if (status != null)
                        status.setText("发送成功");
                    sendSuccess(videoMessage);
                    break;
            }
        }

        private void sendSuccess(final VideoMessage videoMessage) {
            videoLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null)
                        mItemClickListener.onVideoClick(videoMessage);
                }
            });
            if (videoMessage.getCnVideoMessage().isSelf()) {
                Glide.with(mContext).load(videoMessage.getCnVideoMessage().getSnapshotPath()).into(video_snapshot);
            } else {
                String snapshotUuid = videoMessage.getCnVideoMessage().getSnapshot().getUuid();
                final String imagePath = FileUtil.getCacheFilePath(snapshotUuid);
                if (FileUtil.isCacheFileExist(snapshotUuid)) {
                    if (video_snapshot != null && mContext != null)
                        Glide.with(mContext).load(imagePath).into(video_snapshot);
                } else {
                    if (!videoMessage.isDownloading()) {
                        videoMessage.setDownloading(true);
                        videoMessage.getCnVideoMessage().getSnapshot().getImage(imagePath, new IChat.OnDownloadListener() {
                            @Override
                            public void onSuccess() {
                                videoMessage.setDownloading(false);
                                if (video_snapshot != null && mContext != null)
                                    Glide.with(mContext).load(imagePath).into(video_snapshot);
                            }

                            @Override
                            public void onError(int i, String s) {
                                videoMessage.setDownloading(false);
                            }
                        });
                    }
                }
            }
        }
    }

}