Search in sources :

Example 6 with Person

use of org.wordpress.android.models.Person in project WordPress-Android by wordpress-mobile.

the class PeopleUtils method fetchViewers.

public static void fetchViewers(final SiteModel site, final int offset, final FetchViewersCallback callback) {
    com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {

        @Override
        public void onResponse(JSONObject jsonObject) {
            if (jsonObject != null && callback != null) {
                try {
                    JSONArray jsonArray = jsonObject.getJSONArray("viewers");
                    List<Person> people = peopleListFromJSON(jsonArray, site.getId(), Person.PersonType.VIEWER);
                    int numberOfUsers = jsonObject.optInt("found");
                    boolean isEndOfList = (people.size() + offset) >= numberOfUsers;
                    callback.onSuccess(people, isEndOfList);
                } catch (JSONException e) {
                    AppLog.e(T.API, "JSON exception occurred while parsing the response for " + "sites/%s/viewers: " + e);
                    callback.onError();
                }
            }
        }
    };
    RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            AppLog.e(T.API, volleyError);
            if (callback != null) {
                callback.onError();
            }
        }
    };
    int page = (offset / FETCH_LIMIT) + 1;
    Map<String, String> params = new HashMap<>();
    params.put("number", Integer.toString(FETCH_LIMIT));
    params.put("page", Integer.toString(page));
    String path = String.format(Locale.US, "sites/%d/viewers", site.getSiteId());
    WordPress.getRestClientUtilsV1_1().get(path, params, null, listener, errorListener);
}
Also used : VolleyError(com.android.volley.VolleyError) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) Person(org.wordpress.android.models.Person)

Example 7 with Person

use of org.wordpress.android.models.Person in project WordPress-Android by wordpress-mobile.

the class PeopleUtils method updateRole.

public static void updateRole(final SiteModel site, long personID, Role newRole, final int localTableBlogId, final UpdateUserCallback callback) {
    com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {

        @Override
        public void onResponse(JSONObject jsonObject) {
            if (jsonObject != null && callback != null) {
                try {
                    Person person = Person.userFromJSON(jsonObject, localTableBlogId);
                    if (person != null) {
                        callback.onSuccess(person);
                    } else {
                        AppLog.e(T.API, "Couldn't map jsonObject + " + jsonObject + " to person model.");
                        callback.onError();
                    }
                } catch (JSONException e) {
                    callback.onError();
                }
            }
        }
    };
    RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            AppLog.e(T.API, volleyError);
            if (callback != null) {
                callback.onError();
            }
        }
    };
    Map<String, String> params = new HashMap<>();
    params.put("roles", newRole.toRESTString());
    String path = String.format(Locale.US, "sites/%d/users/%d", site.getSiteId(), personID);
    WordPress.getRestClientUtilsV1_1().post(path, params, null, listener, errorListener);
}
Also used : VolleyError(com.android.volley.VolleyError) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JSONException(org.json.JSONException) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) Person(org.wordpress.android.models.Person)

Example 8 with Person

use of org.wordpress.android.models.Person in project WordPress-Android by wordpress-mobile.

the class PeopleUtils method fetchUsers.

public static void fetchUsers(final SiteModel site, final int offset, final FetchUsersCallback callback) {
    com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {

        @Override
        public void onResponse(JSONObject jsonObject) {
            if (jsonObject != null && callback != null) {
                try {
                    JSONArray jsonArray = jsonObject.getJSONArray("users");
                    List<Person> people = peopleListFromJSON(jsonArray, site.getId(), Person.PersonType.USER);
                    int numberOfUsers = jsonObject.optInt("found");
                    boolean isEndOfList = (people.size() + offset) >= numberOfUsers;
                    callback.onSuccess(people, isEndOfList);
                } catch (JSONException e) {
                    AppLog.e(T.API, "JSON exception occurred while parsing the response for sites/%s/users: " + e);
                    callback.onError();
                }
            }
        }
    };
    RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            AppLog.e(T.API, volleyError);
            if (callback != null) {
                callback.onError();
            }
        }
    };
    Map<String, String> params = new HashMap<>();
    params.put("number", Integer.toString(PeopleUtils.FETCH_LIMIT));
    params.put("offset", Integer.toString(offset));
    params.put("order_by", "display_name");
    params.put("order", "ASC");
    String path = String.format(Locale.US, "sites/%d/users", site.getSiteId());
    WordPress.getRestClientUtilsV1_1().get(path, params, null, listener, errorListener);
}
Also used : VolleyError(com.android.volley.VolleyError) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) Person(org.wordpress.android.models.Person)

Example 9 with Person

use of org.wordpress.android.models.Person in project WordPress-Android by wordpress-mobile.

the class PeopleManagementActivity method removeSelectedPerson.

private void removeSelectedPerson() {
    if (!NetworkUtils.checkConnection(this)) {
        return;
    }
    Person person = getCurrentPerson();
    if (person == null) {
        return;
    }
    final Person.PersonType personType = person.getPersonType();
    final String displayName = person.getDisplayName();
    PeopleUtils.RemovePersonCallback callback = new PeopleUtils.RemovePersonCallback() {

        @Override
        public void onSuccess(long personID, int localTableBlogId) {
            if (personType == Person.PersonType.USER) {
                AnalyticsUtils.trackWithSiteDetails(AnalyticsTracker.Stat.PERSON_REMOVED, mSite);
            }
            // remove the person from db, navigate back to list fragment and refresh it
            PeopleTable.deletePerson(personID, localTableBlogId, personType);
            String message = getString(R.string.person_removed, displayName);
            ToastUtils.showToast(PeopleManagementActivity.this, message, ToastUtils.Duration.LONG);
            navigateBackToPeopleListFragment();
            refreshPeopleListFragment();
        }

        @Override
        public void onError() {
            int errorMessageRes;
            switch(personType) {
                case USER:
                    errorMessageRes = R.string.error_remove_user;
                    break;
                case VIEWER:
                    errorMessageRes = R.string.error_remove_viewer;
                    break;
                default:
                    errorMessageRes = R.string.error_remove_follower;
                    break;
            }
            ToastUtils.showToast(PeopleManagementActivity.this, errorMessageRes, ToastUtils.Duration.LONG);
        }
    };
    if (personType == Person.PersonType.FOLLOWER || personType == Person.PersonType.EMAIL_FOLLOWER) {
        PeopleUtils.removeFollower(mSite, person.getPersonID(), personType, callback);
    } else if (personType == Person.PersonType.VIEWER) {
        PeopleUtils.removeViewer(mSite, person.getPersonID(), callback);
    } else {
        PeopleUtils.removeUser(mSite, person.getPersonID(), callback);
    }
}
Also used : Person(org.wordpress.android.models.Person) PeopleUtils(org.wordpress.android.ui.people.utils.PeopleUtils)

Example 10 with Person

use of org.wordpress.android.models.Person in project WordPress-Android by wordpress-mobile.

the class PersonDetailFragment method showRoleChangeDialog.

private void showRoleChangeDialog() {
    Person person = loadPerson();
    if (person == null || person.getRole() == null) {
        return;
    }
    RoleChangeDialogFragment dialog = RoleChangeDialogFragment.newInstance(person.getPersonID(), mSiteStore.getSiteByLocalId(mLocalTableBlogId), person.getRole());
    dialog.show(getFragmentManager(), null);
}
Also used : Person(org.wordpress.android.models.Person)

Aggregations

Person (org.wordpress.android.models.Person)11 VolleyError (com.android.volley.VolleyError)4 RestRequest (com.wordpress.rest.RestRequest)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 JSONArray (org.json.JSONArray)3 PeopleUtils (org.wordpress.android.ui.people.utils.PeopleUtils)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Cursor (android.database.Cursor)1 ArrayList (java.util.ArrayList)1