aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/models/PostsListPost.java
blob: b9ddd7e2641affc1522b4a8c990ef63d332529d9 (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
package org.wordpress.android.models;

import android.text.TextUtils;
import android.text.format.DateUtils;

import org.wordpress.android.WordPress;
import org.wordpress.android.ui.posts.services.PostUploadService;
import org.wordpress.android.util.DateTimeUtils;
import org.wordpress.android.util.HtmlUtils;
import org.wordpress.android.util.StringUtils;

import java.text.BreakIterator;
import java.util.Date;

/**
 * Barebones post/page as listed in PostsListFragment
 */
public class PostsListPost {
    private static final int MAX_EXCERPT_LEN = 150;

    private final long postId;
    private final long blogId;
    private long dateCreatedGmt;
    private final long featuredImageId;

    private final String title;
    private String excerpt;
    private final String description;
    private final String status;

    private final boolean isLocalDraft;
    private final boolean hasLocalChanges;
    private final boolean isUploading;

    // featuredImageUrl is generated by the adapter on the fly
    private transient String featuredImageUrl;

    public PostsListPost(Post post) {
        postId = post.getLocalTablePostId();
        blogId = post.getLocalTableBlogId();
        featuredImageId = post.getFeaturedImageId();

        title = post.getTitle();
        description = post.getDescription();
        excerpt = post.getPostExcerpt();

        status = post.getPostStatus();
        isLocalDraft = post.isLocalDraft();
        hasLocalChanges = post.isLocalChange();
        isUploading = PostUploadService.isPostUploading(postId);

        setDateCreatedGmt(post.getDate_created_gmt());

        // if the post doesn't have an excerpt, generate one from the description
        if (!hasExcerpt() && hasDescription()) {
            excerpt = makeExcerpt(description);
        }
    }

    public long getPostId() {
        return postId;
    }

    public long getBlogId() {
        return blogId;
    }

    public String getTitle() {
        return StringUtils.notNullStr(title);
    }
    public boolean hasTitle() {
        return !TextUtils.isEmpty(title);
    }

    public String getDescription() {
        return StringUtils.notNullStr(description);
    }
    public boolean hasDescription() {
        return !TextUtils.isEmpty(description);
    }

    public String getExcerpt() {
        return StringUtils.notNullStr(excerpt);
    }
    public boolean hasExcerpt() {
        return !TextUtils.isEmpty(excerpt);
    }

    /*
     * java's string.trim() doesn't handle non-breaking space chars (#160), which may appear at the
     * end of post content - work around this by converting them to standard spaces before trimming
     */
    private static final String NBSP = String.valueOf((char) 160);
    private static String trimEx(final String s) {
        return s.replace(NBSP, " ").trim();
    }

    private static String makeExcerpt(String description) {
        if (TextUtils.isEmpty(description)) {
            return null;
        }

        String s = HtmlUtils.fastStripHtml(description);
        if (s.length() < MAX_EXCERPT_LEN) {
            return trimEx(s);
        }

        StringBuilder result = new StringBuilder();
        BreakIterator wordIterator = BreakIterator.getWordInstance();
        wordIterator.setText(s);
        int start = wordIterator.first();
        int end = wordIterator.next();
        int totalLen = 0;
        while (end != BreakIterator.DONE) {
            String word = s.substring(start, end);
            result.append(word);
            totalLen += word.length();
            if (totalLen >= MAX_EXCERPT_LEN) {
                break;
            }
            start = end;
            end = wordIterator.next();
        }

        if (totalLen == 0) {
            return null;
        }
        return trimEx(result.toString()) + "...";
    }

    public long getFeaturedImageId() {
        return featuredImageId;
    }
    public boolean hasFeaturedImageId() {
        return featuredImageId != 0;
    }

    public String getFeaturedImageUrl() {
        return StringUtils.notNullStr(featuredImageUrl);
    }
    public void setFeaturedImageUrl(String url) {
        this.featuredImageUrl = StringUtils.notNullStr(url);
    }
    public boolean hasFeaturedImageUrl() {
        return !TextUtils.isEmpty(featuredImageUrl);
    }

    public long getDateCreatedGmt() {
        return dateCreatedGmt;
    }
    private void setDateCreatedGmt(long dateCreatedGmt) {
        this.dateCreatedGmt = dateCreatedGmt;
    }

    public String getOriginalStatus() {
        return StringUtils.notNullStr(status);
    }

    public PostStatus getStatusEnum() {
        return PostStatus.fromPostsListPost(this);
    }

    public String getFormattedDate() {
        if (getStatusEnum() == PostStatus.SCHEDULED) {
            return DateUtils.formatDateTime(WordPress.getContext(), dateCreatedGmt, DateUtils.FORMAT_ABBREV_ALL);
        } else {
            return DateTimeUtils.javaDateToTimeSpan(new Date(dateCreatedGmt), WordPress.getContext());
        }
    }

    public boolean isLocalDraft() {
        return isLocalDraft;
    }

    public boolean hasLocalChanges() {
        return hasLocalChanges;
    }

    public boolean isUploading() {
        return isUploading;
    }

}