Search in sources :

Example 1 with VisitModel

use of org.wordpress.android.ui.stats.models.VisitModel in project WordPress-Android by wordpress-mobile.

the class StatsVisitorsAndViewsFragment method getDataToShowOnGraph.

private VisitModel[] getDataToShowOnGraph(VisitsModel visitsData) {
    List<VisitModel> visitModels = visitsData.getVisits();
    int numPoints = Math.min(StatsUIHelper.getNumOfBarsToShow(), visitModels.size());
    int currentPointIndex = numPoints - 1;
    VisitModel[] visitModelsToShow = new VisitModel[numPoints];
    for (int i = visitModels.size() - 1; i >= 0 && currentPointIndex >= 0; i--) {
        VisitModel currentVisitModel = visitModels.get(i);
        visitModelsToShow[currentPointIndex] = currentVisitModel;
        currentPointIndex--;
    }
    return visitModelsToShow;
}
Also used : VisitModel(org.wordpress.android.ui.stats.models.VisitModel)

Example 2 with VisitModel

use of org.wordpress.android.ui.stats.models.VisitModel in project WordPress-Android by wordpress-mobile.

the class StatsInsightsTodayFragment method updateUI.

protected void updateUI() {
    super.updateUI();
    if (!isAdded() || !hasDataAvailable()) {
        return;
    }
    if (mVisitsModel.getVisits() == null || mVisitsModel.getVisits().size() == 0) {
        showErrorUI();
        return;
    }
    List<VisitModel> visits = mVisitsModel.getVisits();
    VisitModel data = visits.get(visits.size() - 1);
    LinearLayout ll = (LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.stats_insights_today_item, (ViewGroup) mResultContainer.getRootView(), false);
    LinearLayout tabs = (LinearLayout) ll.findViewById(R.id.stats_post_tabs);
    for (int i = 0; i < tabs.getChildCount(); i++) {
        LinearLayout currentTab = (LinearLayout) tabs.getChildAt(i);
        switch(i) {
            case 0:
                setupTab(currentTab, FormatUtils.formatDecimal(data.getViews()), StatsVisitorsAndViewsFragment.OverviewLabel.VIEWS);
                break;
            case 1:
                setupTab(currentTab, FormatUtils.formatDecimal(data.getVisitors()), StatsVisitorsAndViewsFragment.OverviewLabel.VISITORS);
                break;
            case 2:
                setupTab(currentTab, FormatUtils.formatDecimal(data.getLikes()), StatsVisitorsAndViewsFragment.OverviewLabel.LIKES);
                break;
            case 3:
                setupTab(currentTab, FormatUtils.formatDecimal(data.getComments()), StatsVisitorsAndViewsFragment.OverviewLabel.COMMENTS);
                break;
        }
    }
    mResultContainer.addView(ll);
}
Also used : ViewGroup(android.view.ViewGroup) VisitModel(org.wordpress.android.ui.stats.models.VisitModel) LinearLayout(android.widget.LinearLayout)

Example 3 with VisitModel

use of org.wordpress.android.ui.stats.models.VisitModel in project WordPress-Android by wordpress-mobile.

the class RemoteTests method testVisits.

public void testVisits() throws Exception {
    StatsRestRequestAbstractListener listener = new StatsRestRequestAbstractListener() {

        @Override
        void parseResponse(JSONObject response) throws JSONException {
            VisitsModel model = new VisitsModel(123456, response);
            assertNotNull(model.getVisits());
            assertNotNull(model.getUnit());
            assertNotNull(model.getDate());
            assertEquals(model.getVisits().size(), 30);
            assertEquals(model.getUnit(), "day");
            VisitModel visitModel = model.getVisits().get(0);
            assertEquals(visitModel.getViews(), 7808);
            assertEquals(visitModel.getVisitors(), 4331);
            assertEquals(visitModel.getLikes(), 0);
            assertEquals(visitModel.getComments(), 0);
            assertEquals(visitModel.getPeriod(), "2014-10-08");
        }
    };
    mRestClient.makeRequest(Request.Method.POST, "https://public-api.wordpress.com/rest/v1.1/sites/123456/stats/visits", null, listener, errListener);
}
Also used : JSONObject(org.json.JSONObject) VisitModel(org.wordpress.android.ui.stats.models.VisitModel) VisitsModel(org.wordpress.android.ui.stats.models.VisitsModel)

Example 4 with VisitModel

use of org.wordpress.android.ui.stats.models.VisitModel in project WordPress-Android by wordpress-mobile.

the class StatsVisitorsAndViewsFragment method updateUIBelowTheGraph.

//update the area right below the graph
private void updateUIBelowTheGraph(int itemPosition) {
    if (!isAdded()) {
        return;
    }
    if (mVisitsData == null) {
        setupNoResultsUI(false);
        return;
    }
    final VisitModel[] dataToShowOnGraph = getDataToShowOnGraph(mVisitsData);
    // Make sure we've data to show on the screen
    if (dataToShowOnGraph.length == 0) {
        return;
    }
    // This check should never be true, since we put a check on the index in the calling function updateUI()
    if (dataToShowOnGraph.length <= itemPosition || itemPosition == -1) {
        // Make sure we're not highlighting
        itemPosition = dataToShowOnGraph.length - 1;
    }
    String date = mStatsDate[itemPosition];
    if (date == null) {
        AppLog.w(AppLog.T.STATS, "Cannot update the area below the graph if a null date is passed!!");
        return;
    }
    mDateTextView.setText(getDateForDisplayInLabels(date, getTimeframe()));
    VisitModel modelTapped = dataToShowOnGraph[itemPosition];
    for (int i = 0; i < mModuleButtonsContainer.getChildCount(); i++) {
        View o = mModuleButtonsContainer.getChildAt(i);
        if (o instanceof LinearLayout && o.getTag() instanceof TabViewHolder) {
            TabViewHolder tabViewHolder = (TabViewHolder) o.getTag();
            int currentValue = 0;
            switch(tabViewHolder.labelItem) {
                case VIEWS:
                    currentValue = modelTapped.getViews();
                    break;
                case VISITORS:
                    currentValue = modelTapped.getVisitors();
                    break;
                case LIKES:
                    currentValue = modelTapped.getLikes();
                    break;
                case COMMENTS:
                    currentValue = modelTapped.getComments();
                    break;
            }
            tabViewHolder.value.setText(FormatUtils.formatDecimal(currentValue));
            tabViewHolder.updateBackGroundAndIcon(currentValue);
        }
    }
}
Also used : VisitModel(org.wordpress.android.ui.stats.models.VisitModel) ImageView(android.widget.ImageView) View(android.view.View) GraphView(com.jjoe64.graphview.GraphView) TextView(android.widget.TextView) CheckedTextView(android.widget.CheckedTextView) LinearLayout(android.widget.LinearLayout)

Example 5 with VisitModel

use of org.wordpress.android.ui.stats.models.VisitModel in project WordPress-Android by wordpress-mobile.

the class StatsVisitorsAndViewsFragment method updateUI.

protected void updateUI() {
    if (!isAdded()) {
        return;
    }
    if (mVisitsData == null) {
        setupNoResultsUI(false);
        return;
    }
    final VisitModel[] dataToShowOnGraph = getDataToShowOnGraph(mVisitsData);
    if (dataToShowOnGraph == null || dataToShowOnGraph.length == 0) {
        setupNoResultsUI(false);
        return;
    }
    // Hide the "no-activity this period" message
    mNoActivtyThisPeriodContainer.setVisibility(View.GONE);
    // Read the selected Tab in the UI
    OverviewLabel selectedStatsType = overviewItems[mSelectedOverviewItemIndex];
    // Update the Legend and enable/disable the visitors checkboxes
    mLegendContainer.setVisibility(View.VISIBLE);
    mLegendLabel.setText(StringUtils.capitalize(selectedStatsType.getLabel().toLowerCase()));
    switch(selectedStatsType) {
        case VIEWS:
            mVisitorsCheckboxContainer.setVisibility(View.VISIBLE);
            mVisitorsCheckbox.setEnabled(true);
            mVisitorsCheckbox.setChecked(mIsCheckboxChecked);
            break;
        default:
            mVisitorsCheckboxContainer.setVisibility(View.GONE);
            break;
    }
    // Setting Up labels and prepare variables that hold series
    final String[] horLabels = new String[dataToShowOnGraph.length];
    mStatsDate = new String[dataToShowOnGraph.length];
    GraphView.GraphViewData[] mainSeriesItems = new GraphView.GraphViewData[dataToShowOnGraph.length];
    GraphView.GraphViewData[] secondarySeriesItems = null;
    if (mIsCheckboxChecked && selectedStatsType == OverviewLabel.VIEWS) {
        secondarySeriesItems = new GraphView.GraphViewData[dataToShowOnGraph.length];
    }
    // index of days that should be XXX on the graph
    final boolean[] weekendDays;
    if (getTimeframe() == StatsTimeframe.DAY) {
        weekendDays = new boolean[dataToShowOnGraph.length];
    } else {
        weekendDays = null;
    }
    // Check we have at least one result in the current section.
    boolean atLeastOneResultIsAvailable = false;
    // Fill series variables with data
    for (int i = 0; i < dataToShowOnGraph.length; i++) {
        int currentItemValue = 0;
        switch(selectedStatsType) {
            case VIEWS:
                currentItemValue = dataToShowOnGraph[i].getViews();
                break;
            case VISITORS:
                currentItemValue = dataToShowOnGraph[i].getVisitors();
                break;
            case LIKES:
                currentItemValue = dataToShowOnGraph[i].getLikes();
                break;
            case COMMENTS:
                currentItemValue = dataToShowOnGraph[i].getComments();
                break;
        }
        mainSeriesItems[i] = new GraphView.GraphViewData(i, currentItemValue);
        if (currentItemValue > 0) {
            atLeastOneResultIsAvailable = true;
        }
        if (mIsCheckboxChecked && secondarySeriesItems != null) {
            secondarySeriesItems[i] = new GraphView.GraphViewData(i, dataToShowOnGraph[i].getVisitors());
        }
        String currentItemStatsDate = dataToShowOnGraph[i].getPeriod();
        horLabels[i] = getDateLabelForBarInGraph(currentItemStatsDate);
        mStatsDate[i] = currentItemStatsDate;
        if (weekendDays != null) {
            SimpleDateFormat from = new SimpleDateFormat(StatsConstants.STATS_INPUT_DATE_FORMAT);
            try {
                Date date = from.parse(currentItemStatsDate);
                Calendar c = Calendar.getInstance();
                c.setFirstDayOfWeek(Calendar.MONDAY);
                c.setTimeInMillis(date.getTime());
                weekendDays[i] = c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY;
            } catch (ParseException e) {
                weekendDays[i] = false;
                AppLog.e(AppLog.T.STATS, e);
            }
        }
    }
    if (mGraphContainer.getChildCount() >= 1 && mGraphContainer.getChildAt(0) instanceof GraphView) {
        mGraphView = (StatsBarGraph) mGraphContainer.getChildAt(0);
    } else {
        mGraphContainer.removeAllViews();
        mGraphView = new StatsBarGraph(getActivity());
        mGraphContainer.addView(mGraphView);
    }
    mGraphView.removeAllSeries();
    GraphViewSeries mainSeriesOnScreen = new GraphViewSeries(mainSeriesItems);
    mainSeriesOnScreen.getStyle().color = getResources().getColor(R.color.stats_bar_graph_main_series);
    mainSeriesOnScreen.getStyle().outerColor = getResources().getColor(R.color.grey_lighten_30_translucent_50);
    mainSeriesOnScreen.getStyle().highlightColor = getResources().getColor(R.color.stats_bar_graph_main_series_highlight);
    mainSeriesOnScreen.getStyle().outerhighlightColor = getResources().getColor(R.color.stats_bar_graph_outer_highlight);
    mainSeriesOnScreen.getStyle().padding = DisplayUtils.dpToPx(getActivity(), 5);
    mGraphView.addSeries(mainSeriesOnScreen);
    // Add the Visitors series if it's checked in the legend
    if (mIsCheckboxChecked && secondarySeriesItems != null && selectedStatsType == OverviewLabel.VIEWS) {
        GraphViewSeries secondarySeries = new GraphViewSeries(secondarySeriesItems);
        secondarySeries.getStyle().padding = DisplayUtils.dpToPx(getActivity(), 10);
        secondarySeries.getStyle().color = getResources().getColor(R.color.stats_bar_graph_secondary_series);
        secondarySeries.getStyle().highlightColor = getResources().getColor(R.color.orange_fire);
        mGraphView.addSeries(secondarySeries);
    }
    // the purpose of making these bars visually easily to compare.
    switch(selectedStatsType) {
        case VISITORS:
            double maxYValue = getMaxYValueForVisitorsAndView(dataToShowOnGraph);
            mGraphView.setManualYAxisBounds(maxYValue, 0d);
            break;
        default:
            mGraphView.setManualYAxis(false);
            break;
    }
    // Set the Graph Style
    mGraphView.getGraphViewStyle().setNumHorizontalLabels(dataToShowOnGraph.length);
    // Set the maximum size a column can get on the screen in PX
    mGraphView.getGraphViewStyle().setMaxColumnWidth(DisplayUtils.dpToPx(getActivity(), StatsConstants.STATS_GRAPH_BAR_MAX_COLUMN_WIDTH_DP));
    mGraphView.setHorizontalLabels(horLabels);
    mGraphView.setGestureListener(this);
    // If zero results in the current section disable clicks on the graph and show the dialog.
    mNoActivtyThisPeriodContainer.setVisibility(atLeastOneResultIsAvailable ? View.GONE : View.VISIBLE);
    mGraphView.setClickable(atLeastOneResultIsAvailable);
    // Draw the background on weekend days
    mGraphView.setWeekendDays(weekendDays);
    // Only happens on 720DP tablets
    if (mPrevNumberOfBarsGraph != -1 && mPrevNumberOfBarsGraph != dataToShowOnGraph.length) {
        mSelectedBarGraphBarIndex = -1;
        mPrevNumberOfBarsGraph = dataToShowOnGraph.length;
        onBarTapped(dataToShowOnGraph.length - 1);
        mGraphView.highlightBar(dataToShowOnGraph.length - 1);
        return;
    }
    mPrevNumberOfBarsGraph = dataToShowOnGraph.length;
    int barSelectedOnGraph;
    if (mSelectedBarGraphBarIndex == -1) {
        // No previous bar was highlighted, highlight the most recent one
        barSelectedOnGraph = dataToShowOnGraph.length - 1;
    } else if (mSelectedBarGraphBarIndex < dataToShowOnGraph.length) {
        barSelectedOnGraph = mSelectedBarGraphBarIndex;
    } else {
        // A previous bar was highlighted, but it's out of the screen now. This should never happen atm.
        barSelectedOnGraph = dataToShowOnGraph.length - 1;
        mSelectedBarGraphBarIndex = barSelectedOnGraph;
    }
    updateUIBelowTheGraph(barSelectedOnGraph);
    mGraphView.highlightBar(barSelectedOnGraph);
}
Also used : Calendar(java.util.Calendar) VisitModel(org.wordpress.android.ui.stats.models.VisitModel) GraphView(com.jjoe64.graphview.GraphView) Date(java.util.Date) GraphViewSeries(com.jjoe64.graphview.GraphViewSeries) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

VisitModel (org.wordpress.android.ui.stats.models.VisitModel)7 GraphView (com.jjoe64.graphview.GraphView)3 LinearLayout (android.widget.LinearLayout)2 GraphViewSeries (com.jjoe64.graphview.GraphViewSeries)2 VisitsModel (org.wordpress.android.ui.stats.models.VisitsModel)2 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 CheckedTextView (android.widget.CheckedTextView)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 VolleyError (com.android.volley.VolleyError)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 JSONObject (org.json.JSONObject)1 SiteModel (org.wordpress.android.fluxc.model.SiteModel)1 StatsError (org.wordpress.android.ui.stats.exceptions.StatsError)1