/**
   * Bind an instance of the <code>Chat</code> class to our view. This method is called by <code>
   * FirebaseListAdapter</code> when there is a data change, and we are given an instance of a View
   * that corresponds to the layout that we passed to the constructor, as well as a single <code>
   * Chat</code> instance that represents the current data to bind.
   *
   * @param view A view instance corresponding to the layout we passed to the constructor.
   * @param chat An instance representing the current state of a chat message
   */
  @Override
  protected void populateView(View view, Chat chat) {
    // Map a Chat object to an entry in our listview
    String author = chat.getAuthor();
    TextView authorText = (TextView) view.findViewById(R.id.author);
    authorText.setText(author + ": ");
    instanceOfVolley = VolleySingleton.newInstance(context);

    final NetworkImageView imageView = (NetworkImageView) view.findViewById(R.id.networkImageView);
    final ImageLoader imageLoader = instanceOfVolley.getImageLoader();
    final TextView titleTextView = (TextView) view.findViewById(R.id.title);
    final TextView descriptionTextView = (TextView) view.findViewById(R.id.description);

    // If the message was sent by this user, color it differently
    if (author.equals(username)) {
      authorText.setTextColor(Color.RED);
    } else {
      authorText.setTextColor(Color.BLUE);
    }
    ((TextView) view.findViewById(R.id.message)).setText(chat.getMessage());

    String message = chat.getMessage();
    int start = message.indexOf("((");
    if (start != -1) {
      String query = message.substring(start + 2, message.indexOf("))"));

      // the volley listener
      listener =
          new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
              try {
                JSONArray items = response.getJSONArray("items");
                JSONObject json = items.getJSONObject(0);
                JSONObject id = json.getJSONObject("id");
                JSONObject snippet = json.getJSONObject("snippet");
                JSONObject thumbnails = snippet.getJSONObject("thumbnails");
                JSONObject high = thumbnails.getJSONObject("high");
                String url = high.getString("url");
                String description = snippet.getString("description");
                String title = snippet.getString("title");
                titleTextView.setText(title);
                descriptionTextView.setText(description);
                imageView.setImageUrl(url, imageLoader);
                videoID = id.getString("videoId");
                imageView.setVisibility(View.VISIBLE);
                titleTextView.setVisibility(View.VISIBLE);
                descriptionTextView.setVisibility(View.VISIBLE);
                imageView.setTag(videoID);
                imageView.setOnClickListener(
                    new View.OnClickListener() {

                      @Override
                      public void onClick(View view) {
                        String tag = view.getTag().toString();
                        Intent intent = new Intent(activity, PlayActivity.class);
                        intent.putExtra("id", tag);
                        activity.startActivity(intent);
                      }
                    });
              } catch (JSONException ex) {
                ex.printStackTrace();
              }
            }
          };

      // the volley error listener
      errorListener =
          new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
              error.printStackTrace();
            }
          };

      try {
        url = Config.URL + URLEncoder.encode(query, "utf-8");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }

      JsonObjectRequest req = new JsonObjectRequest(url, null, listener, errorListener);
      instanceOfVolley.addToRequestQueue(req);

    } else {
      imageView.setVisibility(View.GONE);
      titleTextView.setVisibility(View.GONE);
      descriptionTextView.setVisibility(View.GONE);
    }
  }
Пример #2
0
  public void setPost(final Post post, final ThreadManager manager) {
    this.post = post;
    this.manager = manager;

    highlightQuotesNo = -1;

    boolean boardCatalogMode =
        manager.getLoadable().isBoardMode() || manager.getLoadable().isCatalogMode();

    TypedArray ta =
        context.obtainStyledAttributes(null, R.styleable.PostView, R.attr.post_style, 0);

    if (!isBuild) {
      buildView(context, ta);
      isBuild = true;
    }

    int dateColor = ta.getColor(R.styleable.PostView_date_color, 0);
    int savedReplyColor = ta.getColor(R.styleable.PostView_saved_reply_color, 0);
    int highlightedColor = ta.getColor(R.styleable.PostView_highlighted_color, 0);
    int detailSize = ta.getDimensionPixelSize(R.styleable.PostView_detail_size, 0);

    ta.recycle();

    if (post.hasImage) {
      imageView.setVisibility(View.VISIBLE);
      imageView.setImageUrl(post.thumbnailUrl, ChanApplication.getVolleyImageLoader());
    } else {
      imageView.setVisibility(View.GONE);
      imageView.setImageUrl(null, null);
    }

    CharSequence total = new SpannableString("");

    if (post.subjectSpan != null) {
      total = TextUtils.concat(total, post.subjectSpan);
    }

    if (isList()) {
      CharSequence relativeTime =
          DateUtils.getRelativeTimeSpanString(
              post.time * 1000L, Time.get(), DateUtils.SECOND_IN_MILLIS, 0);
      SpannableString date = new SpannableString("No." + post.no + " " + relativeTime);
      date.setSpan(new ForegroundColorSpan(dateColor), 0, date.length(), 0);
      date.setSpan(new AbsoluteSizeSpan(detailSize), 0, date.length(), 0);

      total =
          TextUtils.concat(
              total,
              post.subjectSpan == null ? "" : "\n",
              post.nameTripcodeIdCapcodeSpan,
              date,
              " ");
    }

    if (!TextUtils.isEmpty(total)) {
      titleView.setText(total);
      titleView.setVisibility(View.VISIBLE);
    } else {
      titleView.setVisibility(View.GONE);
    }

    commentView.setText(post.comment);

    if (manager.getLoadable().isThreadMode()) {
      post.setLinkableListener(this);
      commentView.setMovementMethod(new PostViewMovementMethod());
      commentView.setOnClickListener(this);
    } else {
      post.setLinkableListener(null);
      commentView.setOnClickListener(null);
      commentView.setClickable(false);
      commentView.setMovementMethod(null);
    }

    if (isGrid()
        || ((post.isOP && boardCatalogMode && post.replies > 0) || (post.repliesFrom.size() > 0))) {
      repliesCountView.setVisibility(View.VISIBLE);

      String text = "";

      int count = boardCatalogMode ? post.replies : post.repliesFrom.size();

      if (count != 1) {
        text = count + " " + context.getString(R.string.multiple_replies);
      } else if (count == 1) {
        text = count + " " + context.getString(R.string.one_reply);
      }

      if (boardCatalogMode && post.images > 0) {
        if (post.images != 1) {
          text += ", " + post.images + " " + context.getString(R.string.multiple_images);
        } else {
          text += ", " + post.images + " " + context.getString(R.string.one_image);
        }
      }

      if (manager.getLoadable().isThreadMode()) {
        repliesCountView.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                manager.showPostReplies(post);
              }
            });
      }

      repliesCountView.setText(text);
    } else {
      repliesCountView.setVisibility(View.GONE);
      repliesCountView.setOnClickListener(null);
    }

    boolean showCountryFlag =
        isList() && !TextUtils.isEmpty(post.country) && !TextUtils.isEmpty(post.countryUrl);
    boolean showStickyIcon = isList() && post.sticky;
    boolean showDeletedIcon = isList() && post.deleted;
    boolean showArchivedIcon = isList() && post.archived;
    boolean showClosedIcon = isList() && post.closed && !showArchivedIcon;

    iconsView.setVisibility(
        (showCountryFlag || showStickyIcon || showClosedIcon || showDeletedIcon || showArchivedIcon)
            ? View.VISIBLE
            : View.GONE);

    stickyView.setVisibility(showStickyIcon ? View.VISIBLE : View.GONE);
    closedView.setVisibility(showClosedIcon ? View.VISIBLE : View.GONE);
    deletedView.setVisibility(showDeletedIcon ? View.VISIBLE : View.GONE);
    archivedView.setVisibility(showArchivedIcon ? View.VISIBLE : View.GONE);
    if (showCountryFlag) {
      countryView.setVisibility(View.VISIBLE);
      countryView.setImageUrl(post.countryUrl, ChanApplication.getVolleyImageLoader());
    } else {
      countryView.setVisibility(View.GONE);
      countryView.setImageUrl(null, null);
    }

    if (post.isSavedReply) {
      full.setBackgroundColor(savedReplyColor);
    } else if (manager.isPostHightlighted(post)) {
      full.setBackgroundColor(highlightedColor);
    } else {
      full.setBackgroundColor(0x00000000);
    }

    if (manager.isPostLastSeen(post)) {
      lastSeen.setVisibility(View.VISIBLE);
    } else {
      lastSeen.setVisibility(View.GONE);
    }
  }