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

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import net.lacolaco.smileessence.BR;

public class User extends BaseObservable {
    // 重複防止用キャッシュ こっちは weak reference
    private static Cache<Long, User> storage = CacheBuilder.newBuilder().weakValues().build();

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

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

    // インスタンス
    private long id;
    private boolean tweetProtected;
    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(twitter4j.User st) {
        update(st);
    }

    private void update(twitter4j.User user) {
        id = user.getId();
        if (isTweetProtected() != user.isProtected()) {
            tweetProtected = user.isProtected();
            notifyPropertyChanged(BR.tweetProtected);
        }
        if (!getScreenName().equals(user.getScreenName())) {
            screenName = user.getScreenName();
            notifyPropertyChanged(BR.screenName);
        }
        if (!getName().equals(user.getName())) {
            name = user.getName();
            notifyPropertyChanged(BR.name);
        }
        if (!getProfileImageUrl().equals(user.getProfileBackgroundImageUrlHttps())) {
            profileImageUrl = user.getProfileImageURLHttps();
            notifyPropertyChanged(BR.profileImageUrl);
        }
        if (!getProfileBannerUrl().equals(user.getProfileBannerURL())) {
            profileBannerUrl = user.getProfileBannerURL();
            notifyPropertyChanged(BR.profileBannerUrl);
        }
        if (!getDescription().equals(user.getDescription())) {
            description = user.getDescription();
            notifyPropertyChanged(BR.description);
        }
        if (!getLocation().equals(user.getLocation())) {
            location = user.getLocation();
            notifyPropertyChanged(BR.location);
        }
        if (!getUrl().equals(user.getURL())) {
            url = user.getURL();
            notifyPropertyChanged(BR.url);
        }
        if (getFavoritesCount() != user.getFavouritesCount()) {
            favoritesCount = user.getFavouritesCount();
            notifyPropertyChanged(BR.favoritesCount);
        }
        if (getStatusesCount() != user.getStatusesCount()) {
            statusesCount = user.getStatusesCount();
            notifyPropertyChanged(BR.statusesCount);
        }
        if (getFriendsCount() != user.getFriendsCount()) {
            friendsCount = user.getFriendsCount();
            notifyPropertyChanged(BR.friendsCount);
        }
        if (getFollowersCount() != user.getFollowersCount()) {
            followersCount = user.getFollowersCount();
            notifyPropertyChanged(BR.followersCount);
        }
        if (isVerified() != user.isVerified()) {
            isVerified = user.isVerified();
            notifyPropertyChanged(BR.verified);
        }
    }

    public long getId() {
        return id;
    }

    @Bindable // TODO: workaround for bugs in com.android.databinding
    public boolean isTweetProtected() {
        return tweetProtected;
    }

    @Bindable
    public String getScreenName() {
        return screenName;
    }

    @Bindable
    public String getName() {
        return name;
    }

    @Bindable
    public String getProfileImageUrl() {
        return profileImageUrl;
    }

    public String getProfileImageUrlOriginal() {
        return getProfileImageUrlWithSuffix("");
    }

    private String getProfileImageUrlWithSuffix(String suffix) {
        String original = getProfileImageUrl();
        if (original != null) {
            String url = original.substring(0, original.lastIndexOf("_")) + suffix;
            int extIndex = original.lastIndexOf(".");
            if (extIndex > original.lastIndexOf("/")) {
                url += original.substring(extIndex);
            }
            return url;
        }
        return null;
    }

    @Bindable
    public String getProfileBannerUrl() {
        return profileBannerUrl;
    }

    @Bindable
    public String getDescription() {
        return description;
    }

    @Bindable
    public String getLocation() {
        return location;
    }

    @Bindable
    public String getUrl() {
        return url;
    }

    @Bindable
    public int getFavoritesCount() {
        return favoritesCount;
    }

    @Bindable
    public int getStatusesCount() {
        return statusesCount;
    }

    @Bindable
    public int getFriendsCount() {
        return friendsCount;
    }

    @Bindable
    public int getFollowersCount() {
        return followersCount;
    }

    @Bindable
    public boolean isVerified() {
        return isVerified;
    }


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

    public String getAclogTimelineURL() {
        return String.format("http://aclog.koba789.com/%s/timeline", getScreenName());
    }

    public String getFavstarRecentURL() {
        return String.format("http://favstar.fm/users/%s/recent", getScreenName());
    }

    public String getTwilogURL() {
        return String.format("http://twilog.org/%s", getScreenName());
    }
}