aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentAdapter.java
blob: d5e5bd6fb9e198c366c0267cc77f072937f6b70d (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package org.wordpress.android.ui.comments;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.wordpress.android.R;
import org.wordpress.android.datasets.CommentTable;
import org.wordpress.android.models.Comment;
import org.wordpress.android.models.CommentList;
import org.wordpress.android.models.CommentStatus;
import org.wordpress.android.util.AniUtils;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.DateTimeUtils;
import org.wordpress.android.util.StringUtils;
import org.wordpress.android.util.WPHtml;
import org.wordpress.android.widgets.WPNetworkImageView;

import java.util.HashSet;

class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    interface OnDataLoadedListener {
        void onDataLoaded(boolean isEmpty);
    }

    interface OnLoadMoreListener {
        void onLoadMore();
    }

    interface OnSelectedItemsChangeListener {
        void onSelectedItemsChanged();
    }

    interface OnCommentPressedListener {
        void onCommentPressed(int position, View view);

        void onCommentLongPressed(int position, View view);
    }

    private final LayoutInflater mInflater;
    private final Context mContext;

    private final CommentList mComments = new CommentList();
    private final HashSet<Long> mSelectedCommentsId = new HashSet<>();
    private final HashSet<Long> mModeratingCommentsIds = new HashSet<>();

    private final int mStatusColorSpam;
    private final int mStatusColorUnapproved;

    private final int mLocalBlogId;
    private final int mAvatarSz;
    private final String mStatusTextSpam;
    private final String mStatusTextUnapproved;
    private final int mSelectedColor;
    private final int mUnselectedColor;

    private OnDataLoadedListener mOnDataLoadedListener;
    private OnCommentPressedListener mOnCommentPressedListener;
    private OnLoadMoreListener mOnLoadMoreListener;
    private OnSelectedItemsChangeListener mOnSelectedChangeListener;

    private boolean mEnableSelection;

    class CommentHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener {
        private final TextView txtTitle;
        private final TextView txtComment;
        private final TextView txtStatus;
        private final TextView txtDate;
        private final WPNetworkImageView imgAvatar;
        private final ImageView imgCheckmark;
        private final View progressBar;
        private final ViewGroup containerView;

        public CommentHolder(View view) {
            super(view);
            txtTitle = (TextView) view.findViewById(R.id.title);
            txtComment = (TextView) view.findViewById(R.id.comment);
            txtStatus = (TextView) view.findViewById(R.id.status);
            txtDate = (TextView) view.findViewById(R.id.text_date);
            imgCheckmark = (ImageView) view.findViewById(R.id.image_checkmark);
            imgAvatar = (WPNetworkImageView) view.findViewById(R.id.avatar);
            progressBar = view.findViewById(R.id.moderate_progress);
            containerView = (ViewGroup) view.findViewById(R.id.layout_container);

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mOnCommentPressedListener != null) {
                mOnCommentPressedListener.onCommentPressed(getAdapterPosition(), v);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (mOnCommentPressedListener != null) {
                mOnCommentPressedListener.onCommentLongPressed(getAdapterPosition(), v);
            }
            return true;
        }
    }

    CommentAdapter(Context context, int localBlogId) {
        mInflater = LayoutInflater.from(context);
        mContext = context;

        mLocalBlogId = localBlogId;

        mStatusColorSpam = ContextCompat.getColor(context, R.color.comment_status_spam);
        mStatusColorUnapproved = ContextCompat.getColor(context, R.color.comment_status_unapproved);

        mUnselectedColor = ContextCompat.getColor(context, R.color.white);
        mSelectedColor = ContextCompat.getColor(context, R.color.translucent_grey_lighten_20);

        mStatusTextSpam = context.getResources().getString(R.string.comment_status_spam);
        mStatusTextUnapproved = context.getResources().getString(R.string.comment_status_unapproved);

        mAvatarSz = context.getResources().getDimensionPixelSize(R.dimen.avatar_sz_medium);

        setHasStableIds(true);
    }

    void setOnDataLoadedListener(OnDataLoadedListener listener) {
        mOnDataLoadedListener = listener;
    }

    void setOnLoadMoreListener(OnLoadMoreListener listener) {
        mOnLoadMoreListener = listener;
    }

    void setOnCommentPressedListener(OnCommentPressedListener listener) {
        mOnCommentPressedListener = listener;
    }

    void setOnSelectedItemsChangeListener(OnSelectedItemsChangeListener listener) {
        mOnSelectedChangeListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.comment_listitem, null);
        CommentHolder holder = new CommentHolder(view);
        view.setTag(holder);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        Comment comment = mComments.get(position);
        CommentHolder holder = (CommentHolder) viewHolder;

        if (isModeratingCommentId(comment.commentID)) {
            holder.progressBar.setVisibility(View.VISIBLE);
        } else {
            holder.progressBar.setVisibility(View.GONE);
        }

        holder.txtTitle.setText(Html.fromHtml(comment.getFormattedTitle()));
        holder.txtComment.setText(comment.getUnescapedCommentTextWithDrawables());
        holder.txtDate.setText(DateTimeUtils.javaDateToTimeSpan(comment.getDatePublished(), mContext));

        // status is only shown for comments that haven't been approved
        final boolean showStatus;
        switch (comment.getStatusEnum()) {
            case SPAM:
                showStatus = true;
                holder.txtStatus.setText(mStatusTextSpam);
                holder.txtStatus.setTextColor(mStatusColorSpam);
                break;
            case UNAPPROVED:
                showStatus = true;
                holder.txtStatus.setText(mStatusTextUnapproved);
                holder.txtStatus.setTextColor(mStatusColorUnapproved);
                break;
            default:
                showStatus = false;
                break;
        }
        holder.txtStatus.setVisibility(showStatus ? View.VISIBLE : View.GONE);

        int checkmarkVisibility;
        if (mEnableSelection && isItemSelected(position)) {
            checkmarkVisibility = View.VISIBLE;
            holder.containerView.setBackgroundColor(mSelectedColor);
        } else {
            checkmarkVisibility = View.GONE;
            holder.imgAvatar.setImageUrl(comment.getAvatarForDisplay(mAvatarSz), WPNetworkImageView.ImageType.AVATAR);
            holder.containerView.setBackgroundColor(mUnselectedColor);
        }

        if (holder.imgCheckmark.getVisibility() != checkmarkVisibility) {
            holder.imgCheckmark.setVisibility(checkmarkVisibility);
        }

        // comment text needs to be to the left of date/status when the title is a single line and
        // the status is displayed or else the status may overlap the comment text - note that
        // getLineCount() will return 0 if the view hasn't been rendered yet, which is why we
        // check getLineCount() <= 1
        boolean adjustComment = (showStatus && holder.txtTitle.getLineCount() <= 1);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.txtComment.getLayoutParams();
        if (adjustComment) {
            params.addRule(RelativeLayout.LEFT_OF, R.id.layout_date_status);
        } else {
            params.addRule(RelativeLayout.LEFT_OF, 0);
        }

        // request to load more comments when we near the end
        if (mOnLoadMoreListener != null && position >= getItemCount() - 1
                && position >= CommentsListFragment.COMMENTS_PER_PAGE - 1) {
            mOnLoadMoreListener.onLoadMore();
        }
    }

    public Comment getItem(int position) {
        if (isPositionValid(position)) {
            return mComments.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return mComments.get(position).commentID;
    }

    @Override
    public int getItemCount() {
        return mComments.size();
    }

    private boolean isEmpty() {
        return getItemCount() == 0;
    }

    void setEnableSelection(boolean enable) {
        if (enable == mEnableSelection) return;

        mEnableSelection = enable;
        if (mEnableSelection) {
            notifyDataSetChanged();
        } else {
            clearSelectedComments();
        }
    }

    void clearSelectedComments() {
        if (mSelectedCommentsId.size() > 0) {
            mSelectedCommentsId.clear();
            notifyDataSetChanged();
            if (mOnSelectedChangeListener != null) {
                mOnSelectedChangeListener.onSelectedItemsChanged();
            }
        }
    }

    int getSelectedCommentCount() {
        return mSelectedCommentsId.size();
    }

    CommentList getSelectedComments() {
        CommentList comments = new CommentList();
        if (!mEnableSelection) {
            return comments;
        }

        for (Long commentId : mSelectedCommentsId) {
            int commentIndex = indexOfCommentId(commentId);
            if (commentIndex > -1) {
                comments.add(mComments.get(commentIndex));
            }
        }

        return comments;
    }

    private boolean isItemSelected(int position) {
        Comment comment = getItem(position);
        return comment != null && mSelectedCommentsId.contains(comment.commentID);
    }

    void setItemSelected(int position, boolean isSelected, View view) {
        if (isItemSelected(position) == isSelected) return;

        Comment comment = getItem(position);
        if (comment == null) return;

        if (isSelected) {
            mSelectedCommentsId.add(comment.commentID);
        } else {
            mSelectedCommentsId.remove(comment.commentID);
        }


        notifyItemChanged(position);

        if (view != null && view.getTag() instanceof CommentHolder) {
            CommentHolder holder = (CommentHolder) view.getTag();
            // animate the selection change
            AniUtils.startAnimation(holder.imgCheckmark, isSelected ? R.anim.cab_select : R.anim.cab_deselect);
            holder.imgCheckmark.setVisibility(isSelected ? View.VISIBLE : View.GONE);
        }

        if (mOnSelectedChangeListener != null) {
            mOnSelectedChangeListener.onSelectedItemsChanged();
        }
    }

    void toggleItemSelected(int position, View view) {
        setItemSelected(position, !isItemSelected(position), view);
    }

    public void addModeratingCommentId(long commentId) {
        mModeratingCommentsIds.add(commentId);
        int position = indexOfCommentId(commentId);
        if (position >= 0) {
            notifyItemChanged(position);
        }
    }

    public void removeModeratingCommentId(long commentId) {
        mModeratingCommentsIds.remove(commentId);
        int position = indexOfCommentId(commentId);
        if (position >= 0) {
            notifyItemChanged(position);
        }
    }

    public boolean isModeratingCommentId(long commentId) {
        return mModeratingCommentsIds.size() > 0
                && mModeratingCommentsIds.contains(commentId);
    }

    private int indexOfCommentId(long commentId) {
        return mComments.indexOfCommentId(commentId);
    }

    private boolean isPositionValid(int position) {
        return (position >= 0 && position < mComments.size());
    }

    void replaceComments(CommentList comments) {
        mComments.replaceComments(comments);
        notifyDataSetChanged();
    }

    void deleteComments(CommentList comments) {
        mComments.deleteComments(comments);
        notifyDataSetChanged();
        if (mOnDataLoadedListener != null) {
            mOnDataLoadedListener.onDataLoaded(isEmpty());
        }
    }

    public void removeComment(Comment comment) {
        int position = indexOfCommentId(comment.commentID);
        if (position >= 0) {
            mComments.remove(position);
            notifyItemRemoved(position);
        }
    }

    /*
     * clear all comments
     */
    void clearComments() {
        if (mComments != null) {
            mComments.clear();
            notifyDataSetChanged();
        }
    }

    /*
     * load comments using an AsyncTask
     */
    void loadComments(CommentStatus statusFilter) {
        if (mIsLoadTaskRunning) {
            AppLog.w(AppLog.T.COMMENTS, "load comments task already active");
        } else {
            new LoadCommentsTask(statusFilter).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
    }

    /*
     * AsyncTask to load comments from SQLite
     */
    private boolean mIsLoadTaskRunning = false;

    private class LoadCommentsTask extends AsyncTask<Void, Void, Boolean> {
        CommentList tmpComments;
        final CommentStatus mStatusFilter;

        public LoadCommentsTask(CommentStatus statusFilter) {
            mStatusFilter = statusFilter;
        }

        @Override
        protected void onPreExecute() {
            mIsLoadTaskRunning = true;
        }

        @Override
        protected void onCancelled() {
            mIsLoadTaskRunning = false;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            if (mStatusFilter == null) {
                tmpComments = CommentTable.getCommentsForBlogWithFilter(mLocalBlogId, CommentStatus.UNKNOWN);
            } else {
                tmpComments = CommentTable.getCommentsForBlogWithFilter(mLocalBlogId, mStatusFilter);
            }

            if (mComments.isSameList(tmpComments)) {
                return false;
            }

            // pre-calc transient values so they're cached prior to display
            for (Comment comment : tmpComments) {
                comment.getDatePublished();
                comment.getUnescapedPostTitle();
                comment.getAvatarForDisplay(mAvatarSz);
                comment.getFormattedTitle();

                String content = StringUtils.notNullStr(comment.getCommentText());
                //to load images embedded within comments, pass an ImageGetter to WPHtml.fromHtml()
                Spanned spanned = WPHtml.fromHtml(content, null, null, mContext, null, 0);
                comment.setUnescapedCommentWithDrawables(spanned);
            }

            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {
                mComments.clear();
                mComments.addAll(tmpComments);
                notifyDataSetChanged();
            }

            if (mOnDataLoadedListener != null) {
                mOnDataLoadedListener.onDataLoaded(isEmpty());
            }

            mIsLoadTaskRunning = false;
        }
    }

    public HashSet<Long> getSelectedCommentsId() {
        return mSelectedCommentsId;
    }


    public CommentAdapterState getAdapterState() {
        return new CommentAdapterState(mSelectedCommentsId, mModeratingCommentsIds);
    }

    public void setInitialState(CommentAdapterState adapterState) {
        if (adapterState == null) return;

        if (adapterState.hasSelectedComments()) {
            mSelectedCommentsId.clear();
            mSelectedCommentsId.addAll(adapterState.getSelectedComments());
            setEnableSelection(true);
        }

        if (adapterState.hasModeratingComments()) {
            mModeratingCommentsIds.clear();
            mModeratingCommentsIds.addAll(adapterState.getModeratedCommentsId());
        }
    }
}