Ejemplo n.º 1
0
    public ItemViewHolder(View itemView) {
      super(itemView);
      slo_swipe = (SwipeLayout) itemView.findViewById(R.id.item_madapt_swipe);
      btn_star = (StarCheckBox) itemView.findViewById(R.id.item_madapt_starcheck);

      tv_engword = (TextView) itemView.findViewById(R.id.item_madapt_tv_eng);
      tv_korword = (TextView) itemView.findViewById(R.id.item_madapt_tv_kor);

      bt_open = (Button) itemView.findViewById(R.id.item_madapt_bt_open);
      bt_cancel = (Button) itemView.findViewById(R.id.item_madapt_bt_cancel);

      btn_star.setOnClickListener(clickListener);
      tv_korword.setOnClickListener(clickListener);
      bt_open.setOnClickListener(clickListener);
      bt_cancel.setOnClickListener(clickListener);

      slo_swipe.setShowMode(SwipeLayout.ShowMode.PullOut);
      slo_swipe.addSwipeListener(swipeListener);
    }
  @SuppressLint("NewApi")
  public void appendMessageData(final JSONObject message) {
    final JSONObject m_message = message;

    final LinearLayout list = (LinearLayout) findViewById(R.id.linearlayout_main);

    SwipeLayout newCell_host = (SwipeLayout) (View.inflate(this, R.layout.cell_message, null));
    // set show mode.
    newCell_host.setShowMode(SwipeLayout.ShowMode.PullOut);
    // add drag edge.(If the BottomView has 'layout_gravity' attribute, this line is unnecessary)

    newCell_host.setDragEdge(DragEdge.Right);
    newCell_host.setBottom(R.id.right_wrapper);

    ImageView img_delete = (ImageView) newCell_host.findViewById(R.id.imageView_delete);
    img_delete.setImageResource(R.drawable.remove);
    img_delete.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View v) {
            // TODO Auto-generated method stub
            progressView.setVisibility(View.VISIBLE);
            disableButtons();
            String msg_id;
            try {
              msg_id = message.getString("msg_id");
              new RemoveMessage(msg_id).execute();
            } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        });

    TextView message_title = (TextView) newCell_host.findViewById(R.id.textView_message_title);
    TextView message_subtitle =
        (TextView) newCell_host.findViewById(R.id.textView_message_sub_title);
    ImageView messageButton = (ImageView) newCell_host.findViewById(R.id.message_Button);
    messageButton.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
              int action_code = m_message.getInt("action_code");
              String action_data = m_message.getString("action_data");
              String message_id = m_message.getString("msg_id");
              switch (action_code) {
                case 3:
                  progressView.setVisibility(View.VISIBLE);
                  disableButtons();
                  AcceptInvite acceptInvite = new AcceptInvite(message_id, action_data);
                  acceptInvite.execute();
                  break;

                case 4:
                  progressView.setVisibility(View.VISIBLE);
                  disableButtons();
                  GetConf getConf = new GetConf(action_data);
                  getConf.execute();
                  break;
                default:
                  break;
              }
            } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        });

    TextView messageButtonNote = (TextView) newCell_host.findViewById(R.id.message_Button_Note);

    try {
      message_title.setText(message.getString("title"));
      message_subtitle.setText(message.getString("subtitle"));

      int action_code = message.getInt("action_code");
      switch (action_code) {
        case 1:
          messageButton.setVisibility(View.INVISIBLE);
          messageButtonNote.setVisibility(View.INVISIBLE);
          break;
        case 2:
          messageButton.setImageResource(R.drawable.add_host);
          messageButtonNote.setText("已加入");
          messageButtonNote.setTextColor(R.color.green_overlay);
          break;
        case 3:
          messageButton.setImageResource(R.drawable.join);
          messageButtonNote.setText(R.string.str_join_in);
          messageButtonNote.setTextColor(R.color.green);
          break;
        case 4:
          messageButton.setImageResource(R.drawable.right);
          messageButtonNote.setText("查看");
          messageButtonNote.setTextColor(R.color.grey);
          break;
        default:
          break;
      }
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    newCell_host.setTag(0);
    registerForContextMenu(newCell_host);
    list.addView(newCell_host);
  }
    public void fillValues(final int position, final View convertView) {

      Uri convId = conversations.get(position).getId();
      Conversation conv = layerClient.getConversation(convId);

      ArrayList<String> allButMe = new ArrayList<String>(conv.getParticipants());
      allButMe.remove(globalUserId);

      TextView textTitle =
          (TextView)
              convertView.findViewById(
                  com.layer.atlas.R.id.atlas_conversation_view_convert_participant);
      String conversationTitle = Atlas.getTitle(conv, participantProvider, globalUserId);
      textTitle.setText(conversationTitle);

      // avatar icons...
      TextView textInitials =
          (TextView)
              convertView.findViewById(
                  com.layer.atlas.R.id.atlas_view_conversations_list_convert_avatar_single_text);
      View avatarSingle =
          convertView.findViewById(
              com.layer.atlas.R.id.atlas_view_conversations_list_convert_avatar_single);
      View avatarMulti =
          convertView.findViewById(
              com.layer.atlas.R.id.atlas_view_conversations_list_convert_avatar_multi);
      ImageView imageView =
          (ImageView)
              convertView.findViewById(
                  com.layer.atlas.R.id.atlas_view_conversations_list_convert_avatar_single_image);
      //    if (allButMe.size() < 3 && allButMe.contains("1")) {
      String conterpartyUserId = allButMe.get(0);
      Atlas.Participant participant = participantProvider.getParticipant(conterpartyUserId);

      // hide textInitials
      textInitials.setText(participant == null ? null : Atlas.getInitials(participant));
      textInitials.setTextColor(avatarTextColor);
      ((GradientDrawable) textInitials.getBackground()).setColor(avatarBackgroundColor);
      textInitials.setVisibility(View.GONE);

      if (conv.getMetadata().get("counselor") != null && (accountType == 0 || accountType == 2)) {
        String counselorIdMetadataUpper = (String) conv.getMetadata().get("counselor.ID");
        Log.d("counselorIdMetadata", "counselorIdMetadataUpper" + counselorIdMetadataUpper);
        if (getBitmapFromCache(counselorIdMetadataUpper.toLowerCase()) == null) {
          Log.d("Loading Image", "Loading Image " + "Loading Image");

          new LoadImage(imageView, conv)
              .execute((String) conv.getMetadata().get("counselor.avatarString"));
        } else {
          RoundImage roundImage =
              new RoundImage(getBitmapFromCache(counselorIdMetadataUpper.toLowerCase()));
          imageView.setImageDrawable(roundImage);
        }
        if (accountType == 2) {
          String titleText =
              conv.getMetadata().get("student.ID")
                  + ", "
                  + conv.getMetadata().get("counselor.name");
          textTitle.setText(titleText);
        } else {
          textTitle.setText((String) conv.getMetadata().get("counselor.name"));
        }
      } else if (conv.getMetadata().get("student") != null && accountType == 1) {

        if (conv.getMetadata().get("student.name").equals("")) {
          conv.putMetadataAtKeyPath("student.name", generateRandomTreeName());
          Log.d("name set check", "name set check" + conv.getMetadata().get("student.name"));
        }
        if (getBitmapFromCache(((String) conv.getMetadata().get("student.ID")).toLowerCase())
            == null) {
          Log.d("Loading Image", "Loading Image " + "Loading Image");
          new LoadImage(imageView, conv)
              .execute((String) conv.getMetadata().get("student.avatarString"));
        } else {
          Log.d("cached", "cachedConversationList");
          RoundImage roundImage =
              new RoundImage(
                  getBitmapFromCache(
                      ((String) conv.getMetadata().get("student.ID")).toLowerCase()));
          imageView.setImageDrawable(roundImage);
        }

        textTitle.setText((String) conv.getMetadata().get("student.name"));
      }

      avatarSingle.setVisibility(View.VISIBLE);
      avatarMulti.setVisibility(View.GONE);

      // Multi Avatar-for later use
      /*  } else {
          Atlas.Participant leftParticipant = null;
          Atlas.Participant rightParticipant = null;
          for (Iterator<String> itUserId = allButMe.iterator(); itUserId.hasNext();) {
              String userId = itUserId.next();
              Atlas.Participant p = participantProvider.getParticipant(userId);
              if (p == null) continue;

              if (leftParticipant == null) {
                  leftParticipant = p;
              } else {
                  rightParticipant = p;
                  break;
              }
          }

          TextView textInitialsLeft = (TextView) convertView.findViewById(com.layer.atlas.R.id.atlas_view_conversations_list_convert_avatar_multi_left);
          textInitialsLeft.setText(leftParticipant == null ? "?" : Atlas.getInitials(leftParticipant));
          textInitialsLeft.setTextColor(avatarTextColor);
          ((GradientDrawable) textInitialsLeft.getBackground()).setColor(avatarBackgroundColor);

          TextView textInitialsRight = (TextView) convertView.findViewById(com.layer.atlas.R.id.atlas_view_conversations_list_convert_avatar_multi_right);
          textInitialsRight.setText(rightParticipant == null ? "?" : Atlas.getInitials(rightParticipant));
          textInitialsRight.setTextColor(avatarTextColor);
          ((GradientDrawable) textInitialsRight.getBackground()).setColor(avatarBackgroundColor);

          avatarSingle.setVisibility(View.GONE);
          avatarMulti.setVisibility(View.VISIBLE);
      }*/

      TextView textLastMessage =
          (TextView)
              convertView.findViewById(com.layer.atlas.R.id.atlas_conversation_view_last_message);
      TextView timeView =
          (TextView)
              convertView.findViewById(com.layer.atlas.R.id.atlas_conversation_view_convert_time);
      if (conv.getLastMessage() != null) {

        Message last = conv.getLastMessage();
        boolean isReportMessageLast;
        do {
          isReportMessageLast = false;
          for (MessagePart part : last.getMessageParts()) {
            String messageText = new String(part.getData());
            if (messageText.equals("Conversation Reported")) {
              isReportMessageLast = true;
              last =
                  layerClient
                      .getMessages(conv)
                      .get((layerClient.getMessages(conv).indexOf(last) - 1));
            }
          }
        } while (isReportMessageLast);

        String lastMessageText = Atlas.Tools.toString(last);

        textLastMessage.setText(lastMessageText);

        Date sentAt = last.getSentAt();
        if (sentAt == null) timeView.setText("...");
        else timeView.setText(formatTime(sentAt));

        String userId = last.getSender().getUserId(); // could be null for system messages
        String myId = globalUserId;

        if ((userId != null)
            && !userId.equals(myId)
            && last.getRecipientStatus(myId) != RecipientStatus.READ) {
          textTitle.setTextColor(titleUnreadTextColor);
          textTitle.setTypeface(titleUnreadTextTypeface, titleUnreadTextStyle);
          textLastMessage.setTypeface(subtitleUnreadTextTypeface, subtitleUnreadTextStyle);
          textLastMessage.setTextColor(subtitleUnreadTextColor);
          convertView.setBackgroundColor(cellUnreadBackgroundColor);
        } else {
          textTitle.setTextColor(titleTextColor);
          textTitle.setTypeface(titleTextTypeface, titleTextStyle);
          textLastMessage.setTypeface(subtitleTextTypeface, subtitleTextStyle);
          textLastMessage.setTextColor(subtitleTextColor);
          convertView.setBackgroundColor(cellBackgroundColor);
        }
      } else {
        timeView.setText("...");
        textLastMessage.setText("");
        textTitle.setTextColor(titleTextColor);
        textTitle.setTypeface(titleTextTypeface, titleTextStyle);
        textLastMessage.setTypeface(subtitleTextTypeface, subtitleTextStyle);
        textLastMessage.setTextColor(subtitleTextColor);
        convertView.setBackgroundColor(cellBackgroundColor);
      }
      timeView.setTextColor(dateTextColor);
      swipeLayout =
          (SwipeLayout) convertView.findViewById(com.layer.atlas.R.id.swipeconversationlistitem);
      swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
      swipeLayout.addDrag(
          SwipeLayout.DragEdge.Left, convertView.findViewById(com.layer.atlas.R.id.bottom_wrapper));
      ImageView trash = (ImageView) convertView.findViewById(com.layer.atlas.R.id.trash);
      trash.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View view) {
              Conversation deleteConversation = (Conversation) getItem(position);
              deleteConversation.delete(LayerClient.DeletionMode.ALL_PARTICIPANTS);
              conversations.remove(getItem(position));
              swipeLayout.close(false);
              notifyDataSetChanged();

              updateValues();

              if (mEventListener != null) {
                mEventListener.onConversationDeleted();
              }
            }
          });
      ImageView reportButton = (ImageView) convertView.findViewById(R.id.report_student);
      if (accountType == 0) {
        if (conv.getParticipants().size() > 2) {
          trash.setVisibility(View.GONE);
        }
        reportButton.setVisibility(View.GONE);
      } else {
        if (conv.getParticipants().size() > 2) {
          reportButton.setImageResource(R.drawable.ic_undo_white_24dp);
        }
        reportButton.setOnClickListener(
            new OnClickListener() {
              @Override
              public void onClick(View view) {
                Conversation conv = (Conversation) getItem(position);
                if (conv.getParticipants().size() > 2) {
                  getWarningAlertDialog(R.string.undo_warning, R.string.undo, conv, convertView)
                      .show();
                } else {
                  getWarningAlertDialog(R.string.report_warning, R.string.report, conv, convertView)
                      .show();
                }
              }
            });
      }
    }