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

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.apache.commons.lang.StringEscapeUtils;
import org.wordpress.android.R;
import org.wordpress.android.ui.ActivityLauncher;
import org.wordpress.android.ui.stats.models.InsightsLatestPostDetailsModel;
import org.wordpress.android.ui.stats.models.InsightsLatestPostModel;
import org.wordpress.android.ui.stats.models.PostModel;
import org.wordpress.android.ui.stats.service.StatsService;
import org.wordpress.android.util.FormatUtils;

public class StatsInsightsLatestPostSummaryFragment extends StatsAbstractInsightsFragment {
    public static final String TAG = StatsInsightsLatestPostSummaryFragment.class.getSimpleName();

    private static final String ARG_REST_RESPONSE_DETAILS = "ARG_REST_RESPONSE_DETAILS";

    private InsightsLatestPostModel mInsightsLatestPostModel;
    private InsightsLatestPostDetailsModel mInsightsLatestPostDetailsModel;

    @Override
    protected boolean hasDataAvailable() {
        return mInsightsLatestPostModel != null && mInsightsLatestPostDetailsModel != null;
    }
    @Override
    protected void saveStatsData(Bundle outState) {
        if (hasDataAvailable()) {
            outState.putSerializable(ARG_REST_RESPONSE, mInsightsLatestPostModel);
            outState.putSerializable(ARG_REST_RESPONSE_DETAILS, mInsightsLatestPostDetailsModel);
        }
    }
    @Override
    protected void restoreStatsData(Bundle savedInstanceState) {
        if (savedInstanceState.containsKey(ARG_REST_RESPONSE)) {
            mInsightsLatestPostModel = (InsightsLatestPostModel) savedInstanceState.getSerializable(ARG_REST_RESPONSE);
        }
        if (savedInstanceState.containsKey(ARG_REST_RESPONSE_DETAILS)) {
            mInsightsLatestPostDetailsModel = (InsightsLatestPostDetailsModel) savedInstanceState.getSerializable(ARG_REST_RESPONSE_DETAILS);
        }
    }

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

        if (event.mInsightsLatestPostModel == null) {
            showErrorUI();
            return;
        }

        mInsightsLatestPostModel = event.mInsightsLatestPostModel;

        // check if there is a post "published" on the blog
        View mainView = getView();
        if (mainView != null) {
            mainView.setVisibility(mInsightsLatestPostModel.isLatestPostAvailable() ? View.VISIBLE : View.GONE);
        }
        if (!mInsightsLatestPostModel.isLatestPostAvailable()) {
            // No need to go further into UI updating. There are no posts on this blog and the
            // entire fragment is hidden.
            return;
        }

        // Check if we already have the number of "views" for the latest post
        if (mInsightsLatestPostModel.getPostViewsCount() == Integer.MIN_VALUE) {
            // we don't have the views count. Need to call the service again here
            refreshStats(mInsightsLatestPostModel.getPostID(),
                    new StatsService.StatsEndpointsEnum[]{StatsService.StatsEndpointsEnum.INSIGHTS_LATEST_POST_VIEWS});
            showPlaceholderUI();
        } else {
            updateUI();
        }
    }

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

        if (mInsightsLatestPostModel == null || event.mInsightsLatestPostDetailsModel == null) {
            showErrorUI();
            return;
        }

        mInsightsLatestPostDetailsModel = event.mInsightsLatestPostDetailsModel;
        mInsightsLatestPostModel.setPostViewsCount(mInsightsLatestPostDetailsModel.getPostViewsCount());
        updateUI();
    }

    @SuppressWarnings("unused")
    public void onEventMainThread(StatsEvents.SectionUpdateError event) {

        if (!shouldUpdateFragmentOnErrorEvent(event)
                && event.mEndPointName != StatsService.StatsEndpointsEnum.INSIGHTS_LATEST_POST_VIEWS ) {
            return;
        }

        mInsightsLatestPostDetailsModel = null;
        mInsightsLatestPostModel =  null;
        showErrorUI(event.mError);
    }

    protected void updateUI() {
        super.updateUI();

        if (!isAdded() || !hasDataAvailable()) {
            return;
        }

        // check if there are posts "published" on the blog
        if (!mInsightsLatestPostModel.isLatestPostAvailable()) {
            // No need to go further into UI updating. There are no posts on this blog and the
            // entire fragment is hidden.
            return;
        }

        TextView moduleTitle = (TextView) getView().findViewById(R.id.stats_module_title);
        moduleTitle.setOnClickListener(ViewsTabOnClickListener);
        moduleTitle.setTextColor(getResources().getColor(R.color.stats_link_text_color));

        // update the tabs and the text now
        LinearLayout ll = (LinearLayout) getActivity().getLayoutInflater()
                .inflate(R.layout.stats_insights_latest_post_item, (ViewGroup) mResultContainer.getRootView(), false);

        String trendLabel = getString(R.string.stats_insights_latest_post_trend);
        String sinceLabel = StatsUtils.getSinceLabel(
                getActivity(),
                mInsightsLatestPostModel.getPostDate()
        ).toLowerCase();

        String postTitle = StringEscapeUtils.unescapeHtml(mInsightsLatestPostModel.getPostTitle());
        if (TextUtils.isEmpty(postTitle)) {
            postTitle = getString(R.string.stats_insights_latest_post_no_title);
        }

        final String trendLabelFormatted = String.format(
                trendLabel, sinceLabel, postTitle);

        int startIndex, endIndex;
        startIndex = trendLabelFormatted.indexOf(postTitle);
        endIndex = startIndex + postTitle.length() +1;

        Spannable descriptionTextToSpan = new SpannableString(trendLabelFormatted);
        descriptionTextToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.stats_link_text_color)),
                startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        TextView trendLabelTextField = (TextView) ll.findViewById(R.id.stats_post_trend_label);
        trendLabelTextField.setText(descriptionTextToSpan);
        trendLabelTextField.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StatsUtils.openPostInReaderOrInAppWebview(getActivity(),
                        mInsightsLatestPostModel.getBlogID(),
                        String.valueOf(mInsightsLatestPostModel.getPostID()),
                        StatsConstants.ITEM_TYPE_POST,
                        mInsightsLatestPostModel.getPostURL());
            }
        });

        LinearLayout tabs = (LinearLayout) ll.findViewById(R.id.stats_latest_post_tabs);

        for (int i = 0; i < tabs.getChildCount(); i++) {
            LinearLayout currentTab = (LinearLayout) tabs.getChildAt(i);
            switch (i) {
                case 0:
                    setupTab(currentTab, FormatUtils.formatDecimal(mInsightsLatestPostModel.getPostViewsCount()), StatsVisitorsAndViewsFragment.OverviewLabel.VIEWS);
                    break;
                case 1:
                    setupTab(currentTab, FormatUtils.formatDecimal(mInsightsLatestPostModel.getPostLikeCount()), StatsVisitorsAndViewsFragment.OverviewLabel.LIKES);
                    break;
                case 2:
                    setupTab(currentTab, FormatUtils.formatDecimal(mInsightsLatestPostModel.getPostCommentCount()), StatsVisitorsAndViewsFragment.OverviewLabel.COMMENTS);
                    break;
            }
        }

        mResultContainer.addView(ll);
    }

    private void setupTab(LinearLayout currentTab, String total, final StatsVisitorsAndViewsFragment.OverviewLabel itemType) {
        final TextView label;
        final TextView value;
        final ImageView icon;

        currentTab.setTag(itemType);
        // Only Views is clickable here
        if (itemType == StatsVisitorsAndViewsFragment.OverviewLabel.VIEWS) {
            currentTab.setOnClickListener(ViewsTabOnClickListener);
        } else {
            currentTab.setClickable(false);
        }

        label = (TextView) currentTab.findViewById(R.id.stats_visitors_and_views_tab_label);
        label.setText(itemType.getLabel());
        value = (TextView) currentTab.findViewById(R.id.stats_visitors_and_views_tab_value);
        value.setText(total);
        if (total.equals("0")) {
            value.setTextColor(getResources().getColor(R.color.grey));
        } else {
            // Only Views is clickable here.
            // Likes and Comments shouldn't link anywhere because they don't have summaries
            // so their color should be Gray Darken 30 or #3d596d
            if (itemType == StatsVisitorsAndViewsFragment.OverviewLabel.VIEWS) {
                value.setTextColor(getResources().getColor(R.color.blue_wordpress));
            } else {
                value.setTextColor(getResources().getColor(R.color.grey_darken_30));
            }
        }
        icon = (ImageView) currentTab.findViewById(R.id.stats_visitors_and_views_tab_icon);
        icon.setImageDrawable(getTabIcon(itemType));

        if (itemType == StatsVisitorsAndViewsFragment.OverviewLabel.COMMENTS) {
            currentTab.setBackgroundResource(R.drawable.stats_visitors_and_views_button_latest_white);
        } else {
            currentTab.setBackgroundResource(R.drawable.stats_visitors_and_views_button_white);
        }
    }

    private final View.OnClickListener ViewsTabOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!isAdded()) {
                return;
            }

            // Another check that the data is available
            if (mInsightsLatestPostModel == null) {
                showErrorUI();
                return;
            }

            PostModel postModel = new PostModel(
                    mInsightsLatestPostModel.getBlogID(),
                    String.valueOf(mInsightsLatestPostModel.getPostID()),
                    mInsightsLatestPostModel.getPostTitle(),
                    mInsightsLatestPostModel.getPostURL(),
                    StatsConstants.ITEM_TYPE_POST);
            ActivityLauncher.viewStatsSinglePostDetails(getActivity(), postModel);
        }
    };

    private Drawable getTabIcon(final StatsVisitorsAndViewsFragment.OverviewLabel labelItem) {
        switch (labelItem) {
            case VISITORS:
                return getResources().getDrawable(R.drawable.stats_icon_visitors);
            case COMMENTS:
                return getResources().getDrawable(R.drawable.stats_icon_comments);
            case LIKES:
                return getResources().getDrawable(R.drawable.stats_icon_likes);
            default:
                // Views and when no prev match
                return getResources().getDrawable(R.drawable.stats_icon_views);
        }
    }

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

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