Guest User

Untitled

a guest
Aug 29th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 67.91 KB | None | 0 0
  1. public class BaseChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
  2.     private final String TAG = "BaseChatAdapter";
  3.     private int totalMessages = 0;
  4.     protected final int MESSAGE_TEXT = totalMessages++;
  5.     protected final int MESSAGE_CHAT_SERVICE = totalMessages++;
  6.     protected final int MESSAGE_DATE_SEPARATOR = totalMessages++;
  7.     protected final int MESSAGE_UNREAD_SEPARATOR = totalMessages++;
  8.     protected final int MESSAGE_TEXT_FORWARDED = totalMessages++;
  9.     protected final int MESSAGE_PHOTO = totalMessages++;
  10.     protected final int MESSAGE_CONTACT = totalMessages++;
  11.     protected final int MESSAGE_AUDIO = totalMessages++;
  12.     protected final int MESSAGE_GEOPOINT = totalMessages++;
  13.     protected final int MESSAGE_DOCUMENT = totalMessages++;
  14.     protected final int MESSAGE_STICKER = totalMessages++;
  15.     protected final int MESSAGE_VIDEO = totalMessages++;
  16.     protected final int MESSAGE_VOICE = totalMessages++;
  17.     protected final int MESSAGE_WEB_PAGE = totalMessages++;
  18.     protected final int MESSAGE_VENUE = totalMessages++;
  19.     protected final int MESSAGE_BOT_HEADER = totalMessages++;
  20.     protected final int MESSAGE_ANIMATION = totalMessages++;
  21.  
  22.     private BaseChatFragment.ChatDelegate chatDelegate;
  23.     private final List<MessageModel> msgListRef;
  24.     private WeakReference<ChatController> controller;
  25.     private final int defaultMargin, increasedMargin, specialMargin, specialIncreasedMargin;
  26.     private TIntObjectHashMap<MessageStatusView.UpdateChatReadDelegate> statusViewDelegates = new TIntObjectHashMap<MessageStatusView.UpdateChatReadDelegate>();
  27.     private final TIntHashSet selection = new TIntHashSet();
  28.     public BaseChatAdapter(List<MessageModel> data, ChatController c)
  29.     {
  30.         defaultMargin = AndroidUtilities.dp(40);
  31.         increasedMargin = AndroidUtilities.dp(80);
  32.         specialMargin = AndroidUtilities.dp(56);
  33.         specialIncreasedMargin = AndroidUtilities.dp(96);
  34.         this.msgListRef = data;
  35.         this.controller = new WeakReference<>(c);
  36.     }
  37.     @Override
  38.     public int getItemViewType(int position) {
  39.         synchronized(msgListRef)
  40.         {
  41.             MessageModel model = msgListRef.get(position);
  42.             if(model.isPseudoMessage())
  43.             {
  44.                 if(model.getSuitableConstructor() == Constants.MODEL_DATE_SEPARATOR)
  45.                     return MESSAGE_DATE_SEPARATOR;
  46.                 else if(model.getSuitableConstructor() == Constants.MODEL_UNREAD_SEPARATOR)
  47.                     return MESSAGE_UNREAD_SEPARATOR;
  48.                 else if(model.getSuitableConstructor() == Constants.MODEL_BOT_CHAT_HEADER)
  49.                     return MESSAGE_BOT_HEADER;
  50.             }
  51.             if(model.isChatServiceMessage())
  52.                 return MESSAGE_CHAT_SERVICE;
  53.             else if(model.isForwarded() && model.getSuitableConstructor() == TdApi.MessageText.CONSTRUCTOR)
  54.                 return MESSAGE_TEXT_FORWARDED;
  55.             if(model.getSuitableConstructor() == TdApi.MessageText.CONSTRUCTOR || model.getSuitableConstructor() == TdApi.MessageUnsupported.CONSTRUCTOR || model.getSuitableConstructor() == TdApi.MessageDeleted.CONSTRUCTOR)
  56.                 return MESSAGE_TEXT;
  57.             else if(model.getSuitableConstructor() == TdApi.MessagePhoto.CONSTRUCTOR)
  58.                 return MESSAGE_PHOTO;
  59.             else if(model.getSuitableConstructor() == TdApi.MessageContact.CONSTRUCTOR)
  60.                 return MESSAGE_CONTACT;
  61.             else if(model.getSuitableConstructor() == TdApi.MessageAudio.CONSTRUCTOR)
  62.                 return MESSAGE_AUDIO;
  63.             else if(model.getSuitableConstructor() == TdApi.MessageLocation.CONSTRUCTOR)
  64.                 return MESSAGE_GEOPOINT;
  65.             else if(model.getSuitableConstructor() == TdApi.MessageDocument.CONSTRUCTOR)
  66.                 return MESSAGE_DOCUMENT;
  67.             else if(model.getSuitableConstructor() == TdApi.MessageSticker.CONSTRUCTOR)
  68.                 return MESSAGE_STICKER;
  69.             else if(model.getSuitableConstructor() == TdApi.MessageVoice.CONSTRUCTOR)
  70.                 return MESSAGE_VOICE;
  71.             else if(model.getSuitableConstructor() == TdApi.MessageWebPage.CONSTRUCTOR)
  72.                 return MESSAGE_WEB_PAGE;
  73.             else if(model.getSuitableConstructor() == TdApi.MessageVenue.CONSTRUCTOR)
  74.                 return MESSAGE_VENUE;
  75.             else if(model.getSuitableConstructor() == TdApi.MessageAnimation.CONSTRUCTOR)
  76.                 return MESSAGE_ANIMATION;
  77.             return MESSAGE_VIDEO;
  78.         }
  79.     }
  80.     @Override
  81.     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  82.         if(BuildConfig.DEBUG)
  83.             Log.d(TAG, String.format("onCreateViewHolder: %d", viewType));
  84.         if(viewType == MESSAGE_BOT_HEADER)
  85.         {
  86.             View v = new BotHeaderView(parent.getContext());
  87.             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
  88.             lp.setMargins(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(32), 0);
  89.             lp.gravity = Gravity.CENTER_VERTICAL;
  90.             v.setLayoutParams(lp);
  91.             return new BotHeaderViewHolder(v);
  92.         }
  93.         int resource = 0;
  94.         if(viewType == MESSAGE_DATE_SEPARATOR)
  95.             resource = R.layout.chat_date_separator;
  96.         else if(viewType == MESSAGE_UNREAD_SEPARATOR)
  97.             resource = R.layout.new_messages_separator;
  98.         else if(viewType == MESSAGE_TEXT)
  99.             resource = R.layout.message_text;
  100.         else if(viewType == MESSAGE_TEXT_FORWARDED)
  101.             resource = R.layout.message_forwarded_text;
  102.         else if(viewType == MESSAGE_PHOTO)
  103.             resource = R.layout.message_photo;
  104.         else if(viewType == MESSAGE_AUDIO)
  105.             resource = R.layout.message_audio;
  106.         else if(viewType == MESSAGE_CONTACT)
  107.             resource = R.layout.message_contact;
  108.         else if(viewType == MESSAGE_DOCUMENT)
  109.             resource = R.layout.message_document;
  110.         else if(viewType == MESSAGE_GEOPOINT)
  111.             resource = R.layout.message_geopoint;
  112.         else if(viewType == MESSAGE_CHAT_SERVICE)
  113.             resource = R.layout.chat_service_msg;
  114.         else if(viewType == MESSAGE_VIDEO)
  115.             resource = R.layout.message_video;
  116.         else if(viewType == MESSAGE_STICKER)
  117.             resource = R.layout.message_sticker;
  118.         else if(viewType == MESSAGE_VOICE)
  119.             resource = R.layout.message_voice;
  120.         else if(viewType == MESSAGE_WEB_PAGE)
  121.             resource = R.layout.message_webpage;
  122.         else if(viewType == MESSAGE_VENUE)
  123.             resource = R.layout.message_venue;
  124.         else if(viewType == MESSAGE_ANIMATION)
  125.             resource = R.layout.message_animation;
  126.         View view;
  127.         if(viewType == MESSAGE_DATE_SEPARATOR || viewType == MESSAGE_UNREAD_SEPARATOR || viewType == MESSAGE_CHAT_SERVICE)
  128.             view = LayoutInflater.from(TelegramApplication.sharedApplication()).inflate(resource, parent, false);
  129.         else
  130.         {
  131.             view = new SwipeMenuLayout(parent.getContext());
  132.             view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  133.             View content = LayoutInflater.from(TelegramApplication.sharedApplication()).inflate(resource, (ViewGroup) view, false);
  134.             View replyMenu = LayoutInflater.from(TelegramApplication.sharedApplication()).inflate(R.layout.quick_reply_menu, (ViewGroup) view, false);
  135.             View shareMenu = LayoutInflater.from(TelegramApplication.sharedApplication()).inflate(R.layout.quick_share, (ViewGroup) view, false);
  136.             content.setId(R.id.smContentView);
  137.             replyMenu.setId(R.id.smRightMenuView);
  138.             shareMenu.setId(R.id.smLeftMenuView);
  139.             ((ViewGroup) view).addView(content);
  140.             ((ViewGroup) view).addView(replyMenu);
  141.             ((ViewGroup) view).addView(shareMenu);
  142.             ((SwipeMenuLayout) view).didViewComplete();
  143.         }
  144.         if(viewType == MESSAGE_DATE_SEPARATOR)
  145.             return new DateSeparatorViewHolder(view);
  146.         else if(viewType == MESSAGE_UNREAD_SEPARATOR)
  147.             return new UnreadSeparatorViewHolder(view);
  148.         else if(viewType == MESSAGE_TEXT)
  149.             return new MessageTextViewHolder(view);
  150.         else if(viewType == MESSAGE_TEXT_FORWARDED)
  151.             return new ForwardedMessageTextViewHolder(view);
  152.         else if(viewType == MESSAGE_PHOTO)
  153.             return new MessagePhotoViewHolder(view);
  154.         else if(viewType == MESSAGE_AUDIO)
  155.             return new MessageAudioViewHolder(view);
  156.         else if(viewType == MESSAGE_CONTACT)
  157.             return new MessageContactViewHolder(view);
  158.         else if(viewType == MESSAGE_DOCUMENT)
  159.             return new MessageDocumentViewHolder(view);
  160.         else if(viewType == MESSAGE_GEOPOINT)
  161.             return new MessageGeoPointViewHolder(view);
  162.         else if(viewType == MESSAGE_CHAT_SERVICE)
  163.             return new ServiceMsgViewHolder(view);
  164.         else if(viewType == MESSAGE_VIDEO)
  165.             return new MessageVideoViewHolder(view);
  166.         else if(viewType == MESSAGE_STICKER)
  167.             return new MessageStickerViewHolder(view);
  168.         else if(viewType == MESSAGE_VOICE)
  169.             return new MessageVoiceViewHolder(view);
  170.         else if(viewType == MESSAGE_WEB_PAGE)
  171.             return new MessageWebPageViewHolder(view);
  172.         else if(viewType == MESSAGE_VENUE)
  173.             return new MessageVenueViewHolder(view);
  174.         else if(viewType == MESSAGE_ANIMATION)
  175.             return new MessageAnimationViewHolder(view);
  176.         return null;
  177.     }
  178.     @Override
  179.     public int getItemCount() {
  180.        if(msgListRef.isEmpty())
  181.        {
  182.            final ChatController c = getController();
  183.            if(c != null) {
  184.                c.checkLoading(c.start_message);
  185.            }
  186.        }
  187.        return msgListRef.size();
  188.     }
  189.  
  190.     @Override
  191.     public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder)
  192.     {
  193.         int position = holder.getAdapterPosition();
  194.         if(position < 0 || position >= getItemCount())
  195.             return;
  196.         int viewType = getItemViewType(position);
  197.         if(viewType != MESSAGE_DATE_SEPARATOR && viewType != MESSAGE_UNREAD_SEPARATOR && viewType != MESSAGE_CHAT_SERVICE && viewType != MESSAGE_BOT_HEADER)
  198.         {
  199.             try
  200.             {
  201.                 ((DefaultViewHolder) holder).onItemDetachFromWindow();
  202.             }
  203.             catch(Exception e) { e.printStackTrace(); }
  204.         }
  205.     }
  206.     @Override
  207.     public void onViewAttachedToWindow(RecyclerView.ViewHolder holder)
  208.     {
  209.         int position = holder.getAdapterPosition();
  210.         if(position < 0 || position >= getItemCount())
  211.             return;
  212.         int viewType = getItemViewType(position);
  213.         synchronized(msgListRef)
  214.         {
  215.             int itemsBelow = msgListRef.size() - position;
  216.             ScrollButtonDelegate scrollButtonDelegate = getController().getScrollButtonDelegate();
  217.             if(scrollButtonDelegate != null)
  218.                 scrollButtonDelegate.onChatScroll(itemsBelow, msgListRef.size());
  219.             MessageModel itm = msgListRef.get(position);
  220.             if(viewType == MESSAGE_CHAT_SERVICE && itm.getSuitableConstructor() == TdApi.MessageChatChangePhoto.CONSTRUCTOR)
  221.             {
  222.                 GroupChatChangePhotoMessageModel cast = (GroupChatChangePhotoMessageModel) itm;
  223.                 if(cast.getNewChatPhoto() != null) {
  224.                     ((ServiceMsgViewHolder) holder).newChatPhoto.setVisibility(View.VISIBLE);
  225.                     TelegramImageLoader.getInstance().loadImage(cast.getNewChatPhoto(), cast.getNewChatPhotoKey(), ((ServiceMsgViewHolder) holder).newChatPhoto, TelegramImageLoader.POST_PROCESSING_CIRCLE | TelegramImageLoader.POST_PROCESSING_SCALE, AndroidUtilities.dp(60), AndroidUtilities.dp(60), false);
  226.                 }
  227.                 else
  228.                     ((ServiceMsgViewHolder) holder).newChatPhoto.setVisibility(View.GONE);
  229.             }
  230.             else if(viewType == MESSAGE_CHAT_SERVICE)
  231.                 ((ServiceMsgViewHolder) holder).newChatPhoto.setVisibility(View.GONE);
  232.             if(viewType == MESSAGE_BOT_HEADER)
  233.             {
  234.                 ((BotHeaderViewHolder) holder).header.fillHeader((BotHeaderMessageModel) itm);
  235.                 if(getItemCount() == 1)
  236.                     ((BotHeaderViewHolder) holder).header.centerView(true);
  237.                 else
  238.                     ((BotHeaderViewHolder) holder).header.centerView(false);
  239.             }
  240.             else if(viewType == MESSAGE_DATE_SEPARATOR) {
  241.                 ((DateSeparatorViewHolder) holder).dateTextView.setText(((DateSectionMessageModel) itm).getHeaderDate());
  242.             }
  243.             if(!itm.isPseudoMessage() && (position >= msgListRef.size() - 5 || position <= 5))
  244.                 getController().checkLoading(msgListRef.get(position).getMessageId());
  245.             if(itm.isPseudoMessage() || itm.isChatServiceMessage())
  246.                 return;
  247.             DefaultViewHolder defaultHolder = (DefaultViewHolder) holder;
  248.             defaultHolder.onPreDraw(itm);
  249.         }
  250.     }
  251.     @Override
  252.     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  253.         if(BuildConfig.DEBUG)
  254.             Log.d(TAG, String.format("onBindViewHolder: position = %d; className = %s; itemCount = %d", position, holder.getClass().getName(), getItemCount()));
  255.         if(holder == null)
  256.             return;
  257.         int viewType = getItemViewType(position);
  258.         synchronized(msgListRef)
  259.         {
  260.             MessageModel itm = msgListRef.get(position);
  261.             if(viewType == MESSAGE_UNREAD_SEPARATOR)
  262.                 ((UnreadSeparatorViewHolder) holder).unreadTextView.setText(((NewMessagesMessageModel) itm).getHeaderString());
  263.             else if(viewType == MESSAGE_CHAT_SERVICE)
  264.                 ((ServiceMsgViewHolder) holder).serviceMessageText.setText(((TextMessageModel) itm).getText());
  265.             else if(!itm.isPseudoMessage())
  266.                 ((DefaultViewHolder) holder).onDraw(itm);
  267.             ChatController c = getController();
  268.             if(c != null && (position >= getItemCount() - 5 || position <= 5))
  269.                 c.checkLoading(itm.getMessageId());
  270.         }
  271.     }
  272.  
  273.     protected ChatController getController() { return this.controller.get(); }
  274.  
  275.     public class DateSeparatorViewHolder extends RecyclerView.ViewHolder
  276.     {
  277.         public TextView dateTextView;
  278.         public DateSeparatorViewHolder(View itemView) {
  279.             super(itemView);
  280.             this.dateTextView = (TextView) itemView.findViewById(R.id.date);
  281.         }
  282.     }
  283.     public class UnreadSeparatorViewHolder extends RecyclerView.ViewHolder
  284.     {
  285.         public TextView unreadTextView;
  286.         public UnreadSeparatorViewHolder(View itemView)
  287.         {
  288.             super(itemView);
  289.             this.unreadTextView = (TextView) itemView.findViewById(R.id.new_msgs);
  290.         }
  291.     }
  292.     public class BotHeaderViewHolder extends RecyclerView.ViewHolder
  293.     {
  294.         public BotHeaderView header;
  295.         public BotHeaderViewHolder(View itemView) {
  296.             super(itemView);
  297.             this.header = (BotHeaderView) itemView;
  298.         }
  299.     }
  300.  
  301.     public abstract class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
  302.         private AvatarImageView avatar;
  303.         private TextView dateView;
  304.         private TextView userNameView;
  305.         private TextView viewsCountView;
  306.         private MessageStatusView msgStatusView;
  307.         private View.OnClickListener avatarClickListener, viaBotClickListener;
  308.         private ReplyView replyView;
  309.         private TextView viaBotStatic, viaBotDynamic;
  310.         protected SwipeMenuLayout swipeContainer;
  311.         protected View quickReplyButton, quickShareButton;
  312.         protected View.OnClickListener selectionClickListener;
  313.         private QuickActionClickListener quickReplyClick, quickShareClick;
  314.         public DefaultViewHolder(View itemView) {
  315.             super(itemView);
  316.             swipeContainer = (SwipeMenuLayout) itemView;
  317.             selectionClickListener = new View.OnClickListener() {
  318.                 @Override
  319.                 public void onClick(View view) {
  320.                     if(!isInSelectionMode()) {
  321.                         DefaultViewHolder.this.onClick(view);
  322.                         return;
  323.                     }
  324.                     int pos = getAdapterPosition();
  325.                     if(pos >= 0)
  326.                     {
  327.                         synchronized(msgListRef)
  328.                         {
  329.                             getController().selectMessage(msgListRef.get(pos).getMessageId());
  330.                         }
  331.                     }
  332.                 }
  333.             };
  334.             quickReplyButton = swipeContainer.getRightMenuView();
  335.             quickShareButton = swipeContainer.getLeftMenuView();
  336.             quickReplyClick = new QuickActionClickListener();
  337.             quickShareClick = new QuickActionClickListener();
  338.             quickReplyClick.reply = true;
  339.             quickShareClick.reply = false;
  340.             quickReplyButton.setOnClickListener(quickReplyClick);
  341.             quickShareButton.setOnClickListener(quickShareClick);
  342.             this.avatar = (AvatarImageView) itemView.findViewById(R.id.avatar);
  343.             this.viewsCountView = (TextView) itemView.findViewById(R.id.msg_views);
  344.             this.dateView = (TextView) itemView.findViewById(R.id.msg_date);
  345.             this.userNameView = (TextView) itemView.findViewById(R.id.msg_author);
  346.             this.msgStatusView = (MessageStatusView) itemView.findViewById(R.id.msg_status);
  347.             this.replyView = (ReplyView) itemView.findViewById(R.id.reply_view);
  348.             this.replyView.setOnClickListener(new View.OnClickListener() {
  349.                 @Override
  350.                 public void onClick(View view) {
  351.                     if(isInSelectionMode()) {
  352.                         selectionClickListener.onClick(view);
  353.                         return;
  354.                     }
  355.                     int pos = getAdapterPosition();
  356.                     if(pos >= 0)
  357.                     {
  358.                         synchronized(msgListRef)
  359.                         {
  360.                             try
  361.                             {
  362.                                 MessageModel msg = msgListRef.get(pos);
  363.                                 int replyTo = msg.getReplyInfo().getReplyToMessageId();
  364.                                 getController().requestHighlight(replyTo);
  365.                                 getController().jumpTo(replyTo);
  366.                             }
  367.                             catch(Exception ignored) {}
  368.                         }
  369.                     }
  370.                 }
  371.             });
  372.             this.replyView.setOnLongClickListener(this);
  373.             this.viaBotStatic = (TextView) itemView.findViewById(R.id.msg_via_bot_static);
  374.             this.viaBotDynamic = (TextView) itemView.findViewById(R.id.msg_via_bot_dynamic);
  375.             this.avatarClickListener = new View.OnClickListener() {
  376.                 @Override
  377.                 public void onClick(View v) {
  378.                     synchronized(msgListRef)
  379.                     {
  380.                         int position = getAdapterPosition();
  381.                         if(position >= 0)
  382.                         {
  383.                             if(!isInSelectionMode()) {
  384.                                 int userId = msgListRef.get(getAdapterPosition()).getFromId();
  385.                                 if(msgListRef.get(getAdapterPosition()).messageInChannelChat() && !msgListRef.get(getAdapterPosition()).messageInSupergroup())
  386.                                     getController().didChannelAvatarClicked(msgListRef.get(getAdapterPosition()).getChatId());
  387.                                 else
  388.                                     getController().didAvatarClicked(userId);
  389.                             }
  390.                             else
  391.                             {
  392.                                 getController().selectMessage(msgListRef.get(getAdapterPosition()).getMessageId());
  393.                             }
  394.                         }
  395.                     }
  396.                 }
  397.             };
  398.             this.viaBotClickListener = new View.OnClickListener() {
  399.                 @Override
  400.                 public void onClick(View view) {
  401.                     synchronized(msgListRef)
  402.                     {
  403.                         int position = getAdapterPosition();
  404.                         if(position >= 0)
  405.                         {
  406.                             String username = msgListRef.get(getAdapterPosition()).getViaUsername();
  407.                             if(username != null && username.length() > 1) {
  408.                                 if(!isInSelectionMode())
  409.                                     getController().didViaBotClicked(username);
  410.                                 else
  411.                                     getController().selectMessage(msgListRef.get(getAdapterPosition()).getMessageId());
  412.                             }
  413.                         }
  414.                     }
  415.                 }
  416.             };
  417.             this.avatar.setOnLongClickListener(this);
  418.             this.avatar.setOnClickListener(avatarClickListener);
  419.             this.userNameView.setOnClickListener(avatarClickListener);
  420.             this.userNameView.setOnLongClickListener(this);
  421.             swipeContainer.getContentView().setOnClickListener(this);
  422.             swipeContainer.getContentView().setOnLongClickListener(this);
  423.         }
  424.         public void onPreDraw(MessageModel itm)
  425.         {
  426.             boolean highlightRequested = getController() != null && getController().getHighlightRequest() == itm.getMessageId();
  427.             itemView.setBackgroundColor(ContextCompat.getColor(itemView.getContext(), (highlightRequested || selection.contains(itm.getMessageId())) ? R.color.chat_selection : R.color.transparent));
  428.             if(highlightRequested && getController() != null)
  429.                 getController().onHighlight();
  430.             if(chatDelegate != null && !chatDelegate.canSendMessage())
  431.                 swipeContainer.setDisableRightButton(true);
  432.             else if(chatDelegate != null)
  433.                 swipeContainer.setDisableRightButton(false);
  434.             FileModel photo = itm.getFromPhoto();
  435.             int fileId = photo.getFileId();
  436.             if(fileId != 0)
  437.                 TelegramImageLoader.getInstance().loadImage(photo, itm.getFromPhotoKey(), avatar, TelegramImageLoader.POST_PROCESSING_CIRCLE | TelegramImageLoader.POST_PROCESSING_SCALE, TelegramApplication.sharedApplication().getResources().getDimensionPixelSize(R.dimen.avatar_size), TelegramApplication.sharedApplication().getResources().getDimensionPixelSize(R.dimen.avatar_size), false);
  438.             else
  439.                 TelegramImageLoader.getInstance().loadPlaceholder(itm.getFromId(), itm.getFromInitials(), itm.getFromPlaceholderKey(), avatar, false);
  440.             if(itm.getReplyInfo() != null) {
  441.                 adjustForReply(true);
  442.                 replyView.setVisibility(View.VISIBLE);
  443.                 replyView.fill(itm.getReplyInfo());
  444.             }
  445.             else {
  446.                 adjustForReply(false);
  447.                 replyView.setVisibility(View.GONE);
  448.             }
  449.             msgStatusView.attachMessage(itm);
  450.             if(BuildConfig.DEBUG)
  451.                 Log.d(TAG, String.format("adding status delegate %d", itm.getMessageId()));
  452.             statusViewDelegates.put(itm.getMessageId(), msgStatusView.getDelegate());
  453.             msgStatusView.getDelegate().processUpdateChatRead(getController().getLastReadOutMessageId());
  454.             quickReplyClick.attachedMsg = itm;
  455.             quickShareClick.attachedMsg = itm;
  456.         }
  457.         public void onDraw(MessageModel itm)
  458.         {
  459.             if(!statusViewDelegates.containsKey(itm.getMessageId()))
  460.                 statusViewDelegates.put(itm.getMessageId(), msgStatusView.getDelegate());
  461.             if(itm.messageInChannelChat() && !itm.messageInSupergroup()) {
  462.                 viewsCountView.setText(itm.getViewsCount());
  463.                 viewsCountView.setVisibility(View.VISIBLE);
  464.             }
  465.             else
  466.             {
  467.                 viewsCountView.setVisibility(View.GONE);
  468.             }
  469.             dateView.setText(itm.getDate());
  470.             userNameView.setText(itm.getFromUsername());
  471.             if(itm.getViaUsername() != null && itm.getViaId() != 0)
  472.             {
  473.                 viaBotStatic.setVisibility(View.VISIBLE);
  474.                 viaBotDynamic.setVisibility(View.VISIBLE);
  475.                 viaBotDynamic.setText(itm.getViaUsername());
  476.                 viaBotDynamic.setOnClickListener(viaBotClickListener);
  477.             }
  478.             else
  479.             {
  480.                 viaBotDynamic.setVisibility(View.GONE);
  481.                 viaBotStatic.setVisibility(View.GONE);
  482.             }
  483.         }
  484.         public void onItemDetachFromWindow()
  485.         {
  486.             quickReplyClick.attachedMsg = null;
  487.             synchronized(msgListRef)
  488.             {
  489.                 int pos = getAdapterPosition();
  490.                 if(pos >= 0)
  491.                 {
  492.                     MessageModel msg = msgListRef.get(pos);
  493.                     msgStatusView.attachMessage(null);
  494.                     if(BuildConfig.DEBUG)
  495.                         Log.d(TAG, String.format("removing status delegate %d", msg.getMessageId()));
  496.                     statusViewDelegates.remove(msg.getMessageId());
  497.                 }
  498.             }
  499.         }
  500.         @Override
  501.         public void onClick(View v)
  502.         {
  503.             try
  504.             {
  505.                 if(!isInSelectionMode())
  506.                 {
  507.                     int position = getAdapterPosition();
  508.                     if(position >= 0)
  509.                     {
  510.                         getController().didItemClicked(position);
  511.                     }
  512.                 }
  513.             }
  514.             catch(Exception ignored) {}
  515.         }
  516.         @Override
  517.         public boolean onLongClick(View v)
  518.         {
  519.             if(swipeContainer.isOpening() || swipeContainer.isOpen())
  520.                 return false;
  521.             /*if(v != swipeContainer.getContentView())
  522.                 return false;*/
  523.             if(!isInSelectionMode())
  524.             {
  525.                 int position = getAdapterPosition();
  526.                 if(position >= 0)
  527.                 {
  528.                     getController().didItemClicked(position);
  529.                     return true;
  530.                 }
  531.             }
  532.             return false;
  533.         }
  534.         protected abstract void adjustForReply(boolean hasReply);
  535.     }
  536.     public class MessageTextViewHolder extends DefaultViewHolder {
  537.         private TextView text;
  538.         public MessageTextViewHolder(View itemView) {
  539.             super(itemView);
  540.             this.text = (TextView) itemView.findViewById(R.id.message_text);
  541.         }
  542.         @Override
  543.         public void onDraw(MessageModel itm)
  544.         {
  545.             super.onDraw(itm);
  546.             TextMessageModel textMessage = ((TextMessageModel) itm);
  547.             textMessage.setDelegate(chatDelegate);
  548.             this.text.setText(textMessage.getText());
  549.             this.text.setMovementMethod(LinkMovementMethod.getInstance());
  550.             this.text.setOnLongClickListener(new View.OnLongClickListener() {
  551.                 @Override
  552.                 public boolean onLongClick(View view) {
  553.                     if(swipeContainer.isOpening())
  554.                         return false;
  555.                     int position = getAdapterPosition();
  556.                     if (position >= 0) {
  557.                         getController().didItemClicked(position);
  558.                         return true;
  559.                     }
  560.                     return false;
  561.                 }
  562.             });
  563.         }
  564.  
  565.         @Override
  566.         protected void adjustForReply(boolean hasReply) {
  567.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) text.getLayoutParams();
  568.             if(hasReply && lp.topMargin != increasedMargin) {
  569.                 lp.topMargin = increasedMargin;
  570.                 text.setLayoutParams(lp);
  571.             }
  572.             else if(!hasReply && lp.topMargin != defaultMargin)
  573.             {
  574.                 lp.topMargin = defaultMargin;
  575.                 text.setLayoutParams(lp);
  576.             }
  577.         }
  578.     }
  579.  
  580.     public class ForwardedMessageTextViewHolder extends DefaultViewHolder
  581.     {
  582.         //forwarded message
  583.         private AvatarImageView fwd_avatar;
  584.         private TextView fwd_date;
  585.         private TextView fwd_username;
  586.         private TextView fwd_message_text;
  587.         private TextView fwd_views_count;
  588.         private View fwd_container;
  589.         private View.OnClickListener fwdAvatarClickListener;
  590.  
  591.         public ForwardedMessageTextViewHolder(View itemView)
  592.         {
  593.             super(itemView);
  594.             //fwd_msg
  595.             this.fwd_container = itemView.findViewById(R.id.fwd_container);
  596.             this.fwd_avatar = (AvatarImageView) itemView.findViewById(R.id.fwd_avatar);
  597.             this.fwd_date = (TextView) itemView.findViewById(R.id.fwd_msg_date);
  598.             this.fwd_username = (TextView) itemView.findViewById(R.id.fwd_username);
  599.             this.fwd_message_text = (TextView) itemView.findViewById(R.id.fwd_message_text);
  600.             this.fwd_views_count = (TextView) itemView.findViewById(R.id.fwd_msg_views);
  601.             fwdAvatarClickListener = new View.OnClickListener() {
  602.                 @Override
  603.                 public void onClick(View v) {
  604.                     int position = getAdapterPosition();
  605.                     int fwdFromId;
  606.                     boolean channel;
  607.                     if(position >= 0)
  608.                     {
  609.                         synchronized(msgListRef)
  610.                         {
  611.                             channel = msgListRef.get(position).forwardedFromChannel();
  612.                             fwdFromId = msgListRef.get(position).getForwardFromId();
  613.                             if(!isInSelectionMode())
  614.                             {
  615.                                 if(channel)
  616.                                     getController().didChannelAvatarClicked(fwdFromId);
  617.                                 else
  618.                                     getController().didAvatarClicked(fwdFromId);
  619.                             }
  620.                             else
  621.                                 getController().selectMessage(msgListRef.get(position).getMessageId());
  622.                         }
  623.                     }
  624.                 }
  625.             };
  626.             this.fwd_avatar.setOnClickListener(fwdAvatarClickListener);
  627.             this.fwd_avatar.setOnLongClickListener(this);
  628.             this.fwd_message_text.setOnLongClickListener(this);
  629.         }
  630.         @Override
  631.         protected void adjustForReply(boolean hasReply) {
  632.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) fwd_container.getLayoutParams();
  633.             if(hasReply && lp.topMargin != increasedMargin) {
  634.                 lp.topMargin = increasedMargin;
  635.                 fwd_container.setLayoutParams(lp);
  636.             }
  637.             else if(!hasReply && lp.topMargin != defaultMargin)
  638.             {
  639.                 lp.topMargin = defaultMargin;
  640.                 fwd_container.setLayoutParams(lp);
  641.             }
  642.         }
  643.         @Override
  644.         public void onPreDraw(MessageModel itm)
  645.         {
  646.             super.onPreDraw(itm);
  647.             FileModel photo = itm.getForwardFromPhoto();
  648.             int fileId = photo.getFileId();
  649.             if(fileId != 0)
  650.                 TelegramImageLoader.getInstance().loadImage(photo, itm.getForwardFromPhotoKey(), fwd_avatar, TelegramImageLoader.POST_PROCESSING_CIRCLE | TelegramImageLoader.POST_PROCESSING_SCALE, TelegramApplication.sharedApplication().getResources().getDimensionPixelSize(R.dimen.avatar_size), TelegramApplication.sharedApplication().getResources().getDimensionPixelSize(R.dimen.avatar_size), false);
  651.             else
  652.                 TelegramImageLoader.getInstance().loadPlaceholder(itm.getForwardFromId(), itm.getForwardFromInitials(), itm.getForwardFromPlaceholderKey(), fwd_avatar, false);
  653.         }
  654.         @Override
  655.         public void onDraw(MessageModel itm)
  656.         {
  657.             super.onDraw(itm);
  658.             this.fwd_date.setText(itm.getForwardDate());
  659.             this.fwd_username.setText(itm.getForwardFromUsername());
  660.             TextMessageModel textMessage = ((TextMessageModel) itm);
  661.             textMessage.setDelegate(chatDelegate);
  662.             this.fwd_message_text.setMovementMethod(LinkMovementMethod.getInstance());
  663.             this.fwd_message_text.setText(textMessage.getText());
  664.             if(textMessage.forwardedFromChannel())
  665.             {
  666.                 this.fwd_views_count.setVisibility(View.VISIBLE);
  667.                 this.fwd_views_count.setText(textMessage.getViewsCount());
  668.             }
  669.             else
  670.                 this.fwd_views_count.setVisibility(View.GONE);
  671.         }
  672.     }
  673.  
  674.     public class MessagePhotoViewHolder extends DefaultViewHolder
  675.     {
  676.         private View imgContainer;
  677.         private RecyclingImageView imageView;
  678.         private TextView caption;
  679.         private ImageUploadView uploadView;
  680.         private View.OnClickListener imageClickListener;
  681.         private int imgMargin, imgIncreasedMargin;
  682.         public MessagePhotoViewHolder(View itemView)
  683.         {
  684.             super(itemView);
  685.             imgMargin = AndroidUtilities.dp(4);
  686.             imgIncreasedMargin = AndroidUtilities.dp(44);
  687.             imageClickListener = new View.OnClickListener() {
  688.                 @Override
  689.                 public void onClick(View v) {
  690.                     if(isInSelectionMode())
  691.                     {
  692.                         selectionClickListener.onClick(v);
  693.                         return;
  694.                     }
  695.                     synchronized(msgListRef)
  696.                     {
  697.                         MessageModel model = msgListRef.get(getAdapterPosition());
  698.                         if(model.getSuitableConstructor() == TdApi.MessagePhoto.CONSTRUCTOR && chatDelegate != null)
  699.                             chatDelegate.didPhotoClicked((PhotoMessageModel) model);
  700.                     }
  701.                 }
  702.             };
  703.             imgContainer = itemView.findViewById(R.id.msg_photo_container);
  704.             imageView = (RecyclingImageView) imgContainer.findViewById(R.id.msg_photo);
  705.             caption = (TextView) imgContainer.findViewById(R.id.photo_caption);
  706.             uploadView = (ImageUploadView) itemView.findViewById(R.id.img_upload);
  707.             imgContainer.setOnClickListener(imageClickListener);
  708.         }
  709.         @Override
  710.         protected void adjustForReply(boolean hasReply) {
  711.             RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) imgContainer.getLayoutParams();
  712.             RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams) uploadView.getLayoutParams();
  713.             if(hasReply && lp1.topMargin != imgIncreasedMargin) {
  714.                 lp1.topMargin = imgIncreasedMargin;
  715.                 lp2.topMargin = specialIncreasedMargin;
  716.                 imgContainer.setLayoutParams(lp1);
  717.                 uploadView.setLayoutParams(lp2);
  718.             }
  719.             else if(!hasReply && lp1.topMargin != imgMargin)
  720.             {
  721.                 lp1.topMargin = imgMargin;
  722.                 lp2.topMargin = specialMargin;
  723.                 imgContainer.setLayoutParams(lp1);
  724.                 uploadView.setLayoutParams(lp2);
  725.             }
  726.         }
  727.         @Override
  728.         public void onPreDraw(MessageModel itm)
  729.         {
  730.             super.onPreDraw(itm);
  731.             PhotoMessageModel msg = (PhotoMessageModel) itm;
  732.             PhotoSizeModel previewObject = msg.getPreview();
  733.             if(msg.getCaption() != null && !msg.getCaption().isEmpty())
  734.             {
  735.                 caption.setVisibility(View.VISIBLE);
  736.                 caption.setText(msg.getCaption());
  737.             }
  738.             else
  739.                 caption.setVisibility(View.GONE);
  740.             if(msg.isSending() && msg.isSentFromThisClient() && msg.getViaId() == 0)
  741.             {
  742.                 Log.d("BaseChatAdapter", "uploading".concat(previewObject.getPhoto().getFile().toString()));
  743.                 uploadView.setVisibility(View.VISIBLE);
  744.                 uploadView.setAttachedMsgId(msg.getMessageId());
  745.                 uploadView.attachDelegateToFile(previewObject.getPhoto());
  746.             }
  747.             else
  748.                 uploadView.setVisibility(View.GONE);
  749.             if(previewObject == null)
  750.                 return;
  751.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
  752.             lp.width = msg.getLayoutParamsWidth();
  753.             lp.height = msg.getLayoutParamsHeight();
  754.             imageView.setLayoutParams(lp);
  755.             TelegramImageLoader.getInstance().loadImage(previewObject.getPhoto(), msg.getPreviewKey(), imageView, TelegramImageLoader.POST_PROCESSING_NOTHING, 0, 0, false);
  756.         }
  757.     }
  758.     public class MessageAnimationViewHolder extends DefaultViewHolder
  759.     {
  760.         private AnimationView animView;
  761.         private ImageUploadView uploadView;
  762.         private int imgMargin, imgIncreasedMargin;
  763.         public MessageAnimationViewHolder(View itemView)
  764.         {
  765.             super(itemView);
  766.             imgMargin = AndroidUtilities.dp(4);
  767.             imgIncreasedMargin = AndroidUtilities.dp(44);
  768.             animView = (AnimationView) itemView.findViewById(R.id.msg_anim);
  769.             uploadView = (ImageUploadView) itemView.findViewById(R.id.anim_upload);
  770.             animView.setOnClickListener(selectionClickListener);
  771.             animView.setOnLongClickListener(this);
  772.         }
  773.         @Override
  774.         protected void adjustForReply(boolean hasReply) {
  775.             RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) animView.getLayoutParams();
  776.             RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams) uploadView.getLayoutParams();
  777.             if(hasReply && lp1.topMargin != imgIncreasedMargin) {
  778.                 lp1.topMargin = imgIncreasedMargin;
  779.                 lp2.topMargin = specialIncreasedMargin;
  780.                 animView.setLayoutParams(lp1);
  781.                 uploadView.setLayoutParams(lp2);
  782.             }
  783.             else if(!hasReply && lp1.topMargin != imgMargin)
  784.             {
  785.                 lp1.topMargin = imgMargin;
  786.                 lp2.topMargin = specialMargin;
  787.                 animView.setLayoutParams(lp1);
  788.                 uploadView.setLayoutParams(lp2);
  789.             }
  790.         }
  791.         @Override
  792.         public void onPreDraw(MessageModel itm)
  793.         {
  794.             super.onPreDraw(itm);
  795.             AnimationMessageModel msg = (AnimationMessageModel) itm;
  796.             if(msg.isSending() && msg.isSentFromThisClient() && msg.getViaId() == 0)
  797.             {
  798.                 uploadView.setVisibility(View.VISIBLE);
  799.                 uploadView.setAttachedMsgId(msg.getMessageId());
  800.                 uploadView.attachDelegateToFile(FileCache.getInstance().getFileModel(msg.getAnimation().animation));
  801.             }
  802.             else
  803.                 uploadView.setVisibility(View.GONE);
  804.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) animView.getLayoutParams();
  805.             lp.width = msg.getLayoutParamsWidth();
  806.             lp.height = msg.getLayoutParamsHeight();
  807.             animView.setLayoutParams(lp);
  808.             TelegramImageLoader.getInstance().loadAnimation(msg.getAnimation(), msg.getKey(), animView);
  809.         }
  810.     }
  811.     public class MessageStickerViewHolder extends DefaultViewHolder
  812.     {
  813.         private RecyclingImageView imageView;
  814.         public MessageStickerViewHolder(View itemView)
  815.         {
  816.             super(itemView);
  817.             imageView = (RecyclingImageView) itemView.findViewById(R.id.sticker);
  818.         }
  819.         @Override
  820.         public void onPreDraw(MessageModel itm)
  821.         {
  822.             super.onPreDraw(itm);
  823.             imageView.setOnClickListener(selectionClickListener);
  824.             StickerMessageModel msg = (StickerMessageModel) itm;
  825.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
  826.             lp.width = msg.getSticker().width;
  827.             lp.height = msg.getSticker().height;
  828.             imageView.setLayoutParams(lp);
  829.             TelegramImageLoader.getInstance().loadSticker(msg.getSticker(), TelegramImageLoader.CMD_RETURN_STICKER_FULL_SIZE, imageView);
  830.         }
  831.         @Override
  832.         protected void adjustForReply(boolean hasReply) {
  833.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
  834.             if(hasReply && lp.topMargin != increasedMargin) {
  835.                 lp.topMargin = increasedMargin;
  836.                 imageView.setLayoutParams(lp);
  837.             }
  838.             else if(!hasReply && lp.topMargin != defaultMargin)
  839.             {
  840.                 lp.topMargin = defaultMargin;
  841.                 imageView.setLayoutParams(lp);
  842.             }
  843.         }
  844.     }
  845.     public class MessageVoiceViewHolder extends DefaultViewHolder
  846.     {
  847.         private VoiceDownloadView downloadView;
  848.  
  849.         public MessageVoiceViewHolder(View itemView)
  850.         {
  851.             super(itemView);
  852.             downloadView = (VoiceDownloadView) itemView.findViewById(R.id.audio_view);
  853.         }
  854.         @Override
  855.         protected void adjustForReply(boolean hasReply) {
  856.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) downloadView.getLayoutParams();
  857.             if(hasReply && lp.topMargin != increasedMargin) {
  858.                 lp.topMargin = increasedMargin;
  859.                 downloadView.setLayoutParams(lp);
  860.             }
  861.             else if(!hasReply && lp.topMargin != defaultMargin)
  862.             {
  863.                 lp.topMargin = defaultMargin;
  864.                 downloadView.setLayoutParams(lp);
  865.             }
  866.         }
  867.         @Override
  868.         public void onDraw(MessageModel itm)
  869.         {
  870.             super.onDraw(itm);
  871.             final VoiceMessageModel msg = (VoiceMessageModel) itm;
  872.             downloadView.setDuration(msg.getDuration());
  873.             final MessageVoicePlayer.VoiceMessageDelegate voicePlaybackDelegate = new MessageVoicePlayer.VoiceMessageDelegate() {
  874.                 @Override
  875.                 public void didPlaybackStarted() {
  876.                     if(BuildConfig.DEBUG)
  877.                         Log.d(TAG, "didPlaybackStarted");
  878.                     msg.setPlaybackState(true);
  879.                     ((AudioDownloadButton) downloadView.getDownloadButton()).setPlaybackState(true);
  880.                     AudioPlaybackService playbackService = TelegramApplication.sharedApplication().getAudioPlaybackService();
  881.                     if(playbackService != null)
  882.                         playbackService.pausePlayback();
  883.                 }
  884.  
  885.                 @Override
  886.                 public void didPlaybackStopped() {
  887.                     msg.setPlaybackState(false);
  888.                     msg.resetStartMark();
  889.                     downloadView.updateProgress(0.0f);
  890.                     downloadView.setPlaybackState(false);
  891.                 }
  892.  
  893.                 @Override
  894.                 public void didPlaybackProgressUpdated() {
  895.                     if(msg.getPlaybackState() == false) {
  896.                         msg.setPlaybackState(true);
  897.                         downloadView.setPlaybackState(true);
  898.                     }
  899.                     downloadView.updateProgress(msg.getStartFrom());
  900.                 }
  901.  
  902.                 @Override
  903.                 public void didPlaybackPaused() {
  904.                     msg.setPlaybackState(false);
  905.                     downloadView.setPlaybackState(false);
  906.                 }
  907.  
  908.                 @Override
  909.                 public String getDelegateKey() {
  910.                     return msg.getDelegateKey();
  911.                 }
  912.             };
  913.             msg.getAudioFileRef().addDelegate(voicePlaybackDelegate);
  914.             downloadView.setSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  915.                 @Override
  916.                 public void onProgressChanged(SeekBar seekBar, int i, boolean fromUser) {
  917.                 }
  918.  
  919.                 @Override
  920.                 public void onStartTrackingTouch(SeekBar seekBar) {
  921.                 }
  922.  
  923.                 @Override
  924.                 public void onStopTrackingTouch(SeekBar seekBar) {
  925.                     msg.startPlaybackFrom((float) seekBar.getProgress() / 100);
  926.                     MessageVoicePlayer.getInstance().seek(msg);
  927.                 }
  928.             });
  929.             downloadView.setPlaybackState(msg.getPlaybackState());
  930.             downloadView.updateProgress(msg.getStartFrom());
  931.             downloadView.getDownloadButton().setOnClickListener(new View.OnClickListener() {
  932.                 @Override
  933.                 public void onClick(View view) {
  934.                     FileModel.DownloadState state = msg.getAudioFileRef().getDownloadState();
  935.                     if(state == FileModel.DownloadState.EMPTY)
  936.                         downloadView.startDownload();
  937.                     else if(state == FileModel.DownloadState.LOADING)
  938.                         downloadView.cancelDownload();
  939.                     else if(state == FileModel.DownloadState.LOADED && chatDelegate != null) {
  940.                         MessageVoicePlayer.getInstance().play(msg);
  941.                     }
  942.                 }
  943.             });
  944.             downloadView.setAttachedMsgId(msg.getMessageId());
  945.             downloadView.attachDelegateToFile(msg.getAudioFileRef());
  946.         }
  947.         @Override
  948.         public void onItemDetachFromWindow()
  949.         {
  950.             super.onItemDetachFromWindow();
  951.             int position = getAdapterPosition();
  952.             if(position >= 0)
  953.             {
  954.                 synchronized(msgListRef)
  955.                 {
  956.                     VoiceMessageModel msg = (VoiceMessageModel) msgListRef.get(position);
  957.                     msg.getAudioFileRef().removeVoiceMsgDelegate(msg.getDelegateKey());
  958.                 }
  959.             }
  960.             downloadView.setAttachedMsgId(0);
  961.             downloadView.attachDelegateToFile(null);
  962.         }
  963.     }
  964.     public class MessageAudioViewHolder extends DefaultViewHolder
  965.     {
  966.         private AudioDownloadView downloadView;
  967.  
  968.         public MessageAudioViewHolder(View itemView)
  969.         {
  970.             super(itemView);
  971.             downloadView = (AudioDownloadView) itemView.findViewById(R.id.audio_download_view);
  972.         }
  973.         @Override
  974.         public void onDraw(MessageModel itm)
  975.         {
  976.             super.onDraw(itm);
  977.             final AudioMessageModel msg = (AudioMessageModel) itm;
  978.             downloadView.setTrackName(msg.getTrackName());
  979.             downloadView.setBandName(msg.getBandName());
  980.             downloadView.getDownloadButton().setOnClickListener(new View.OnClickListener() {
  981.                 @Override
  982.                 public void onClick(View view) {
  983.                     FileModel.DownloadState state = msg.getAudioFileRef().getDownloadState();
  984.                     if(state == FileModel.DownloadState.EMPTY)
  985.                         downloadView.startDownload();
  986.                     else if(state == FileModel.DownloadState.LOADING)
  987.                         downloadView.cancelDownload();
  988.                     else if(state == FileModel.DownloadState.LOADED && chatDelegate != null) {
  989.                         chatDelegate.didPlayerLaunchRequested(msg.getTrackModel());
  990.                     }
  991.                 }
  992.             });
  993.             downloadView.setAttachedMsgId(msg.getMessageId());
  994.             downloadView.attachDelegateToFile(msg.getAudioFileRef());
  995.         }
  996.         @Override
  997.         public void onItemDetachFromWindow()
  998.         {
  999.             super.onItemDetachFromWindow();
  1000.             downloadView.setAttachedMsgId(0);
  1001.             downloadView.attachDelegateToFile(null);
  1002.         }
  1003.  
  1004.         @Override
  1005.         protected void adjustForReply(boolean hasReply) {
  1006.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) downloadView.getLayoutParams();
  1007.             if(hasReply && lp.topMargin != increasedMargin) {
  1008.                 lp.topMargin = increasedMargin;
  1009.                 downloadView.setLayoutParams(lp);
  1010.             }
  1011.             else if(!hasReply && lp.topMargin != defaultMargin)
  1012.             {
  1013.                 lp.topMargin = defaultMargin;
  1014.                 downloadView.setLayoutParams(lp);
  1015.             }
  1016.         }
  1017.     }
  1018.  
  1019.     public class MessageContactViewHolder extends DefaultViewHolder
  1020.     {
  1021.         private AvatarImageView avatar;
  1022.         private TextView nameTextView;
  1023.         private TextView phoneTextView;
  1024.         private View contactContainer;
  1025.         private View.OnClickListener contactPhotoClickListener;
  1026.         public MessageContactViewHolder(View itemView)
  1027.         {
  1028.             super(itemView);
  1029.             contactContainer = itemView.findViewById(R.id.contact_container);
  1030.             avatar = (AvatarImageView) itemView.findViewById(R.id.contact_avatar);
  1031.             nameTextView = (TextView) itemView.findViewById(R.id.username);
  1032.             phoneTextView = (TextView) itemView.findViewById(R.id.text);
  1033.             contactPhotoClickListener = new View.OnClickListener() {
  1034.                 @Override
  1035.                 public void onClick(View v) {
  1036.                     int position = getAdapterPosition();
  1037.                     int userId;
  1038.                     if(position >= 0)
  1039.                     {
  1040.                         synchronized(msgListRef)
  1041.                         {
  1042.                             if(!isInSelectionMode())
  1043.                             {
  1044.                                 userId = ((ContactMessageModel)msgListRef.get(getAdapterPosition())).getContactUserId();
  1045.                                 getController().didAvatarClicked(userId);
  1046.                             }
  1047.                             else
  1048.                                 getController().selectMessage(msgListRef.get(getAdapterPosition()).getMessageId());
  1049.                         }
  1050.                     }
  1051.                 }
  1052.             };
  1053.             avatar.setOnClickListener(contactPhotoClickListener);
  1054.         }
  1055.         @Override
  1056.         protected void adjustForReply(boolean hasReply) {
  1057.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) contactContainer.getLayoutParams();
  1058.             if(hasReply && lp.topMargin != increasedMargin) {
  1059.                 lp.topMargin = increasedMargin;
  1060.                 contactContainer.setLayoutParams(lp);
  1061.             }
  1062.             else if(!hasReply && lp.topMargin != defaultMargin)
  1063.             {
  1064.                 lp.topMargin = defaultMargin;
  1065.                 contactContainer.setLayoutParams(lp);
  1066.             }
  1067.         }
  1068.         @Override
  1069.         public void onPreDraw(MessageModel itm)
  1070.         {
  1071.             super.onPreDraw(itm);
  1072.             ContactMessageModel msg = (ContactMessageModel) itm;
  1073.             FileModel photo = msg.getContactPhoto();
  1074.             if(photo != null)
  1075.             {
  1076.                 int fileId = photo.getFileId();
  1077.                 if(fileId != 0)
  1078.                     TelegramImageLoader.getInstance().loadImage(photo, msg.getContactPhotoKey(), avatar, TelegramImageLoader.POST_PROCESSING_CIRCLE | TelegramImageLoader.POST_PROCESSING_SCALE, TelegramApplication.sharedApplication().getResources().getDimensionPixelSize(R.dimen.avatar_size), TelegramApplication.sharedApplication().getResources().getDimensionPixelSize(R.dimen.avatar_size), false);
  1079.             }
  1080.         }
  1081.         @Override
  1082.         public void onDraw(MessageModel itm)
  1083.         {
  1084.             super.onDraw(itm);
  1085.             ContactMessageModel msg = (ContactMessageModel) itm;
  1086.             nameTextView.setText(msg.getContactDisplayName());
  1087.             phoneTextView.setText(msg.getContactPhoneNumber());
  1088.         }
  1089.     }
  1090.  
  1091.     public class MessageDocumentViewHolder extends DefaultViewHolder
  1092.     {
  1093.         private DocumentDownloadView downloadView;
  1094.  
  1095.         public MessageDocumentViewHolder(View itemView) {
  1096.             super(itemView);
  1097.             downloadView = (DocumentDownloadView) itemView.findViewById(R.id.document_download);
  1098.         }
  1099.         @Override
  1100.         public void onPreDraw(MessageModel itm)
  1101.         {
  1102.             super.onPreDraw(itm);
  1103.             final DocumentMessageModel msg = (DocumentMessageModel) itm;
  1104.             downloadView.setHasThumb(msg.hasThumb());
  1105.             if(msg.hasThumb()) {
  1106.                 downloadView.getThumbImageView().setOnClickListener(new View.OnClickListener() {
  1107.                     @Override
  1108.                     public void onClick(View view) {
  1109.                         FileModel.DownloadState state = msg.getDocFileRef().getDownloadState();
  1110.                         if(state == FileModel.DownloadState.LOADED && chatDelegate != null)
  1111.                             chatDelegate.didFileOpenRequested(msg.getDocFileRef().getFile().path, msg.getMimeType());
  1112.                     }
  1113.                 });
  1114.                 TelegramImageLoader.getInstance().loadImage(msg.getThumbnail().getPhoto(), CommonTools.makeFileKey(msg.getThumbnail().getPhoto().getFileId()), downloadView.getThumbImageView(), TelegramImageLoader.POST_PROCESSING_NOTHING, AndroidUtilities.dp(80), AndroidUtilities.dp(80), false);
  1115.             }
  1116.             downloadView.setDocumentName(((DocumentMessageModel) itm).getDocName());
  1117.             downloadView.getDownloadButton().setOnClickListener(new View.OnClickListener() {
  1118.                 @Override
  1119.                 public void onClick(View view) {
  1120.                     FileModel.DownloadState state = msg.getDocFileRef().getDownloadState();
  1121.                     if(state == FileModel.DownloadState.EMPTY)
  1122.                         downloadView.startDownload();
  1123.                     else if(state == FileModel.DownloadState.LOADING)
  1124.                         downloadView.cancelDownload();
  1125.                     else if(state == FileModel.DownloadState.LOADED && chatDelegate != null)
  1126.                         chatDelegate.didFileOpenRequested(msg.getDocFileRef().getFile().path, msg.getMimeType());
  1127.                 }
  1128.             });
  1129.             downloadView.setAttachedMsgId(msg.getMessageId());
  1130.             downloadView.attachDelegateToFile(msg.getDocFileRef());
  1131.         }
  1132.         @Override
  1133.         protected void adjustForReply(boolean hasReply) {
  1134.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) downloadView.getLayoutParams();
  1135.             if(hasReply && lp.topMargin != increasedMargin) {
  1136.                 lp.topMargin = increasedMargin;
  1137.                 downloadView.setLayoutParams(lp);
  1138.             }
  1139.             else if(!hasReply && lp.topMargin != defaultMargin)
  1140.             {
  1141.                 lp.topMargin = defaultMargin;
  1142.                 downloadView.setLayoutParams(lp);
  1143.             }
  1144.         }
  1145.         @Override
  1146.         public void onItemDetachFromWindow()
  1147.         {
  1148.             super.onItemDetachFromWindow();
  1149.             downloadView.setAttachedMsgId(0);
  1150.             downloadView.attachDelegateToFile(null);
  1151.         }
  1152.     }
  1153.  
  1154.     public class MessageGeoPointViewHolder extends DefaultViewHolder
  1155.     {
  1156.         private RecyclingImageView locationImg;
  1157.         public MessageGeoPointViewHolder(View itemView)
  1158.         {
  1159.             super(itemView);
  1160.             locationImg = (RecyclingImageView) itemView.findViewById(R.id.location_img);
  1161.             itemView.setOnClickListener(new View.OnClickListener() {
  1162.                 @Override
  1163.                 public void onClick(View view) {
  1164.                     synchronized(msgListRef)
  1165.                     {
  1166.                         int position = getAdapterPosition();
  1167.                         if(position >= 0 && chatDelegate != null)
  1168.                         {
  1169.                             GeoPointMessageModel msg = (GeoPointMessageModel) msgListRef.get(position);
  1170.                             chatDelegate.didLocationClicked(msg.getLocation());
  1171.                         }
  1172.                     }
  1173.                 }
  1174.             });
  1175.         }
  1176.         @Override
  1177.         protected void adjustForReply(boolean hasReply) {
  1178.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) locationImg.getLayoutParams();
  1179.             if(hasReply && lp.topMargin != increasedMargin) {
  1180.                 lp.topMargin = increasedMargin;
  1181.                 locationImg.setLayoutParams(lp);
  1182.             }
  1183.             else if(!hasReply && lp.topMargin != defaultMargin)
  1184.             {
  1185.                 lp.topMargin = defaultMargin;
  1186.                 locationImg.setLayoutParams(lp);
  1187.             }
  1188.         }
  1189.         @Override
  1190.         public void onPreDraw(MessageModel itm)
  1191.         {
  1192.             super.onPreDraw(itm);
  1193.             GeoPointMessageModel msg = (GeoPointMessageModel) itm;
  1194.             TelegramImageLoader.getInstance().loadLocation(msg.getLocation(), msg.getLocationKey(), locationImg, AndroidUtilities.dp(150), AndroidUtilities.dp(100));
  1195.         }
  1196.     }
  1197.     public class MessageVenueViewHolder extends DefaultViewHolder
  1198.     {
  1199.         private RecyclingImageView img;
  1200.         private TextView title;
  1201.         private TextView address;
  1202.         private View venueContainer;
  1203.         public MessageVenueViewHolder(View itemView)
  1204.         {
  1205.             super(itemView);
  1206.             venueContainer = itemView.findViewById(R.id.venue_info_container);
  1207.             img = (RecyclingImageView) itemView.findViewById(R.id.location_pic);
  1208.             title = (TextView) itemView.findViewById(R.id.venue_title);
  1209.             address = (TextView) itemView.findViewById(R.id.venue_address);
  1210.             itemView.setOnClickListener(new View.OnClickListener() {
  1211.                 @Override
  1212.                 public void onClick(View view) {
  1213.                     synchronized(msgListRef)
  1214.                     {
  1215.                         int position = getAdapterPosition();
  1216.                         if(position >= 0 && chatDelegate != null)
  1217.                         {
  1218.                             VenueMessageModel msg = (VenueMessageModel) msgListRef.get(position);
  1219.                             chatDelegate.didLocationClicked(msg.getLocation());
  1220.                         }
  1221.                     }
  1222.                 }
  1223.             });
  1224.         }
  1225.         @Override
  1226.         public void onPreDraw(MessageModel itm)
  1227.         {
  1228.             super.onPreDraw(itm);
  1229.             VenueMessageModel msg = (VenueMessageModel) itm;
  1230.             TelegramImageLoader.getInstance().loadLocation(msg.getLocation(), msg.getLocationKey(), img, AndroidUtilities.dp(50), AndroidUtilities.dp(50));
  1231.         }
  1232.         @Override
  1233.         public void onDraw(MessageModel itm)
  1234.         {
  1235.             super.onDraw(itm);
  1236.             VenueMessageModel msg = (VenueMessageModel) itm;
  1237.             title.setText(msg.getTitle());
  1238.             address.setText(msg.getAddress());
  1239.         }
  1240.         @Override
  1241.         protected void adjustForReply(boolean hasReply) {
  1242.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) venueContainer.getLayoutParams();
  1243.             if(hasReply && lp.topMargin != increasedMargin) {
  1244.                 lp.topMargin = increasedMargin;
  1245.                 venueContainer.setLayoutParams(lp);
  1246.             }
  1247.             else if(!hasReply && lp.topMargin != defaultMargin)
  1248.             {
  1249.                 lp.topMargin = defaultMargin;
  1250.                 venueContainer.setLayoutParams(lp);
  1251.             }
  1252.         }
  1253.     }
  1254.     public class MessageVideoViewHolder extends DefaultViewHolder
  1255.     {
  1256.         private VideoDownloadView downloadView;
  1257.         public MessageVideoViewHolder(View itemView) {
  1258.             super(itemView);
  1259.             downloadView = (VideoDownloadView) itemView.findViewById(R.id.video_download_view);
  1260.         }
  1261.         @Override
  1262.         public void onPreDraw(MessageModel itm)
  1263.         {
  1264.             super.onPreDraw(itm);
  1265.             final VideoMessageModel msg = (VideoMessageModel) itm;
  1266.             PhotoSizeModel previewObject = msg.getThumb();
  1267.             FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) downloadView.getThumbImageView().getLayoutParams();
  1268.             lp.width = msg.getLayoutParamsWidth();
  1269.             lp.height = msg.getLayoutParamsHeight();
  1270.             downloadView.getThumbImageView().setLayoutParams(lp);
  1271.             TelegramImageLoader.getInstance().loadImage(previewObject.getPhoto(), msg.getThumbKey(), downloadView.getThumbImageView(), TelegramImageLoader.POST_PROCESSING_BLUR, 0, 0, false);
  1272.             downloadView.getDownloadButton().setOnClickListener(new View.OnClickListener() {
  1273.                 @Override
  1274.                 public void onClick(View view) {
  1275.                     FileModel.DownloadState state = msg.getVideoFileRef().getDownloadState();
  1276.                     if(state == FileModel.DownloadState.EMPTY)
  1277.                         downloadView.startDownload();
  1278.                     else if(state == FileModel.DownloadState.LOADING)
  1279.                         downloadView.cancelDownload();
  1280.                     else if(state == FileModel.DownloadState.LOADED && chatDelegate != null) {
  1281.                         chatDelegate.didFileOpenRequested(msg.getVideoFileRef().getFile().path, "video/*");
  1282.                     }
  1283.                 }
  1284.             });
  1285.             downloadView.setAttachedMsgId(msg.getMessageId());
  1286.             downloadView.attachDelegateToFile(msg.getVideoFileRef());
  1287.         }
  1288.         @Override
  1289.         protected void adjustForReply(boolean hasReply) {
  1290.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) downloadView.getLayoutParams();
  1291.             if(hasReply && lp.topMargin != increasedMargin) {
  1292.                 lp.topMargin = increasedMargin;
  1293.                 downloadView.setLayoutParams(lp);
  1294.             }
  1295.             else if(!hasReply && lp.topMargin != defaultMargin)
  1296.             {
  1297.                 lp.topMargin = defaultMargin;
  1298.                 downloadView.setLayoutParams(lp);
  1299.             }
  1300.         }
  1301.     }
  1302.     public class MessageWebPageViewHolder extends DefaultViewHolder
  1303.     {
  1304.         private TextView text;
  1305.         private WebPagePreview preview;
  1306.         public MessageWebPageViewHolder(View itemView) {
  1307.             super(itemView);
  1308.             text = (TextView) itemView.findViewById(R.id.message_text);
  1309.             preview = (WebPagePreview) itemView.findViewById(R.id.web_page_preview);
  1310.  
  1311.             preview.setOnClickListener(new View.OnClickListener() {
  1312.                 @Override
  1313.                 public void onClick(View view) {
  1314.                     if (BuildConfig.DEBUG)
  1315.                         Log.d(TAG, "preview click");
  1316.                     if(chatDelegate != null) {
  1317.                         int position = getAdapterPosition();
  1318.                         if(position >= 0) {
  1319.                             synchronized(msgListRef)
  1320.                             {
  1321.                                 chatDelegate.didUrlClicked(((WebPageMessageModel) msgListRef.get(position)).getPageContainer().url, false);
  1322.                             }
  1323.                         }
  1324.                     }
  1325.                 }
  1326.             });
  1327.             preview.setPhotoClickListener(new View.OnClickListener() {
  1328.                 @Override
  1329.                 public void onClick(View view) {
  1330.                     if (BuildConfig.DEBUG)
  1331.                         Log.d(TAG, "photoClick");
  1332.                         if (chatDelegate != null) {
  1333.                             int position = getAdapterPosition();
  1334.                             if (position >= 0) {
  1335.                                 synchronized (msgListRef) {
  1336.                                     chatDelegate.didPhotoClicked(((WebPageMessageModel) msgListRef.get(position)));
  1337.                                 }
  1338.                             }
  1339.                         }
  1340.                 }
  1341.             });
  1342.             preview.setOnLongClickListener(this);
  1343.         }
  1344.         @Override
  1345.         public void onDraw(MessageModel itm)
  1346.         {
  1347.             super.onDraw(itm);
  1348.             WebPageMessageModel textMessage = ((WebPageMessageModel) itm);
  1349.             textMessage.setDelegate(chatDelegate);
  1350.             text.setText(((WebPageMessageModel) itm).getText());
  1351.             text.setMovementMethod(LinkMovementMethod.getInstance());
  1352.             this.text.setOnLongClickListener(this);
  1353.             preview.attachWebPage(((WebPageMessageModel) itm).getPageContainer());
  1354.         }
  1355.         @Override
  1356.         protected void adjustForReply(boolean hasReply) {
  1357.             RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) text.getLayoutParams();
  1358.             if(hasReply && lp.topMargin != increasedMargin) {
  1359.                 lp.topMargin = increasedMargin;
  1360.                 text.setLayoutParams(lp);
  1361.             }
  1362.             else if(!hasReply && lp.topMargin != defaultMargin)
  1363.             {
  1364.                 lp.topMargin = defaultMargin;
  1365.                 text.setLayoutParams(lp);
  1366.             }
  1367.         }
  1368.         @Override
  1369.         public void onPreDraw(MessageModel itm)
  1370.         {
  1371.             super.onPreDraw(itm);
  1372.             preview.setPhoto((WebPageMessageModel) itm);
  1373.         }
  1374.     }
  1375.     public void updateId(int oldId, int newId)
  1376.     {
  1377.         if(!statusViewDelegates.containsKey(oldId))
  1378.             return;
  1379.         try
  1380.         {
  1381.             if(BuildConfig.DEBUG)
  1382.                 Log.d(TAG, String.format("removing status delegate %d; adding %d", oldId, newId));
  1383.             MessageStatusView.UpdateChatReadDelegate delegate = statusViewDelegates.get(oldId);
  1384.             statusViewDelegates.remove(oldId);
  1385.             statusViewDelegates.put(newId, delegate);
  1386.             if(getController() != null)
  1387.                 delegate.processUpdateChatRead(getController().getLastReadOutMessageId());
  1388.         }
  1389.         catch(Exception e) { e.printStackTrace(); }
  1390.     }
  1391.     public void updateStatusViews()
  1392.     {
  1393.         if(getController() == null)
  1394.             return;
  1395.         int[] keys = statusViewDelegates.keys();
  1396.         int lastReadOutbox = getController().getLastReadOutMessageId();
  1397.         for(int k : keys)
  1398.         {
  1399.             MessageStatusView.UpdateChatReadDelegate delegate = statusViewDelegates.get(k);
  1400.             if(delegate == null)
  1401.                 continue;
  1402.             delegate.processUpdateChatRead(lastReadOutbox);
  1403.         }
  1404.     }
  1405.     public boolean hasInSelection(int msgId)
  1406.     {
  1407.         return selection.contains(msgId);
  1408.     }
  1409.     public void toggleSelection(int msgId, int index)
  1410.     {
  1411.         synchronized(msgListRef)
  1412.         {
  1413.             MessageModel msg = msgListRef.get(index);
  1414.             if(msg.getMessageId() == msgId)
  1415.             {
  1416.                 if(selection.contains(msgId))
  1417.                     selection.remove(msgId);
  1418.                 else
  1419.                     selection.add(msgId);
  1420.                 notifyItemChanged(index);
  1421.             }
  1422.         }
  1423.     }
  1424.     public void removeFromSelectionSet(int messageId)
  1425.     {
  1426.         if(selection.contains(messageId))
  1427.             selection.remove(messageId);
  1428.     }
  1429.     public void clearSelectionSet()
  1430.     {
  1431.         selection.clear();
  1432.     }
  1433.     public boolean isInSelectionMode() { return !this.selection.isEmpty(); }
  1434.     public int[] getSelection() { return this.selection.toArray(); }
  1435.     public int getSelectionCount() { return this.selection.size(); }
  1436.     public void setChatDelegate(BaseChatFragment.ChatDelegate delegate)
  1437.     {
  1438.         this.chatDelegate = delegate;
  1439.     }
  1440.  
  1441.     public class ServiceMsgViewHolder extends RecyclerView.ViewHolder
  1442.     {
  1443.         public TextView serviceMessageText;
  1444.         public ImageView newChatPhoto;
  1445.         public ServiceMsgViewHolder(View itemView)
  1446.         {
  1447.             super(itemView);
  1448.             this.serviceMessageText = (TextView) itemView.findViewById(R.id.chat_service_message);
  1449.             this.serviceMessageText.setMovementMethod(LinkMovementMethod.getInstance());
  1450.             this.newChatPhoto = (ImageView) itemView.findViewById(R.id.chat_new_photo);
  1451.         }
  1452.     }
  1453.     private class QuickActionClickListener implements View.OnClickListener
  1454.     {
  1455.         private MessageModel attachedMsg;
  1456.         private boolean reply;
  1457.  
  1458.         @Override
  1459.         public void onClick(View view) {
  1460.             if(attachedMsg == null || chatDelegate == null)
  1461.                 return;
  1462.             if(reply)
  1463.                 chatDelegate.didQuickReplyRequested(attachedMsg);
  1464.             else
  1465.                 chatDelegate.didQuickShareRequested(attachedMsg);
  1466.         }
  1467.     }
Advertisement
Add Comment
Please, Sign In to add comment