aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/notifications/blocks/CommentUserNoteBlock.java
blob: 8f851348288fb5ed22b3eae9a909b7548f13b5ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package org.wordpress.android.ui.notifications.blocks;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.models.CommentStatus;
import org.wordpress.android.ui.notifications.utils.NotificationsUtils;
import org.wordpress.android.util.DateTimeUtils;
import org.wordpress.android.util.GravatarUtils;
import org.wordpress.android.widgets.WPNetworkImageView;

// A user block with slightly different formatting for display in a comment detail
public class CommentUserNoteBlock extends UserNoteBlock {

    private CommentStatus mCommentStatus = CommentStatus.UNKNOWN;
    private int mNormalBackgroundColor;
    private int mNormalTextColor;
    private int mAgoTextColor;
    private int mUnapprovedTextColor;
    private int mIndentedLeftPadding;

    private boolean mStatusChanged;

    public interface OnCommentStatusChangeListener {
        public void onCommentStatusChanged(CommentStatus newStatus);
    }

    public CommentUserNoteBlock(Context context, JSONObject noteObject,
                                OnNoteBlockTextClickListener onNoteBlockTextClickListener,
                                OnGravatarClickedListener onGravatarClickedListener) {
        super(context, noteObject, onNoteBlockTextClickListener, onGravatarClickedListener);

        if (context != null) {
            setAvatarSize(context.getResources().getDimensionPixelSize(R.dimen.avatar_sz_small));
        }
    }

    @Override
    public BlockType getBlockType() {
        return BlockType.USER_COMMENT;
    }

    @Override
    public int getLayoutResourceId() {
        return R.layout.note_block_comment_user;
    }

    @Override
    public View configureView(View view) {
        final CommentUserNoteBlockHolder noteBlockHolder = (CommentUserNoteBlockHolder)view.getTag();

        noteBlockHolder.nameTextView.setText(Html.fromHtml("<strong>" + getNoteText().toString() + "</strong>"));
        noteBlockHolder.agoTextView.setText(DateTimeUtils.timeSpanFromTimestamp(getTimestamp(),
                WordPress.getContext()));
        if (!TextUtils.isEmpty(getMetaHomeTitle()) || !TextUtils.isEmpty(getMetaSiteUrl())) {
            noteBlockHolder.bulletTextView.setVisibility(View.VISIBLE);
            noteBlockHolder.siteTextView.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(getMetaHomeTitle())) {
                noteBlockHolder.siteTextView.setText(getMetaHomeTitle());
            } else {
                noteBlockHolder.siteTextView.setText(getMetaSiteUrl().replace("http://", "").replace("https://", ""));
            }
        } else {
            noteBlockHolder.bulletTextView.setVisibility(View.GONE);
            noteBlockHolder.siteTextView.setVisibility(View.GONE);
        }

        if (hasImageMediaItem()) {
            String imageUrl = GravatarUtils.fixGravatarUrl(getNoteMediaItem().optString("url", ""), getAvatarSize());
            noteBlockHolder.avatarImageView.setImageUrl(imageUrl, WPNetworkImageView.ImageType.AVATAR);
            if (!TextUtils.isEmpty(getUserUrl())) {
                noteBlockHolder.avatarImageView.setOnTouchListener(mOnGravatarTouchListener);
            } else {
                noteBlockHolder.avatarImageView.setOnTouchListener(null);
            }
        } else {
            noteBlockHolder.avatarImageView.showDefaultGravatarImage();
            noteBlockHolder.avatarImageView.setOnTouchListener(null);
        }

        noteBlockHolder.commentTextView.setText(
                NotificationsUtils.getSpannableContentForRanges(
                        getNoteData().optJSONObject("comment_text"),
                        noteBlockHolder.commentTextView,
                        getOnNoteBlockTextClickListener(),
                        false)
        );

        // Change display based on comment status and type:
        // 1. Comment replies are indented and have a 'pipe' background
        // 2. Unapproved comments have different background and text color
        int paddingLeft = view.getPaddingLeft();
        int paddingTop = view.getPaddingTop();
        int paddingRight = view.getPaddingRight();
        int paddingBottom = view.getPaddingBottom();
        if (mCommentStatus == CommentStatus.UNAPPROVED) {
            if (hasCommentNestingLevel()) {
                paddingLeft = mIndentedLeftPadding;
                view.setBackgroundResource(R.drawable.comment_reply_unapproved_background);
            } else {
                view.setBackgroundResource(R.drawable.comment_unapproved_background);
            }

            noteBlockHolder.dividerView.setVisibility(View.INVISIBLE);

            noteBlockHolder.agoTextView.setTextColor(mUnapprovedTextColor);
            noteBlockHolder.bulletTextView.setTextColor(mUnapprovedTextColor);
            noteBlockHolder.siteTextView.setTextColor(mUnapprovedTextColor);
            noteBlockHolder.nameTextView.setTextColor(mUnapprovedTextColor);
            noteBlockHolder.commentTextView.setTextColor(mUnapprovedTextColor);
        } else {
            if (hasCommentNestingLevel()) {
                paddingLeft = mIndentedLeftPadding;
                view.setBackgroundResource(R.drawable.comment_reply_background);
                noteBlockHolder.dividerView.setVisibility(View.INVISIBLE);
            } else {
                view.setBackgroundColor(mNormalBackgroundColor);
                noteBlockHolder.dividerView.setVisibility(View.VISIBLE);
            }

            noteBlockHolder.agoTextView.setTextColor(mAgoTextColor);
            noteBlockHolder.bulletTextView.setTextColor(mAgoTextColor);
            noteBlockHolder.siteTextView.setTextColor(mAgoTextColor);
            noteBlockHolder.nameTextView.setTextColor(mNormalTextColor);
            noteBlockHolder.commentTextView.setTextColor(mNormalTextColor);
        }

        view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);

        // If status was changed, fade in the view
        if (mStatusChanged) {
            mStatusChanged = false;
            view.setAlpha(0.4f);
            view.animate().alpha(1.0f).start();
        }

        return view;
    }

    private long getTimestamp() {
        return getNoteData().optInt("timestamp", 0);
    }

    private boolean hasCommentNestingLevel() {
        try {
            JSONObject commentTextObject = getNoteData().getJSONObject("comment_text");
            return commentTextObject.optInt("nest_level", 0) > 0;
        } catch (JSONException e) {
            return false;
        }
    }

    @Override
    public Object getViewHolder(View view) {
        return new CommentUserNoteBlockHolder(view);
    }

    private class CommentUserNoteBlockHolder {
        private final WPNetworkImageView avatarImageView;
        private final TextView nameTextView;
        private final TextView agoTextView;
        private final TextView bulletTextView;
        private final TextView siteTextView;
        private final TextView commentTextView;
        private final View dividerView;

        public CommentUserNoteBlockHolder(View view) {
            nameTextView = (TextView)view.findViewById(R.id.user_name);
            agoTextView = (TextView)view.findViewById(R.id.user_comment_ago);
            agoTextView.setVisibility(View.VISIBLE);
            bulletTextView = (TextView)view.findViewById(R.id.user_comment_bullet);
            siteTextView = (TextView)view.findViewById(R.id.user_comment_site);
            commentTextView = (TextView)view.findViewById(R.id.user_comment);
            commentTextView.setMovementMethod(new NoteBlockLinkMovementMethod());
            avatarImageView = (WPNetworkImageView)view.findViewById(R.id.user_avatar);
            dividerView = view.findViewById(R.id.divider_view);

            siteTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getOnNoteBlockTextClickListener() != null) {
                        getOnNoteBlockTextClickListener().showSitePreview(getMetaSiteId(), getMetaSiteUrl());
                    }
                }
            });

            // show all comments on this post when user clicks the comment text
            commentTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getOnNoteBlockTextClickListener() != null) {
                        getOnNoteBlockTextClickListener().showReaderPostComments();
                    }
                }
            });
        }
    }

    public void configureResources(Context context) {
        if (context == null) return;

        mNormalTextColor = context.getResources().getColor(R.color.grey_dark);
        mNormalBackgroundColor = context.getResources().getColor(R.color.white);
        mAgoTextColor = context.getResources().getColor(R.color.grey);
        mUnapprovedTextColor = context.getResources().getColor(R.color.notification_status_unapproved_dark);
        // Double margin_extra_large for increased indent in comment replies
        mIndentedLeftPadding = context.getResources().getDimensionPixelSize(R.dimen.margin_extra_large) * 2;
    }

    private final OnCommentStatusChangeListener mOnCommentChangedListener = new OnCommentStatusChangeListener() {
        @Override
        public void onCommentStatusChanged(CommentStatus newStatus) {
            mCommentStatus = newStatus;
            mStatusChanged = true;
        }
    };

    public void setCommentStatus(CommentStatus status) {
        mCommentStatus = status;
    }

    public OnCommentStatusChangeListener getOnCommentChangeListener() {
        return mOnCommentChangedListener;
    }
}