aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsReferrersFragment.java
blob: c94293c7b3c7c90a889d53e9ab20efb5f157188f (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
package org.wordpress.android.ui.stats;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import org.wordpress.android.R;
import org.wordpress.android.ui.stats.models.ReferrerGroupModel;
import org.wordpress.android.ui.stats.models.ReferrerResultModel;
import org.wordpress.android.ui.stats.models.ReferrersModel;
import org.wordpress.android.ui.stats.models.SingleItemModel;
import org.wordpress.android.ui.stats.service.StatsService;
import org.wordpress.android.util.FormatUtils;
import org.wordpress.android.util.GravatarUtils;
import org.wordpress.android.widgets.WPNetworkImageView;

import java.util.ArrayList;
import java.util.List;

public class StatsReferrersFragment extends StatsAbstractListFragment {
    public static final String TAG = StatsReferrersFragment.class.getSimpleName();

    private ReferrersModel mReferrers;

    @Override
    protected boolean hasDataAvailable() {
        return mReferrers != null;
    }
    @Override
    protected void saveStatsData(Bundle outState) {
        if (hasDataAvailable()) {
            outState.putSerializable(ARG_REST_RESPONSE, mReferrers);
        }
    }
    @Override
    protected void restoreStatsData(Bundle savedInstanceState) {
        if (savedInstanceState.containsKey(ARG_REST_RESPONSE)) {
            mReferrers = (ReferrersModel) savedInstanceState.getSerializable(ARG_REST_RESPONSE);
        }
    }

    @SuppressWarnings("unused")
    public void onEventMainThread(StatsEvents.ReferrersUpdated event) {
        if (!shouldUpdateFragmentOnUpdateEvent(event)) {
            return;
        }

        mGroupIdToExpandedMap.clear();
        mReferrers = event.mReferrers;

        updateUI();
    }

    @SuppressWarnings("unused")
    public void onEventMainThread(StatsEvents.SectionUpdateError event) {
        if (!shouldUpdateFragmentOnErrorEvent(event)) {
            return;
        }

        mReferrers = null;
        mGroupIdToExpandedMap.clear();
        showErrorUI(event.mError);
    }

    @Override
    protected void updateUI() {
        if (!isAdded()) {
            return;
        }

        if (hasReferrers()) {
            BaseExpandableListAdapter adapter = new MyExpandableListAdapter(getActivity(), getReferrersGroups());
            StatsUIHelper.reloadGroupViews(getActivity(), adapter, mGroupIdToExpandedMap, mList, getMaxNumberOfItemsToShowInList());
            showHideNoResultsUI(false);
        } else {
            showHideNoResultsUI(true);
        }
    }

    private boolean hasReferrers() {
        return mReferrers != null
                && mReferrers.getGroups() != null
                && mReferrers.getGroups().size() > 0;
    }

    private List<ReferrerGroupModel> getReferrersGroups() {
        if (!hasReferrers()) {
            return new ArrayList<ReferrerGroupModel>(0);
        }
        return mReferrers.getGroups();
    }

    @Override
    protected boolean isViewAllOptionAvailable() {
        return hasReferrers() && getReferrersGroups().size() > MAX_NUM_OF_ITEMS_DISPLAYED_IN_LIST;
    }

    @Override
    protected boolean isExpandableList() {
        return true;
    }

    @Override
    protected StatsService.StatsEndpointsEnum[] sectionsToUpdate() {
        return new StatsService.StatsEndpointsEnum[]{
                StatsService.StatsEndpointsEnum.REFERRERS
        };
    }

    @Override
    protected int getEntryLabelResId() {
        return R.string.stats_entry_referrers;
    }
    @Override
    protected int getTotalsLabelResId() {
        return R.string.stats_totals_views;
    }
    @Override
    protected int getEmptyLabelTitleResId() {
        return R.string.stats_empty_referrers_title;
    }
    @Override
    protected int getEmptyLabelDescResId() {
        return R.string.stats_empty_referrers_desc;
    }

    private class MyExpandableListAdapter extends BaseExpandableListAdapter {
        public final LayoutInflater inflater;
        public final Activity act;
        private final List<ReferrerGroupModel> groups;
        private final List<List<MyChildModel>> children;

        public MyExpandableListAdapter(Activity act, List<ReferrerGroupModel> groups) {
            this.groups = groups;
            this.inflater = LayoutInflater.from(act);
            this.act = act;

            // The code below flattens the 3-levels tree of children to a 2-levels structure
            // that will be used later to populate the UI
            this.children = new ArrayList<>(groups.size());
            // pre-populate the structure with null values
            for (int i = 0; i < groups.size(); i++) {
                this.children.add(null);
            }

            for (int i = 0; i < groups.size(); i++) {
                ReferrerGroupModel currentGroup = groups.get(i);
                List<MyChildModel> currentGroupChildren = new ArrayList<>();
                List<ReferrerResultModel> childrenOfLevelOne = currentGroup.getResults();
                if (childrenOfLevelOne != null) {
                    // Children at first level could be a single item or another tree
                    // Levels 2 children are skipped in the UI.
                    for (ReferrerResultModel singleLevelOneChild : childrenOfLevelOne) {
                        // Use all the info given in the first level child.
                        MyChildModel myChild = new MyChildModel();
                        myChild.icon = singleLevelOneChild.getIcon();
                        myChild.url = singleLevelOneChild.getUrl();
                        myChild.name = singleLevelOneChild.getName();
                        myChild.views = singleLevelOneChild.getViews();

                        // read the URL from the first second-level child if available.
                        List<SingleItemModel> secondLevelChildren = singleLevelOneChild.getChildren();
                        if (secondLevelChildren != null && secondLevelChildren.size() > 0) {
                            SingleItemModel firstThirdLevelChild = secondLevelChildren.get(0);
                            myChild.url = firstThirdLevelChild.getUrl();
                        }
                        currentGroupChildren.add(myChild);
                    }
                }
                this.children.set(i, currentGroupChildren);
            }
        }

        private final class MyChildModel {
            String name;
            int views;
            String url;
            String icon;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            List<MyChildModel> currentGroupChildren = children.get(groupPosition);
            return currentGroupChildren.get(childPosition);
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public View getChildView(int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {

            final MyChildModel currentChild = (MyChildModel) getChild(groupPosition, childPosition);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.stats_list_cell, parent, false);
                // configure view holder
                StatsViewHolder viewHolder = new StatsViewHolder(convertView);
                convertView.setTag(viewHolder);
            }

            final StatsViewHolder holder = (StatsViewHolder) convertView.getTag();

            String name = currentChild.name;
            int views = currentChild.views;

            holder.chevronImageView.setVisibility(View.GONE);
            holder.linkImageView.setVisibility(TextUtils.isEmpty(currentChild.url) ? View.GONE : View.VISIBLE);
            holder.setEntryTextOrLink(currentChild.url, name);

            // totals
            holder.totalsTextView.setText(FormatUtils.formatDecimal(views));

            // site icon
            holder.networkImageView.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(currentChild.icon)) {
                holder.networkImageView.setImageUrl(
                        GravatarUtils.fixGravatarUrl(currentChild.icon, mResourceVars.headerAvatarSizePx),
                        WPNetworkImageView.ImageType.GONE_UNTIL_AVAILABLE);
            }

            // no more btm
            holder.imgMore.setVisibility(View.GONE);

            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            List<MyChildModel> currentGroupChildren = children.get(groupPosition);
            if (currentGroupChildren == null) {
                return 0;
            } else {
                return currentGroupChildren.size();
            }
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groups.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return groups.size();
        }


        @Override
        public long getGroupId(int groupPosition) {
            return 0;
        }

        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {

            final StatsViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.stats_list_cell, parent, false);
                holder = new StatsViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (StatsViewHolder) convertView.getTag();
            }

            final ReferrerGroupModel group = (ReferrerGroupModel) getGroup(groupPosition);

            String name = group.getName();
            int total = group.getTotal();
            String url = group.getUrl();
            String icon = group.getIcon();
            int children = getChildrenCount(groupPosition);

            if (children > 0) {
                holder.setEntryText(name, getResources().getColor(R.color.stats_link_text_color));
            } else {
                holder.setEntryTextOrLink(url, name);
            }

            // totals
            holder.totalsTextView.setText(FormatUtils.formatDecimal(total));

            // Site icon
            holder.networkImageView.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(icon)) {
                holder.networkImageView.setImageUrl(
                        GravatarUtils.fixGravatarUrl(icon, mResourceVars.headerAvatarSizePx),
                        WPNetworkImageView.ImageType.GONE_UNTIL_AVAILABLE);
            }

            if (children == 0) {
                holder.showLinkIcon();
            } else {
                holder.showChevronIcon();
            }

            // Setup the spam button
            if (ReferrerSpamHelper.isSpamActionAvailable(group)) {
                holder.imgMore.setVisibility(View.VISIBLE);
                holder.imgMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ReferrerSpamHelper rp = new ReferrerSpamHelper(act);
                        rp.showPopup(holder.imgMore, group);
                    }
                });

            } else {
                holder.imgMore.setVisibility(View.GONE);
                holder.imgMore.setClickable(false);
            }

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }

    }

    @Override
    public String getTitle() {
        return getString(R.string.stats_view_referrers);
    }
}