Search in sources :

Example 1 with RestClientUtils

use of org.wordpress.android.networking.RestClientUtils in project WordPress-Android by wordpress-mobile.

the class ReferrerSpamHelper method showPopup.

public void showPopup(View anchor, final ReferrerGroupModel referrerGroup) {
    if (mActivityRef.get() == null || mActivityRef.get().isFinishing()) {
        return;
    }
    final PopupMenu popup = new PopupMenu(mActivityRef.get(), anchor);
    final MenuItem menuItem;
    if (referrerGroup.isRestCallInProgress) {
        menuItem = popup.getMenu().add(referrerGroup.isMarkedAsSpam ? mActivityRef.get().getString(R.string.stats_referrers_marking_not_spam) : mActivityRef.get().getString(R.string.stats_referrers_marking_spam));
    } else {
        menuItem = popup.getMenu().add(referrerGroup.isMarkedAsSpam ? mActivityRef.get().getString(R.string.stats_referrers_unspam) : mActivityRef.get().getString(R.string.stats_referrers_spam));
    }
    menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            item.setTitle(referrerGroup.isMarkedAsSpam ? mActivityRef.get().getString(R.string.stats_referrers_marking_not_spam) : mActivityRef.get().getString(R.string.stats_referrers_marking_spam));
            item.setOnMenuItemClickListener(null);
            final RestClientUtils restClientUtils = WordPress.getRestClientUtilsV1_1();
            final String restPath;
            final boolean isMarkingAsSpamInProgress;
            if (referrerGroup.isMarkedAsSpam) {
                restPath = String.format(Locale.US, "/sites/%s/stats/referrers/spam/delete/?domain=%s", referrerGroup.getBlogId(), getDomain(referrerGroup));
                isMarkingAsSpamInProgress = false;
            } else {
                restPath = String.format(Locale.US, "/sites/%s/stats/referrers/spam/new/?domain=%s", referrerGroup.getBlogId(), getDomain(referrerGroup));
                isMarkingAsSpamInProgress = true;
            }
            referrerGroup.isRestCallInProgress = true;
            ReferrerSpamRestListener vListener = new ReferrerSpamRestListener(mActivityRef.get(), referrerGroup, isMarkingAsSpamInProgress);
            restClientUtils.post(restPath, vListener, vListener);
            AppLog.d(AppLog.T.STATS, "Enqueuing the following REST request " + restPath);
            return true;
        }
    });
    popup.show();
}
Also used : RestClientUtils(org.wordpress.android.networking.RestClientUtils) MenuItem(android.view.MenuItem) PopupMenu(android.widget.PopupMenu)

Example 2 with RestClientUtils

use of org.wordpress.android.networking.RestClientUtils in project WordPress-Android by wordpress-mobile.

the class FollowHelper method showPopup.

public void showPopup(View anchor, final FollowDataModel followData) {
    if (mActivityRef.get() == null || mActivityRef.get().isFinishing()) {
        return;
    }
    final String workingText = followData.getFollowingText();
    final String followText = followData.getFollowText();
    final String unfollowText = followData.getFollowingHoverText();
    final PopupMenu popup = new PopupMenu(mActivityRef.get(), anchor);
    final MenuItem menuItem;
    if (followData.isRestCallInProgress) {
        menuItem = popup.getMenu().add(workingText);
    } else {
        menuItem = popup.getMenu().add(followData.isFollowing() ? unfollowText : followText);
    }
    menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            item.setTitle(workingText);
            item.setOnMenuItemClickListener(null);
            final RestClientUtils restClientUtils = WordPress.getRestClientUtils();
            final String restPath;
            if (!followData.isFollowing()) {
                restPath = String.format(Locale.US, "/sites/%s/follows/new", followData.getSiteID());
            } else {
                restPath = String.format(Locale.US, "/sites/%s/follows/mine/delete", followData.getSiteID());
            }
            followData.isRestCallInProgress = true;
            FollowRestListener vListener = new FollowRestListener(mActivityRef.get(), followData);
            restClientUtils.post(restPath, vListener, vListener);
            AppLog.d(AppLog.T.STATS, "Enqueuing the following REST request " + restPath);
            return true;
        }
    });
    popup.show();
}
Also used : RestClientUtils(org.wordpress.android.networking.RestClientUtils) MenuItem(android.view.MenuItem) PopupMenu(android.widget.PopupMenu)

Example 3 with RestClientUtils

use of org.wordpress.android.networking.RestClientUtils in project WordPress-Android by wordpress-mobile.

the class StatsService method startTasks.

private void startTasks(final long blogId, final StatsTimeframe timeframe, final String date, final StatsEndpointsEnum sectionToUpdate, final int maxResultsRequested, final int pageRequested) {
    EventBus.getDefault().post(new StatsEvents.UpdateStatusChanged(true));
    String cachedStats = getCachedStats(blogId, timeframe, date, sectionToUpdate, maxResultsRequested, pageRequested);
    if (cachedStats != null) {
        BaseStatsModel mResponseObjectModel;
        try {
            JSONObject response = new JSONObject(cachedStats);
            mResponseObjectModel = StatsUtils.parseResponse(sectionToUpdate, blogId, response);
            EventBus.getDefault().post(sectionToUpdate.getEndpointUpdateEvent(blogId, timeframe, date, maxResultsRequested, pageRequested, mResponseObjectModel));
            updateWidgetsUI(blogId, sectionToUpdate, timeframe, date, pageRequested, mResponseObjectModel);
            checkAllRequestsFinished(null);
            return;
        } catch (JSONException e) {
            AppLog.e(AppLog.T.STATS, e);
        }
    }
    final RestClientUtils restClientUtils = WordPress.getRestClientUtilsV1_1();
    String period = timeframe.getLabelForRestCall();
    RestListener vListener = new RestListener(sectionToUpdate, blogId, timeframe, date, maxResultsRequested, pageRequested);
    final String periodDateMaxPlaceholder = "?period=%s&date=%s&max=%s";
    String path = String.format(Locale.US, "/sites/%s/stats/" + sectionToUpdate.getRestEndpointPath(), blogId);
    synchronized (mStatsNetworkRequests) {
        switch(sectionToUpdate) {
            case VISITS:
                path = String.format(Locale.US, path + "?unit=%s&quantity=15&date=%s", period, date);
                break;
            case TOP_POSTS:
            case REFERRERS:
            case CLICKS:
            case GEO_VIEWS:
            case AUTHORS:
            case VIDEO_PLAYS:
            case SEARCH_TERMS:
                path = String.format(Locale.US, path + periodDateMaxPlaceholder, period, date, maxResultsRequested);
                break;
            case TAGS_AND_CATEGORIES:
            case PUBLICIZE:
                path = String.format(Locale.US, path + "?max=%s", maxResultsRequested);
                break;
            case COMMENTS:
                // No parameters
                break;
            case FOLLOWERS_WPCOM:
                if (pageRequested < 1) {
                    path = String.format(Locale.US, path + "&max=%s", maxResultsRequested);
                } else {
                    path = String.format(Locale.US, path + "&period=%s&date=%s&max=%s&page=%s", period, date, maxResultsRequested, pageRequested);
                }
                break;
            case FOLLOWERS_EMAIL:
                if (pageRequested < 1) {
                    path = String.format(Locale.US, path + "&max=%s", maxResultsRequested);
                } else {
                    path = String.format(Locale.US, path + "&period=%s&date=%s&max=%s&page=%s", period, date, maxResultsRequested, pageRequested);
                }
                break;
            case COMMENT_FOLLOWERS:
                if (pageRequested < 1) {
                    path = String.format(Locale.US, path + "?max=%s", maxResultsRequested);
                } else {
                    path = String.format(Locale.US, path + "?period=%s&date=%s&max=%s&page=%s", period, date, maxResultsRequested, pageRequested);
                }
                break;
            case INSIGHTS_ALL_TIME:
            case INSIGHTS_POPULAR:
                break;
            case INSIGHTS_TODAY:
                path = String.format(Locale.US, path + "?period=day&date=%s", date);
                break;
            case INSIGHTS_LATEST_POST_SUMMARY:
                // This is an edge cases since  we're not loading stats but posts
                path = String.format(Locale.US, "/sites/%s/%s", blogId, sectionToUpdate.getRestEndpointPath() + "?order_by=date&number=1&type=post&fields=ID,title,URL,discussion,like_count,date");
                break;
            case INSIGHTS_LATEST_POST_VIEWS:
                // This is a kind of edge case, since we used the pageRequested parameter to request a single postID
                path = String.format(Locale.US, path + "/%s?fields=views", pageRequested);
                break;
            default:
                AppLog.i(T.STATS, "Called an update of Stats of unknown section!?? " + sectionToUpdate.name());
                return;
        }
        // We need to check if we already have the same request in the queue
        if (checkIfRequestShouldBeEnqueued(restClientUtils, path)) {
            AppLog.d(AppLog.T.STATS, "Enqueuing the following Stats request " + path);
            Request<JSONObject> currentRequest = restClientUtils.get(path, vListener, vListener);
            vListener.currentRequest = currentRequest;
            currentRequest.setTag("StatsCall");
            mStatsNetworkRequests.add(currentRequest);
        } else {
            AppLog.d(AppLog.T.STATS, "Stats request is already in the queue:" + path);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) StatsEvents(org.wordpress.android.ui.stats.StatsEvents) RestClientUtils(org.wordpress.android.networking.RestClientUtils) JSONException(org.json.JSONException) BaseStatsModel(org.wordpress.android.ui.stats.models.BaseStatsModel)

Example 4 with RestClientUtils

use of org.wordpress.android.networking.RestClientUtils in project WordPress-Android by wordpress-mobile.

the class StatsSingleItemDetailsActivity method refreshStats.

private void refreshStats() {
    if (mIsUpdatingStats) {
        AppLog.w(AppLog.T.STATS, "stats details are already updating for the following postID " + mRemoteItemID + ", refresh cancelled.");
        return;
    }
    if (!NetworkUtils.checkConnection(this)) {
        mSwipeToRefreshHelper.setRefreshing(false);
        return;
    }
    final RestClientUtils restClientUtils = WordPress.getRestClientUtilsV1_1();
    // View and visitor counts for a site
    final String singlePostRestPath = String.format("/sites/%s/stats/post/%s", mRemoteBlogID, mRemoteItemID);
    AppLog.d(AppLog.T.STATS, "Enqueuing the following Stats request " + singlePostRestPath);
    RestBatchCallListener vListener = new RestBatchCallListener(this);
    restClientUtils.get(singlePostRestPath, vListener, vListener);
    mIsUpdatingStats = true;
    mSwipeToRefreshHelper.setRefreshing(true);
}
Also used : RestClientUtils(org.wordpress.android.networking.RestClientUtils)

Aggregations

RestClientUtils (org.wordpress.android.networking.RestClientUtils)4 MenuItem (android.view.MenuItem)2 PopupMenu (android.widget.PopupMenu)2 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 StatsEvents (org.wordpress.android.ui.stats.StatsEvents)1 BaseStatsModel (org.wordpress.android.ui.stats.models.BaseStatsModel)1