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

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.wordpress.android.R;
import org.wordpress.android.ui.stats.models.InsightsPopularModel;
import org.wordpress.android.ui.stats.service.StatsService;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;


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

    private InsightsPopularModel mInsightsPopularModel;

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

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

        mInsightsPopularModel = event.mInsightsPopularModel;
        updateUI();
    }

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

        mInsightsPopularModel = null;
        showErrorUI(event.mError);
    }

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

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

        LinearLayout ll = (LinearLayout) getActivity().getLayoutInflater()
                .inflate(R.layout.stats_insights_most_popular_item, (ViewGroup) mResultContainer.getRootView(), false);

        int dayOfTheWeek = mInsightsPopularModel.getHighestDayOfWeek();

        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTimeInMillis(System.currentTimeMillis());
        switch (dayOfTheWeek) {
            case 0:
                c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                break;
            case 1:
                c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
                break;
            case 2:
                c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
                break;
            case 3:
                c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
                break;
            case 4:
                c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
                break;
            case 5:
                c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
                break;
            case 6:
                c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                break;
        }

        DateFormat formatter = new SimpleDateFormat("EEEE");
        final TextView mostPopularDayTextView = (TextView) ll.findViewById(R.id.stats_most_popular_day);
        mostPopularDayTextView.setText(formatter.format(c.getTime()));
        final TextView mostPopularDayPercentTextView = (TextView) ll.findViewById(R.id.stats_most_popular_day_percent);
        mostPopularDayPercentTextView.setText(
                String.format(
                        getString(R.string.stats_insights_most_popular_percent_views),
                        roundToInteger(mInsightsPopularModel.getHighestDayPercent())
                )
        );

        TextView mostPopularHourTextView = (TextView) ll.findViewById(R.id.stats_most_popular_hour);
        DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(getActivity());
        c.set(Calendar.HOUR_OF_DAY, mInsightsPopularModel.getHighestHour());
        c.set(Calendar.MINUTE, 0);
        mostPopularHourTextView.setText(timeFormat.format(c.getTime()));
        final TextView mostPopularHourPercentTextView = (TextView) ll.findViewById(R.id.stats_most_popular_hour_percent);
        mostPopularHourPercentTextView.setText(
                String.format(
                        getString(R.string.stats_insights_most_popular_percent_views),
                        roundToInteger(mInsightsPopularModel.getHighestHourPercent())
                )
        );

        mResultContainer.addView(ll);
    }

    /*
     * Round a double to the closest integer
     *
     * If the decimal part is less than 0.5, the integer part stays the same,
     * and truncation gives the right result.
     * If the decimal part is more that 0.5, the integer part increments,
     * and again truncation gives what we want.
     *
     */
    private int roundToInteger(double inputValue) {
        return (int) Math.floor(inputValue + 0.5);
    }

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

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