Search in sources :

Example 1 with PrivateMessage

use of net.dean.jraw.models.PrivateMessage in project Slide by ccrama.

the class InboxAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int pos) {
    int i = pos != 0 ? pos - 1 : pos;
    if (!(viewHolder instanceof ContributionAdapter.EmptyViewHolder) && !(viewHolder instanceof SpacerViewHolder)) {
        final MessageViewHolder messageViewHolder = (MessageViewHolder) viewHolder;
        final Message comment = dataSet.posts.get(i);
        messageViewHolder.time.setText(TimeUtils.getTimeAgo(comment.getCreated().getTime(), mContext));
        SpannableStringBuilder titleString = new SpannableStringBuilder();
        String author = comment.getAuthor();
        String direction = "from ";
        if (!dataSet.where.contains("mod") && comment.getDataNode().has("dest") && !Authentication.name.equalsIgnoreCase(comment.getDataNode().get("dest").asText()) && !comment.getDataNode().get("dest").asText().equals("reddit")) {
            author = comment.getDataNode().get("dest").asText().replace("#", "/r/");
            direction = "to ";
        }
        if (comment.getDataNode().has("subreddit") && author == null || author.isEmpty()) {
            direction = "via /r/" + comment.getSubreddit();
        }
        titleString.append(direction);
        if (author != null) {
            titleString.append(author);
            titleString.append(" ");
            if (UserTags.isUserTagged(author)) {
                SpannableStringBuilder pinned = new SpannableStringBuilder(" " + UserTags.getUserTag(author) + " ");
                pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_blue_500, false), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                titleString.append(pinned);
                titleString.append(" ");
            }
            if (UserSubscriptions.friends.contains(author)) {
                SpannableStringBuilder pinned = new SpannableStringBuilder(" " + mContext.getString(R.string.profile_friend) + " ");
                pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_deep_orange_500, false), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                titleString.append(pinned);
                titleString.append(" ");
            }
        }
        String spacer = mContext.getString(R.string.submission_properties_seperator);
        if (comment.getDataNode().has("subreddit") && !comment.getDataNode().get("subreddit").isNull()) {
            titleString.append(spacer);
            String subname = comment.getDataNode().get("subreddit").asText();
            SpannableStringBuilder subreddit = new SpannableStringBuilder("/r/" + subname);
            if ((SettingValues.colorSubName && Palette.getColor(subname) != Palette.getDefaultColor())) {
                subreddit.setSpan(new ForegroundColorSpan(Palette.getColor(subname)), 0, subreddit.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                subreddit.setSpan(new StyleSpan(Typeface.BOLD), 0, subreddit.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            titleString.append(subreddit);
        }
        messageViewHolder.user.setText(titleString);
        SpannableStringBuilder b = new SpannableStringBuilder();
        if (mContext instanceof Inbox && comment.getCreated().getTime() > ((Inbox) mContext).last && !comment.isRead()) {
            SpannableStringBuilder tagNew = new SpannableStringBuilder("\u00A0NEW\u00A0");
            tagNew.setSpan(new RoundedBackgroundSpan(Color.WHITE, mContext.getResources().getColor(R.color.md_green_400), true, mContext), 0, tagNew.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            b.append(tagNew);
            b.append(" ");
        }
        b.append(comment.getSubject());
        if (comment.getDataNode().has("link_title")) {
            SpannableStringBuilder link = new SpannableStringBuilder(" " + Html.fromHtml(comment.getDataNode().get("link_title").asText()) + " ");
            link.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            link.setSpan(new RelativeSizeSpan(0.8f), 0, link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            b.append(link);
        }
        messageViewHolder.title.setText(b);
        if (comment.isRead()) {
            messageViewHolder.title.setTextColor(messageViewHolder.content.getCurrentTextColor());
        } else {
            messageViewHolder.title.setTextColor(ContextCompat.getColor(mContext, R.color.md_red_400));
        }
        messageViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                int[] attrs = new int[] { R.attr.tintColor };
                TypedArray ta = mContext.obtainStyledAttributes(attrs);
                final int color = ta.getColor(0, Color.WHITE);
                Drawable profile = mContext.getResources().getDrawable(R.drawable.profile);
                final Drawable reply = mContext.getResources().getDrawable(R.drawable.reply);
                Drawable unhide = mContext.getResources().getDrawable(R.drawable.ic_visibility);
                Drawable hide = mContext.getResources().getDrawable(R.drawable.hide);
                Drawable copy = mContext.getResources().getDrawable(R.drawable.ic_content_copy);
                Drawable reddit = mContext.getResources().getDrawable(R.drawable.commentchange);
                profile.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                hide.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                copy.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                reddit.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                reply.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                unhide.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                ta.recycle();
                BottomSheet.Builder b = new BottomSheet.Builder((Activity) mContext).title(Html.fromHtml(comment.getSubject()));
                String author = comment.getAuthor();
                if (!dataSet.where.contains("mod") && comment.getDataNode().has("dest") && !Authentication.name.equalsIgnoreCase(comment.getDataNode().get("dest").asText()) && !comment.getDataNode().get("dest").asText().equals("reddit")) {
                    author = comment.getDataNode().get("dest").asText().replace("#", "/r/");
                }
                if (comment.getAuthor() != null) {
                    b.sheet(1, profile, "/u/" + author);
                }
                String read = mContext.getString(R.string.mail_mark_read);
                Drawable rDrawable = hide;
                if (comment.isRead()) {
                    read = mContext.getString(R.string.mail_mark_unread);
                    rDrawable = unhide;
                }
                b.sheet(2, rDrawable, read);
                b.sheet(3, reply, mContext.getString(R.string.btn_reply));
                b.sheet(25, copy, mContext.getString(R.string.misc_copy_text));
                if (comment.isComment()) {
                    b.sheet(30, reddit, mContext.getString(R.string.mail_view_full_thread));
                }
                final String finalAuthor = author;
                b.listener(new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch(which) {
                            case 1:
                                {
                                    Intent i = new Intent(mContext, Profile.class);
                                    i.putExtra(Profile.EXTRA_PROFILE, finalAuthor);
                                    mContext.startActivity(i);
                                }
                                break;
                            case 2:
                                {
                                    if (comment.isRead()) {
                                        comment.read = false;
                                        new AsyncSetRead(false).execute(comment);
                                        messageViewHolder.title.setTextColor(ContextCompat.getColor(mContext, R.color.md_red_400));
                                    } else {
                                        comment.read = true;
                                        new AsyncSetRead(true).execute(comment);
                                        messageViewHolder.title.setTextColor(messageViewHolder.content.getCurrentTextColor());
                                    }
                                }
                                break;
                            case 3:
                                {
                                    doInboxReply(comment);
                                }
                                break;
                            case 25:
                                {
                                    ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                                    ClipData clip = ClipData.newPlainText("Message", comment.getBody());
                                    clipboard.setPrimaryClip(clip);
                                    Toast.makeText(mContext, mContext.getString(R.string.mail_message_copied), Toast.LENGTH_SHORT).show();
                                }
                                break;
                            case 30:
                                {
                                    String context = comment.getDataNode().get("context").asText();
                                    new OpenRedditLink(mContext, "https://reddit.com" + context.substring(0, context.lastIndexOf("/")));
                                }
                                break;
                        }
                    }
                }).show();
                return true;
            }
        });
        messageViewHolder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (comment.isRead()) {
                    if (comment instanceof PrivateMessage) {
                        DataShare.sharedMessage = (PrivateMessage) comment;
                        Intent i = new Intent(mContext, SendMessage.class);
                        i.putExtra(SendMessage.EXTRA_NAME, comment.getAuthor());
                        i.putExtra(SendMessage.EXTRA_REPLY, true);
                        mContext.startActivity(i);
                    } else {
                        new OpenRedditLink(mContext, comment.getDataNode().get("context").asText());
                    }
                } else {
                    comment.read = true;
                    new AsyncSetRead(true).execute(comment);
                    messageViewHolder.title.setTextColor(messageViewHolder.content.getCurrentTextColor());
                    {
                        SpannableStringBuilder b = new SpannableStringBuilder(comment.getSubject());
                        if (comment.getDataNode().has("link_title")) {
                            SpannableStringBuilder link = new SpannableStringBuilder(" " + Html.fromHtml(comment.getDataNode().get("link_title").asText()) + " ");
                            link.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            link.setSpan(new RelativeSizeSpan(0.8f), 0, link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            b.append(link);
                        }
                        messageViewHolder.title.setText(b);
                    }
                }
            }
        });
        // Set typeface for body
        int type = new FontPreferences(mContext).getFontTypeComment().getTypeface();
        Typeface typeface;
        if (type >= 0) {
            typeface = RobotoTypefaces.obtainTypeface(mContext, type);
        } else {
            typeface = Typeface.DEFAULT;
        }
        messageViewHolder.content.setTypeface(typeface);
        setViews(comment.getDataNode().get("body_html").asText(), "FORCE_LINK_CLICK", messageViewHolder);
    }
    if (viewHolder instanceof SpacerViewHolder) {
        viewHolder.itemView.findViewById(R.id.height).setLayoutParams(new LinearLayout.LayoutParams(viewHolder.itemView.getWidth(), ((Activity) mContext).findViewById(R.id.header).getHeight()));
    }
}
Also used : SendMessage(me.ccrama.redditslide.Activities.SendMessage) PrivateMessage(net.dean.jraw.models.PrivateMessage) Message(net.dean.jraw.models.Message) DialogInterface(android.content.DialogInterface) SpannableStringBuilder(android.text.SpannableStringBuilder) RelativeSizeSpan(android.text.style.RelativeSizeSpan) TypedArray(android.content.res.TypedArray) PrivateMessage(net.dean.jraw.models.PrivateMessage) Inbox(me.ccrama.redditslide.Activities.Inbox) SendMessage(me.ccrama.redditslide.Activities.SendMessage) ClipboardManager(android.content.ClipboardManager) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Typeface(android.graphics.Typeface) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) FontPreferences(me.ccrama.redditslide.Visuals.FontPreferences) RoundedBackgroundSpan(me.ccrama.redditslide.Views.RoundedBackgroundSpan) StyleSpan(android.text.style.StyleSpan) ClipData(android.content.ClipData) SpannableStringBuilder(android.text.SpannableStringBuilder) LinearLayout(android.widget.LinearLayout) OpenRedditLink(me.ccrama.redditslide.OpenRedditLink)

Aggregations

ClipData (android.content.ClipData)1 ClipboardManager (android.content.ClipboardManager)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 TypedArray (android.content.res.TypedArray)1 Typeface (android.graphics.Typeface)1 Drawable (android.graphics.drawable.Drawable)1 RecyclerView (android.support.v7.widget.RecyclerView)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 RelativeSizeSpan (android.text.style.RelativeSizeSpan)1 StyleSpan (android.text.style.StyleSpan)1 View (android.view.View)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 Inbox (me.ccrama.redditslide.Activities.Inbox)1 SendMessage (me.ccrama.redditslide.Activities.SendMessage)1 OpenRedditLink (me.ccrama.redditslide.OpenRedditLink)1 RoundedBackgroundSpan (me.ccrama.redditslide.Views.RoundedBackgroundSpan)1 FontPreferences (me.ccrama.redditslide.Visuals.FontPreferences)1