diff options
author | Chris Warrington <cmw@google.com> | 2016-10-18 12:29:21 +0100 |
---|---|---|
committer | Chris Warrington <cmw@google.com> | 2016-10-18 12:34:18 +0100 |
commit | e3780081075c01aa1dff6d1f373cb43192b33e68 (patch) | |
tree | fb734615933a39f3d009210dc0d1457160479b35 /WordPress/src/main/java/org/wordpress/android/models/ReaderPostDiscoverData.java | |
parent | 7e05eb7e57827eddc885570bc00aed8a50320dbf (diff) | |
parent | 025b8b226c8d8edba2b309ca878572f40512eca7 (diff) | |
download | gradle-perf-android-medium-e3780081075c01aa1dff6d1f373cb43192b33e68.tar.gz |
Merge remote-tracking branch 'origin/upstream-master' into masterHEADstudio-3.4.0studio-3.2.1studio-3.1.2studio-3.0studio-2.3gradle_3.4.0gradle_3.1.2gradle_3.0.0gradle_2.3.0studio-master-devmirror-goog-studio-master-devmastermain
Change-Id: I63f5e16d09297c48432192761b840310935eb903
Diffstat (limited to 'WordPress/src/main/java/org/wordpress/android/models/ReaderPostDiscoverData.java')
-rw-r--r-- | WordPress/src/main/java/org/wordpress/android/models/ReaderPostDiscoverData.java | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/WordPress/src/main/java/org/wordpress/android/models/ReaderPostDiscoverData.java b/WordPress/src/main/java/org/wordpress/android/models/ReaderPostDiscoverData.java new file mode 100644 index 000000000..b525708a7 --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/models/ReaderPostDiscoverData.java @@ -0,0 +1,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; + } +} |