Search in sources :

Example 1 with ReaderTagList

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

the class ReaderTagActions method addTag.

public static boolean addTag(final ReaderTag tag, final ReaderActions.ActionListener actionListener) {
    if (tag == null) {
        ReaderActions.callActionListener(actionListener, false);
        return false;
    }
    final String tagNameForApi = ReaderUtils.sanitizeWithDashes(tag.getTagSlug());
    final String path = "read/tags/" + tagNameForApi + "/mine/new";
    String endpoint = "/read/tags/" + tagNameForApi + "/posts";
    ReaderTag newTag = new ReaderTag(tag.getTagSlug(), tag.getTagDisplayName(), tag.getTagTitle(), endpoint, ReaderTagType.FOLLOWED);
    com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {

        @Override
        public void onResponse(JSONObject jsonObject) {
            AppLog.i(T.READER, "add tag  succeeded");
            // the response will contain the list of the user's followed tags
            ReaderTagList tags = parseFollowedTags(jsonObject);
            ReaderTagTable.replaceFollowedTags(tags);
            ReaderActions.callActionListener(actionListener, true);
        }
    };
    RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            // treat is as a success if we're adding a tag and the error says the user is
            // already following it
            String error = VolleyUtils.errStringFromVolleyError(volleyError);
            if (error.equals("already_subscribed")) {
                AppLog.w(T.READER, "add tag succeeded with error " + error);
                ReaderActions.callActionListener(actionListener, true);
                return;
            }
            AppLog.w(T.READER, "add tag failed");
            AppLog.e(T.READER, volleyError);
            // revert on failure
            ReaderTagTable.deleteTag(tag);
            ReaderActions.callActionListener(actionListener, false);
        }
    };
    ReaderTagTable.addOrUpdateTag(newTag);
    WordPress.getRestClientUtilsV1_1().post(path, listener, errorListener);
    return true;
}
Also used : ReaderTag(org.wordpress.android.models.ReaderTag) VolleyError(com.android.volley.VolleyError) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) ReaderTagList(org.wordpress.android.models.ReaderTagList)

Example 2 with ReaderTagList

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

the class ReaderTagTable method getTagsOfType.

private static ReaderTagList getTagsOfType(ReaderTagType tagType) {
    String[] args = { Integer.toString(tagType.toInt()) };
    Cursor c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags WHERE tag_type=? ORDER BY tag_slug", args);
    try {
        ReaderTagList tagList = new ReaderTagList();
        if (c.moveToFirst()) {
            do {
                tagList.add(getTagFromCursor(c));
            } while (c.moveToNext());
        }
        return tagList;
    } finally {
        SqlUtils.closeCursor(c);
    }
}
Also used : ReaderTagList(org.wordpress.android.models.ReaderTagList) Cursor(android.database.Cursor)

Example 3 with ReaderTagList

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

the class ReaderPostTable method purge.

/*
     * purge table of unattached/older posts - no need to wrap this in a transaction since it's
     * only called from ReaderDatabase.purge() which already creates a transaction
     */
protected static int purge(SQLiteDatabase db) {
    // delete posts attached to tags that no longer exist
    int numDeleted = db.delete("tbl_posts", "tag_name NOT IN (SELECT DISTINCT tag_name FROM tbl_tags)", null);
    // delete excess posts on a per-tag basis
    ReaderTagList tags = ReaderTagTable.getAllTags();
    for (ReaderTag tag : tags) {
        numDeleted += purgePostsForTag(db, tag);
    }
    // delete search results
    numDeleted += purgeSearchResults(db);
    return numDeleted;
}
Also used : ReaderTag(org.wordpress.android.models.ReaderTag) ReaderTagList(org.wordpress.android.models.ReaderTagList)

Example 4 with ReaderTagList

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

the class ReaderTagActions method parseFollowedTags.

/*
     * return the user's followed tags from the response to read/tags/{tag}/mine/new
     */
/*
        {
        "added_tag": "84776",
        "subscribed": true,
        "tags": [
            {
                "display_name": "fitness",
                "ID": "5189",
                "slug": "fitness",
                "title": "Fitness",
                "URL": "https://public-api.wordpress.com/rest/v1.1/read/tags/fitness/posts"
            },
            ...
        }
     */
private static ReaderTagList parseFollowedTags(JSONObject jsonObject) {
    if (jsonObject == null) {
        return null;
    }
    JSONArray jsonTags = jsonObject.optJSONArray(ReaderConstants.JSON_TAG_TAGS_ARRAY);
    if (jsonTags == null || jsonTags.length() == 0) {
        return null;
    }
    ReaderTagList tags = new ReaderTagList();
    for (int i = 0; i < jsonTags.length(); i++) {
        JSONObject jsonThisTag = jsonTags.optJSONObject(i);
        String tagTitle = JSONUtils.getStringDecoded(jsonThisTag, ReaderConstants.JSON_TAG_TITLE);
        String tagDisplayName = JSONUtils.getStringDecoded(jsonThisTag, ReaderConstants.JSON_TAG_DISPLAY_NAME);
        String tagSlug = JSONUtils.getStringDecoded(jsonThisTag, ReaderConstants.JSON_TAG_SLUG);
        String endpoint = JSONUtils.getString(jsonThisTag, ReaderConstants.JSON_TAG_URL);
        tags.add(new ReaderTag(tagSlug, tagDisplayName, tagTitle, endpoint, ReaderTagType.FOLLOWED));
    }
    return tags;
}
Also used : ReaderTag(org.wordpress.android.models.ReaderTag) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ReaderTagList(org.wordpress.android.models.ReaderTagList)

Example 5 with ReaderTagList

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

the class ReaderUpdateService method parseTags.

/*
     * parse a specific topic section from the topic response
     */
private static ReaderTagList parseTags(JSONObject jsonObject, String name, ReaderTagType tagType) {
    ReaderTagList topics = new ReaderTagList();
    if (jsonObject == null) {
        return topics;
    }
    JSONObject jsonTopics = jsonObject.optJSONObject(name);
    if (jsonTopics == null) {
        return topics;
    }
    Iterator<String> it = jsonTopics.keys();
    while (it.hasNext()) {
        String internalName = it.next();
        JSONObject jsonTopic = jsonTopics.optJSONObject(internalName);
        if (jsonTopic != null) {
            String tagTitle = JSONUtils.getStringDecoded(jsonTopic, ReaderConstants.JSON_TAG_TITLE);
            String tagDisplayName = JSONUtils.getStringDecoded(jsonTopic, ReaderConstants.JSON_TAG_DISPLAY_NAME);
            String tagSlug = JSONUtils.getStringDecoded(jsonTopic, ReaderConstants.JSON_TAG_SLUG);
            String endpoint = JSONUtils.getString(jsonTopic, ReaderConstants.JSON_TAG_URL);
            // included in the response as default tags
            if (tagType == ReaderTagType.DEFAULT && endpoint.contains("/read/list/")) {
                topics.add(new ReaderTag(tagSlug, tagDisplayName, tagTitle, endpoint, ReaderTagType.CUSTOM_LIST));
            } else {
                topics.add(new ReaderTag(tagSlug, tagDisplayName, tagTitle, endpoint, tagType));
            }
        }
    }
    return topics;
}
Also used : ReaderTag(org.wordpress.android.models.ReaderTag) JSONObject(org.json.JSONObject) ReaderTagList(org.wordpress.android.models.ReaderTagList)

Aggregations

ReaderTagList (org.wordpress.android.models.ReaderTagList)6 ReaderTag (org.wordpress.android.models.ReaderTag)4 JSONObject (org.json.JSONObject)3 Cursor (android.database.Cursor)1 VolleyError (com.android.volley.VolleyError)1 RestRequest (com.wordpress.rest.RestRequest)1 JSONArray (org.json.JSONArray)1