aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/models/ReaderPostDiscoverData.java
blob: b525708a7bdcb4865596dc2b3f1f79d911760dca (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package org.wordpress.android.models;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONObject;
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.util.JSONUtils;
import org.wordpress.android.util.StringUtils;

/**
 * additional data for "discover" posts in the reader - these are posts chosen by
 * Editorial which highlight other posts or sites - the reader shows an attribution
 * line for these posts, and when tapped they open the original post - the like
 * and comment counts come from the original post
 */
public class ReaderPostDiscoverData {

    public enum DiscoverType {
        EDITOR_PICK,
        SITE_PICK,
        OTHER
    }

    private String authorName;
    private String authorUrl;
    private String blogName;
    private String blogUrl;
    private String avatarUrl;
    private final String permaLink;

    private long blogId;
    private long postId;

    private int numLikes;
    private int numComments;

    private DiscoverType discoverType = DiscoverType.OTHER;

    /*
     * passed JSONObject is the "discover_metadata" section of a reader post
     */
    public ReaderPostDiscoverData(@NonNull JSONObject json) {
        permaLink = json.optString("permalink");

        JSONObject jsonAttribution = json.optJSONObject("attribution");
        if (jsonAttribution != null) {
            authorName = jsonAttribution.optString("author_name");
            authorUrl = jsonAttribution.optString("author_url");
            blogName = jsonAttribution.optString("blog_name");
            blogUrl = jsonAttribution.optString("blog_url");
            avatarUrl = jsonAttribution.optString("avatar_url");
        }

        JSONObject jsonWpcomData = json.optJSONObject("featured_post_wpcom_data");
        if (jsonWpcomData != null) {
            blogId = jsonWpcomData.optLong("blog_id");
            postId = jsonWpcomData.optLong("post_id");
            numLikes = jsonWpcomData.optInt("like_count");
            numComments = jsonWpcomData.optInt("comment_count");
        }

        // walk the post formats array until we find one we know we should handle differently
        //  - image-pick, quote-pick, and standard-pick all display as editors picks
        //  - site-pick displays as a site pick
        //  - collection + feature can be ignored because those display the same as normal posts
        JSONArray jsonPostFormats = json.optJSONArray("discover_fp_post_formats");
        if (jsonPostFormats != null) {
            for (int i = 0; i < jsonPostFormats.length(); i++) {
                String slug = JSONUtils.getString(jsonPostFormats.optJSONObject(i), "slug");
                if (slug.equals("site-pick")) {
                    discoverType = DiscoverType.SITE_PICK;
                    break;
                } else if (slug.equals("standard-pick") || slug.equals("image-pick") || slug.equals("quote-pick")) {
                    discoverType = DiscoverType.EDITOR_PICK;
                    break;
                }
            }
        }
    }

    public long getBlogId() {
        return blogId;
    }

    public long getPostId() {
        return postId;
    }

    private String getAuthorName() {
        return StringUtils.notNullStr(authorName);
    }

    private String getAuthorUrl() {
        return StringUtils.notNullStr(authorUrl);
    }

    public String getBlogName() {
        return StringUtils.notNullStr(blogName);
    }

    public String getBlogUrl() {
        return StringUtils.notNullStr(blogUrl);
    }

    public String getAvatarUrl() {
        return StringUtils.notNullStr(avatarUrl);
    }

    public String getPermaLink() {
        return StringUtils.notNullStr(permaLink);
    }

    public boolean hasBlogUrl() {
        return !TextUtils.isEmpty(blogUrl);
    }

    public boolean hasBlogName() {
        return !TextUtils.isEmpty(blogName);
    }

    private boolean hasAuthorName() {
        return !TextUtils.isEmpty(authorName);
    }

    public boolean hasPermalink() {
        return !TextUtils.isEmpty(permaLink);
    }

    public boolean hasAvatarUrl() {
        return !TextUtils.isEmpty(avatarUrl);
    }

    public DiscoverType getDiscoverType() {
        return discoverType;
    }

    /*
     * returns the spanned html for the attribution line
     */
    private transient Spanned attributionHtml;
    public Spanned getAttributionHtml() {
        if (attributionHtml == null) {
            String html;
            String author = "<strong>" + getAuthorName() + "</strong>";
            String blog = "<strong>" + getBlogName() + "</strong>";
            Context context = WordPress.getContext();

            switch (getDiscoverType()) {
                case EDITOR_PICK:
                    if (hasBlogName() && hasAuthorName()) {
                        // "Originally posted by [AuthorName] on [BlogName]"
                        html = String.format(context.getString(R.string.reader_discover_attribution_author_and_blog), author, blog);
                    } else if (hasBlogName()) {
                        // "Originally posted on [BlogName]"
                        html = String.format(context.getString(R.string.reader_discover_attribution_blog), blog);
                    } else if (hasAuthorName()) {
                        // "Originally posted by [AuthorName]"
                        html = String.format(context.getString(R.string.reader_discover_attribution_author), author);
                    } else {
                        return null;
                    }
                    break;

                case SITE_PICK:
                    if (blogId != 0 && hasBlogName()) {
                        // "Visit [BlogName]" - opens blog preview when tapped
                        html = String.format(context.getString(R.string.reader_discover_visit_blog), blog);
                    } else {
                        return null;
                    }
                    break;

                default:
                    return null;
            }

            attributionHtml = Html.fromHtml(html);
        }
        return attributionHtml;
    }
}