aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/viewmodel/StatusViewModel.java
blob: e65de8df233aa693b7374746ecfc3b92e648c9f4 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2012-2014 lacolaco.net
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.lacolaco.smileessence.viewmodel;

import android.app.Activity;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import net.lacolaco.smileessence.R;
import net.lacolaco.smileessence.activity.MainActivity;
import net.lacolaco.smileessence.data.FavoriteCache;
import net.lacolaco.smileessence.data.ImageCache;
import net.lacolaco.smileessence.data.UserCache;
import net.lacolaco.smileessence.entity.Account;
import net.lacolaco.smileessence.preference.UserPreferenceHelper;
import net.lacolaco.smileessence.twitter.util.TwitterUtils;
import net.lacolaco.smileessence.util.Morse;
import net.lacolaco.smileessence.util.NameStyles;
import net.lacolaco.smileessence.util.StringUtils;
import net.lacolaco.smileessence.util.Themes;
import net.lacolaco.smileessence.view.dialog.DialogHelper;
import net.lacolaco.smileessence.view.dialog.StatusDetailDialogFragment;
import net.lacolaco.smileessence.view.dialog.UserDetailDialogFragment;
import net.lacolaco.smileessence.view.listener.ListItemClickListener;

import twitter4j.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StatusViewModel implements IViewModel {

    // ------------------------------ FIELDS ------------------------------

    public static final String STATUS_DIALOG = "statusDialog";
    private long id;
    private long userID;
    private String screenName;
    private String name;
    private String iconURL;
    private String text;
    private Date createdAt;
    private String source;
    private boolean isProtected;
    private StatusViewModel retweetedStatus;
    private UserMentionEntity[] mentions;
    private HashtagEntity[] hashtags;
    private MediaEntity[] media;
    private URLEntity[] urls;
    private SymbolEntity[] symbols;
    private boolean isMyStatus;
    private boolean isMention;
    private boolean isRetweetOfMe;

    // --------------------------- CONSTRUCTORS ---------------------------

    public StatusViewModel(Status status, Account account) {
        if (status.isRetweet()) {
            retweetedStatus = new StatusViewModel(status.getRetweetedStatus(), account);
        }
        id = status.getId();
        text = TwitterUtils.replaceURLEntities(status.getText(), status.getURLEntities(), false);
        createdAt = status.getCreatedAt();
        source = status.getSource();
        mentions = status.getUserMentionEntities();
        hashtags = status.getHashtagEntities();
        media = status.getMediaEntities();
        urls = status.getURLEntities();
        symbols = status.getSymbolEntities();
        User user = status.getUser();
        UserCache.getInstance().put(user);
        userID = user.getId();
        screenName = user.getScreenName();
        name = user.getName();
        iconURL = user.getProfileImageURLHttps();
        isProtected = user.isProtected();
        setMention(isMention(account.screenName));
        setMyStatus(isMyStatus(account.userID));
        setRetweetOfMe(isRetweetOfMe(account.userID));
    }

    // --------------------- GETTER / SETTER METHODS ---------------------

    public Date getCreatedAt() {
        if (isRetweet()) {
            return retweetedStatus.createdAt;
        }
        return createdAt;
    }

    private List<Long> getEmbeddedStatusIDs() {
        ArrayList<Long> list = new ArrayList<>();
        for (URLEntity url : urls) {
            Uri uri = Uri.parse(url.getExpandedURL());
            if (uri.getHost().equals("twitter.com")) {
                String[] arr = uri.toString().split("/");
                if (arr[arr.length - 2].equals("status")) {
                    list.add(Long.parseLong(arr[arr.length - 1].split("\\?")[0]));
                }
            }
        }
        return list;
    }

    private String getFooterText() {
        StringBuilder builder = new StringBuilder();
        if (isRetweet()) {
            builder.append("(RT: ").append(this.screenName).append(") ");
        }
        builder.append(StringUtils.dateToString(getCreatedAt()));
        builder.append(" via ");
        builder.append(Html.fromHtml(getSource()));
        return builder.toString();
    }

    public HashtagEntity[] getHashtags() {
        if (isRetweet()) {
            return retweetedStatus.hashtags;
        }
        return hashtags;
    }

    public String getIconURL() {
        if (isRetweet()) {
            return retweetedStatus.iconURL;
        }
        return iconURL;
    }

    public long getID() {
        return id;
    }

    public MediaEntity[] getMedia() {
        if (isRetweet()) {
            return retweetedStatus.media;
        }
        return media;
    }

    public UserMentionEntity[] getMentions() {
        if (isRetweet()) {
            return retweetedStatus.mentions;
        }
        return mentions;
    }

    public String getName() {
        if (isRetweet()) {
            return retweetedStatus.name;
        }
        return name;
    }

    public StatusViewModel getOriginal() {
        return isRetweet() ? retweetedStatus : this;
    }

    public long getOriginalUserID() {
        return isRetweet() ? getRetweetedStatus().getUserID() : getUserID();
    }

    public StatusViewModel getRetweetedStatus() {
        return retweetedStatus;
    }

    public String getScreenName() {
        if (isRetweet()) {
            return retweetedStatus.screenName;
        }
        return screenName;
    }

    public String getSource() {
        if (isRetweet()) {
            return retweetedStatus.source;
        }
        return source;
    }

    public SymbolEntity[] getSymbols() {
        return symbols;
    }

    public String getText() {
        if (isRetweet()) {
            return retweetedStatus.text;
        }
        return text;
    }

    public URLEntity[] getURLs() {
        return urls;
    }

    public long getUserID() {
        return userID;
    }

    public boolean isFavorited() {
        if (isRetweet()) {
            return FavoriteCache.getInstance().get(retweetedStatus.id);
        }
        return FavoriteCache.getInstance().get(id);
    }

    public boolean isMention() {
        if (isRetweet()) {
            return retweetedStatus.isMention;
        }
        return isMention;
    }

    public void setMention(boolean mention) {
        isMention = mention;
    }

    public boolean isMyStatus() {
        if (isRetweet()) {
            return retweetedStatus.isMyStatus;
        }
        return isMyStatus;
    }

    public void setMyStatus(boolean myStatus) {
        isMyStatus = myStatus;
    }

    public boolean isProtected() {
        if (isRetweet()) {
            return retweetedStatus.isProtected;
        }
        return isProtected;
    }

    public boolean isRetweet() {
        return retweetedStatus != null;
    }

    public boolean isRetweetOfMe() {
        return isRetweetOfMe;
    }

    public void setRetweetOfMe(boolean retweet) {
        this.isRetweetOfMe = retweet;
    }

    // ------------------------ INTERFACE METHODS ------------------------


    // --------------------- Interface IViewModel ---------------------

    @Override
    public View getView(final Activity activity, final LayoutInflater inflater, View convertedView) {
        boolean extendStatusURL = new UserPreferenceHelper(activity).getValue(R.string.key_setting_extend_status_url, true);
        return getView(activity, inflater, convertedView, extendStatusURL);
    }

    // -------------------------- OTHER METHODS --------------------------

    public View getView(final Activity activity, final LayoutInflater inflater, View convertedView, boolean extendStatusURL) {
        if (convertedView == null) {
            convertedView = inflater.inflate(R.layout.list_item_status, null);
        }
        UserPreferenceHelper preferenceHelper = new UserPreferenceHelper(activity);
        int textSize = preferenceHelper.getValue(R.string.key_setting_text_size, 10);
        int nameStyle = preferenceHelper.getValue(R.string.key_setting_namestyle, 0);
        int theme = ((MainActivity) activity).getThemeIndex();
        NetworkImageView icon = (NetworkImageView) convertedView.findViewById(R.id.imageview_status_icon);
        ImageCache.getInstance().setImageToView(getIconURL(), icon);
        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onIconClick(activity);
            }
        });
        TextView header = (TextView) convertedView.findViewById(R.id.textview_status_header);
        header.setTextSize(textSize);
        int colorHeader = Themes.getStyledColor(activity, theme, R.attr.color_status_text_header, 0);
        int colorMineHeader = Themes.getStyledColor(activity, theme, R.attr.color_status_text_mine, 0);
        header.setTextColor(isMyStatus() ? colorMineHeader : colorHeader);
        header.setText(NameStyles.getNameString(nameStyle, getScreenName(), getName()));
        TextView content = (TextView) convertedView.findViewById(R.id.textview_status_text);
        content.setTextSize(textSize);
        int colorNormal = Themes.getStyledColor(activity, theme, R.attr.color_status_text_normal, 0);
        content.setTextColor(colorNormal);
        String rawText = getText();
        if (isReadMorseEnabled((MainActivity) activity) && Morse.isMorse(rawText)) {
            content.setText(String.format("%s\n(%s)", rawText, Morse.morseToJa(rawText)));
        } else {
            content.setText(rawText);
        }
        TextView footer = (TextView) convertedView.findViewById(R.id.textview_status_footer);
        footer.setTextSize(textSize - 2);
        int colorFooter = Themes.getStyledColor(activity, theme, R.attr.color_status_text_footer, 0);
        footer.setTextColor(colorFooter);
        footer.setText(getFooterText());
        ImageView favorited = (ImageView) convertedView.findViewById(R.id.imageview_status_favorited);
        favorited.setVisibility(isFavorited() ? View.VISIBLE : View.GONE);
        if (isRetweet()) {
            int colorBgRetweet = Themes.getStyledColor(activity, theme, R.attr.color_status_bg_retweet, 0);
            convertedView.setBackgroundColor(colorBgRetweet);
        } else if (isMention()) {
            int colorBgMention = Themes.getStyledColor(activity, theme, R.attr.color_status_bg_mention, 0);
            convertedView.setBackgroundColor(colorBgMention);
        } else {
            int colorBgNormal = Themes.getStyledColor(activity, theme, R.attr.color_status_bg_normal, 0);
            convertedView.setBackgroundColor(colorBgNormal);
        }
        convertedView.setOnClickListener(new ListItemClickListener(activity, new Runnable() {
            @Override
            public void run() {
                onClick(activity);
            }
        }));
        final ViewGroup embeddedStatus = (ViewGroup) convertedView.findViewById(R.id.view_status_embedded_status);
        embeddedStatus.removeAllViews();
        if (extendStatusURL) {
            if (containsStatusURL()) {
                embeddedStatus.setVisibility(View.VISIBLE);
                final Account account = ((MainActivity) activity).getCurrentAccount();
                List<Long> embeddedStatusIDs = getEmbeddedStatusIDs();
                for (int i = 0; i < embeddedStatusIDs.size(); i++) {
                    long id = embeddedStatusIDs.get(i);
                    final int index = i;
                    final View finalConvertedView = convertedView;
                    TwitterUtils.tryGetStatus(account, id, new TwitterUtils.StatusCallback() {
                        @Override
                        public void success(Status status) {
                            StatusViewModel viewModel = new StatusViewModel(status, account);
                            View embeddedHolder = viewModel.getView(activity, inflater, null, false);
                            embeddedStatus.addView(embeddedHolder);
                            finalConvertedView.invalidate();
                        }

                        @Override
                        public void error() {

                        }
                    });
                }

            }
        } else {
            embeddedStatus.setVisibility(View.GONE);
        }
        return convertedView;
    }

    public boolean isMention(String screenName) {
        if (mentions == null) {
            return false;
        }
        for (UserMentionEntity mention : mentions) {
            if (mention.getScreenName().equals(screenName)) {
                return true;
            }
        }
        return false;
    }

    public boolean isMyStatus(long userID) {
        return this.userID == userID;
    }

    public boolean isRetweetOfMe(long userID) {
        return retweetedStatus != null && retweetedStatus.getUserID() == userID;
    }

    private boolean containsStatusURL() {
        return getEmbeddedStatusIDs().size() > 0;
    }

    private boolean isReadMorseEnabled(MainActivity activity) {
        return activity.getUserPreferenceHelper().getValue(R.string.key_setting_read_morse, true);
    }

    private void onClick(Activity activity) {
        StatusDetailDialogFragment fragment = new StatusDetailDialogFragment();
        fragment.setStatusID(getID());
        DialogHelper.showDialog(activity, fragment, STATUS_DIALOG);
    }

    private void onIconClick(Activity activity) {
        DialogHelper.close(activity, STATUS_DIALOG);
        UserDetailDialogFragment dialogFragment = new UserDetailDialogFragment();
        dialogFragment.setUserID(isRetweet() ? getRetweetedStatus().userID : userID);
        DialogHelper.showDialog(activity, dialogFragment);
    }
}