aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/entity/User.java
blob: abc2c04796a2d532bd89c3e86aad87926e8403f4 (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 net.lacolaco.smileessence.entity;

import net.lacolaco.smileessence.util.UIObservable;

import java.util.HashMap;
import java.util.Map;

public class User extends UIObservable implements IdObject {
    private static Map<Long, User> storage = new HashMap<>();
    private long id;
    private boolean isProtected;
    private String screenName;
    private String name;
    private String profileImageUrl;
    private String profileBannerUrl;
    private String description;
    private String location;
    private String url;
    private int favoritesCount;
    private int statusesCount;
    private int friendsCount;
    private int followersCount;
    private boolean isVerified;

    private User() {
    }

    public synchronized static User fetch(long userId) {
        return storage.get(userId);
    }

    public synchronized static User fromTwitter(final twitter4j.User st) {
        User u = fetch(st.getId());
        if (u == null) {
            u = new User();
            storage.put(st.getId(), u);
        }

        u.update(st);
        return u;
    }

    // only for initialization; DO NOT have reference for this object
    public synchronized static User _makeSkeleton(long id, String screenName) {
        User u = fetch(id);
        if (u != null) {
            return u;
        } else {
            u = new User();
            u.id = id;
            u.screenName = screenName;
            storage.put(id, u);
            return u;
        }
    }

    private void update(twitter4j.User user) {
        id = user.getId();

        if (isProtected() != user.isProtected() ||
                getScreenName() == null || !getScreenName().equals(user.getScreenName()) ||
                getName() == null || !getName().equals(user.getName()) ||
                profileImageUrl == null || !profileImageUrl.equals(user.getProfileImageURLHttps())) {
            isProtected = user.isProtected();
            if (user.getScreenName() != null)
                screenName = user.getScreenName();
            if (user.getName() != null)
                name = user.getName();
            if (user.getProfileImageURLHttps() != null)
                profileImageUrl = user.getProfileImageURLHttps();

            notifyChange(RBinding.BASIC);
        }

        if (getProfileBannerUrl() == null || !getProfileBannerUrl().equals(user.getProfileBannerURL()) ||
                getDescription() == null || !getDescription().equals(user.getDescription()) ||
                getLocation() == null || !getLocation().equals(user.getLocation()) ||
                getUrl() == null || !getUrl().equals(user.getURL()) ||
                getFavoritesCount() != user.getFavouritesCount() ||
                getStatusesCount() != user.getStatusesCount() ||
                getFriendsCount() != user.getFriendsCount() ||
                getFollowersCount() != user.getFollowersCount()) {
            isVerified = user.isVerified();
            if (user.getProfileBannerURL() != null)
                profileBannerUrl = user.getProfileBannerURL();
            if (user.getDescription() != null)
                description = user.getDescription();
            if (user.getLocation() != null)
                location = user.getLocation();
            if (user.getURL() != null)
                url = user.getURL();
            if (user.getFavouritesCount() != -1)
                favoritesCount = user.getFavouritesCount();
            if (user.getStatusesCount() != -1)
                statusesCount = user.getStatusesCount();
            if (user.getFriendsCount() != -1)
                friendsCount = user.getFriendsCount();
            if (user.getFollowersCount() != -1)
                followersCount = user.getFollowersCount();

            notifyChange(RBinding.DETAIL);
        }
    }

    @Override
    public long getId() {
        return id;
    }

    public boolean isProtected() {
        return isProtected;
    }

    public String getScreenName() {
        return screenName;
    }

    public String getName() {
        return name;
    }

    public String getProfileImageUrlOriginal() {
        String original = profileImageUrl;
        if (original != null) {
            String url = original.substring(0, original.lastIndexOf("_"));
            int extIndex = original.lastIndexOf(".");
            if (extIndex > original.lastIndexOf("/")) {
                url += original.substring(extIndex);
            }
            return url;
        }
        return null;
    }

    public String getProfileBannerUrl() {
        return profileBannerUrl;
    }

    public String getDescription() {
        return description;
    }

    public String getLocation() {
        return location;
    }

    public String getUrl() {
        return url;
    }

    public int getFavoritesCount() {
        return favoritesCount;
    }

    public int getStatusesCount() {
        return statusesCount;
    }

    public int getFriendsCount() {
        return friendsCount;
    }

    public int getFollowersCount() {
        return followersCount;
    }

    public boolean isVerified() {
        return isVerified;
    }

    // helper methods
    public String getUserHomeURL() {
        return String.format("https://twitter.com/%s", getScreenName());
    }

    public String getAclogTimelineURL() {
        return String.format("https://aclog.rhe.jp/%s/timeline", getScreenName());
    }

    public String getFormattedName() {
        return screenName + " / " + name;
    }
}