aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/stats/models/InsightsLatestPostModel.java
blob: 534348c3744e682fcf93042c9e46bf903b30884e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.wordpress.android.ui.stats.models;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class InsightsLatestPostModel extends BaseStatsModel {
    private String mBlogID;
    private String mPostTitle;
    private String mPostURL;
    private String mPostDate;
    private int mPostID;
    private int mPostViewsCount = Integer.MIN_VALUE;
    private int mPostCommentCount;
    private int mPostLikeCount;
    private int mPostsFound; // if 0 there are no posts on the blog.

    public InsightsLatestPostModel(String blogID, JSONObject response) throws JSONException {
        this.mBlogID = blogID;

        mPostsFound = response.optInt("found", 0);
        if (mPostsFound == 0) {
            // No latest post found!
           return;
        }

        JSONArray postsObject = response.getJSONArray("posts");
        if (postsObject.length() == 0) {
            throw new JSONException("Invalid document returned from the REST API");
        }

        // Read the first post
        JSONObject firstPostObject = postsObject.getJSONObject(0);

        this.mPostID = firstPostObject.getInt("ID");
        this.mPostTitle = firstPostObject.getString("title");
        this.mPostDate = firstPostObject.getString("date");
        this.mPostURL = firstPostObject.getString("URL");
        this.mPostLikeCount = firstPostObject.getInt("like_count");

        JSONObject discussionObject = firstPostObject.optJSONObject("discussion");
        if (discussionObject != null) {
            this.mPostCommentCount = discussionObject.optInt("comment_count", 0);
        }
    }

    public boolean isLatestPostAvailable() {
        return mPostsFound > 0;
    }

    public String getBlogID() {
        return mBlogID;
    }

    public String getPostDate() {
        return mPostDate;
    }

    public String getPostTitle() {
        return mPostTitle;
    }

    public String getPostURL() {
        return mPostURL;
    }

    public int getPostID() {
        return mPostID;
    }

    public int getPostViewsCount() {
        return mPostViewsCount;
    }

    public void setPostViewsCount(int postViewsCount) {
        this.mPostViewsCount = postViewsCount;
    }

    public int getPostCommentCount() {
        return mPostCommentCount;
    }

    public int getPostLikeCount() {
        return mPostLikeCount;
    }
}