ChatRoomAdpter.java 20.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
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.UgcVideoMessage;
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 int ugcVideoUser = 9;
    private int ugcVideoOppsite = 10;
    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;

        } else if (o instanceof UgcVideoMessage) {
            UgcVideoMessage o1 = (UgcVideoMessage) o;
            if (o1.isUserSend())
                return ugcVideoUser;
            else return ugcVideoOppsite;

        }
        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));
        } else if (viewType == ugcVideoUser) {
            return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_user, null));
        } else if (viewType == ugcVideoOppsite) {
            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);
        } else if (holder instanceof UgcVideoMessageHolder) {
            final UgcVideoMessageHolder holder1 = (UgcVideoMessageHolder) holder;
            final Object o = mItems.get(position);
            final UgcVideoMessage o1 = (UgcVideoMessage) 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);
                            }
                        });
                    }
                }
            }
        }
    }

    public class UgcVideoMessageHolder extends RecyclerView.ViewHolder {

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

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

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

        private void sendSuccess(final UgcVideoMessage videoMessage) {
            videoLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null)
                        mItemClickListener.onUgcVideoClick(videoMessage);
                }
            });
            if (videoMessage.getCnUgcMessage().isSelf()) {
                Glide.with(mContext).load(videoMessage.getCnUgcMessage().getCoverPath()).into(video_cover);
            } else {
                String coverUuid = videoMessage.getCnUgcMessage().getFileId() + "_image";
                final String imagePath = FileUtil.getCacheFilePath(coverUuid);
                if (FileUtil.isCacheFileExist(coverUuid)) {
                    if (video_cover != null && mContext != null)
                        Glide.with(mContext).load(imagePath).into(video_cover);
                } else {
                    if (!videoMessage.isDownloading()) {
                        videoMessage.setDownloading(true);
                        videoMessage.getCnUgcMessage().getCover().getImage(imagePath, new IChat.OnDownloadListener() {
                            @Override
                            public void onSuccess() {
                                videoMessage.setDownloading(false);
                                if (video_cover != null && mContext != null)
                                    Glide.with(mContext).load(imagePath).into(video_cover);
                            }

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

}