Search in sources :

Example 1 with GeoviewModel

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

the class StatsGeoviewsFragment method updateUI.

@Override
protected void updateUI() {
    if (!isAdded()) {
        return;
    }
    if (hasCountries()) {
        List<GeoviewModel> countries = getCountries();
        ArrayAdapter adapter = new GeoviewsAdapter(getActivity(), countries);
        StatsUIHelper.reloadLinearLayout(getActivity(), adapter, mList, getMaxNumberOfItemsToShowInList());
        showHideNoResultsUI(false);
        showMap(countries);
    } else {
        showHideNoResultsUI(true);
        hideMap();
    }
}
Also used : GeoviewModel(org.wordpress.android.ui.stats.models.GeoviewModel) ArrayAdapter(android.widget.ArrayAdapter)

Example 2 with GeoviewModel

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

the class RemoteTests method testCountryViewsDay.

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

        @Override
        void parseResponse(JSONObject response) throws JSONException {
            GeoviewsModel model = new GeoviewsModel(123456, response);
            assertEquals(model.getOtherViews(), 17);
            assertEquals(model.getTotalViews(), 55);
            assertNotNull(model.getCountries());
            assertEquals(model.getCountries().size(), 10);
            GeoviewModel first = model.getCountries().get(0);
            assertEquals(first.getCountryFullName(), "United States");
            assertEquals(first.getFlagIconURL(), "https://secure.gravatar.com/blavatar/5a83891a81b057fed56930a6aaaf7b3c?s=48");
            assertEquals(first.getFlatFlagIconURL(), "https://secure.gravatar.com/blavatar/9f4faa5ad0c723474f7a6d810172447c?s=48");
            assertEquals(first.getViews(), 8);
            GeoviewModel second = model.getCountries().get(1);
            assertEquals(second.getCountryFullName(), "Taiwan");
            assertEquals(second.getFlagIconURL(), "https://secure.gravatar.com/blavatar/f983fff0dda7387746b697cfd865e657?s=48");
            assertEquals(second.getFlatFlagIconURL(), "https://secure.gravatar.com/blavatar/2c224480a40527ee89d7340d4396e8e6?s=48");
            assertEquals(second.getViews(), 6);
        }
    };
    mRestClient.makeRequest(Request.Method.POST, "https://public-api.wordpress.com/rest/v1.1/sites/123456/stats/country-views", null, listener, errListener);
}
Also used : GeoviewsModel(org.wordpress.android.ui.stats.models.GeoviewsModel) JSONObject(org.json.JSONObject) GeoviewModel(org.wordpress.android.ui.stats.models.GeoviewModel)

Example 3 with GeoviewModel

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

the class StatsGeoviewsFragment method showMap.

private void showMap(final List<GeoviewModel> countries) {
    if (!isAdded()) {
        return;
    }
    // setting up different margins for the map. We're basically remove left margins since the
    // chart service produce a map that's slightly shifted on the right. See the Web version.
    int dp4 = DisplayUtils.dpToPx(mTopPagerContainer.getContext(), 4);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(0, 0, dp4, 0);
    mTopPagerContainer.setLayoutParams(layoutParams);
    mTopPagerContainer.removeAllViews();
    // must wait for mTopPagerContainer to be fully laid out (ie: measured). Then we can read the width and
    // calculate the right height for the map div
    mTopPagerContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            mTopPagerContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            if (!isAdded()) {
                return;
            }
            StringBuilder dataToLoad = new StringBuilder();
            for (int i = 0; i < countries.size(); i++) {
                final GeoviewModel currentCountry = countries.get(i);
                dataToLoad.append("['").append(currentCountry.getCountryFullName()).append("',").append(currentCountry.getViews()).append("],");
            }
            // This is the label that is shown when the user taps on a region
            String label = getResources().getString(getTotalsLabelResId());
            // See: https://developers.google.com/chart/interactive/docs/gallery/geochart
            // Loading the v42 of the Google Charts API, since the latest stable version has a problem with the legend. https://github.com/wordpress-mobile/WordPress-Android/issues/4131
            // https://developers.google.com/chart/interactive/docs/release_notes#release-candidate-details
            String htmlPage = "<html>" + "<head>" + "<script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>" + "<script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>" + "<script type=\"text/javascript\">" + "google.charts.load('42', {'packages':['geochart']});" + "google.charts.setOnLoadCallback(drawRegionsMap);" + "function drawRegionsMap() {" + "var data = google.visualization.arrayToDataTable(" + "[" + "['Country', '" + label + "']," + dataToLoad + "]);" + "var options = {keepAspectRatio: true, region: 'world', colorAxis: { colors: [ '#FFF088', '#F34605' ] }, enableRegionInteractivity: true};" + "var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));" + "chart.draw(data, options);" + "}" + "</script>" + "</head>" + "<body>" + "<div id=\"regions_div\" style=\"width: 100%; height: 100%;\"></div>" + "</body>" + "</html>";
            WebView webView = new WebView(getActivity());
            mTopPagerContainer.addView(webView);
            int width = mTopPagerContainer.getWidth();
            int height = width * 3 / 4;
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) webView.getLayoutParams();
            params.width = WebView.LayoutParams.MATCH_PARENT;
            params.height = height;
            webView.setLayoutParams(params);
            // Hide map in case of unrecoverable errors
            webView.setWebViewClient(new MyWebViewClient());
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            webView.loadData(htmlPage, "text/html", "UTF-8");
        }
    });
    mTopPagerContainer.setVisibility(View.VISIBLE);
}
Also used : GeoviewModel(org.wordpress.android.ui.stats.models.GeoviewModel) WebView(android.webkit.WebView) ViewTreeObserver(android.view.ViewTreeObserver) LinearLayout(android.widget.LinearLayout)

Aggregations

GeoviewModel (org.wordpress.android.ui.stats.models.GeoviewModel)3 ViewTreeObserver (android.view.ViewTreeObserver)1 WebView (android.webkit.WebView)1 ArrayAdapter (android.widget.ArrayAdapter)1 LinearLayout (android.widget.LinearLayout)1 JSONObject (org.json.JSONObject)1 GeoviewsModel (org.wordpress.android.ui.stats.models.GeoviewsModel)1