aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/miz_hi/smileessence/model/status/user/UserModel.java
blob: 16e90ccd59175da2546ca9113064f4b70e18f329 (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
package net.miz_hi.smileessence.model.status.user;

import net.miz_hi.smileessence.Client;
import net.miz_hi.smileessence.cache.IconCache;
import net.miz_hi.smileessence.cache.RelationshipCache;
import net.miz_hi.smileessence.model.status.IStatusModel;
import net.miz_hi.smileessence.preference.EnumPreferenceKey;
import net.miz_hi.smileessence.status.EnumNameStyle;
import net.miz_hi.smileessence.task.impl.GetRelationshipTask;
import net.miz_hi.smileessence.task.impl.GetUserTask;
import twitter4j.Relationship;
import twitter4j.User;

import java.util.Date;
import java.util.concurrent.Future;

public class UserModel implements IStatusModel
{

    public long userId;
    public String screenName;
    public String name;
    public String homePageUrl;
    public String location;
    public String description;
    public String iconUrl;
    public int statusCount;
    public int friendCount;
    public int followerCount;
    public int favoriteCount;
    public Date createdAt;
    public boolean isProtected;


    private UserModel()
    {
    }

    public UserModel(User user)
    {
        userId = user.getId();
        updateData(user);
    }

    public UserModel updateData(User user)
    {
        screenName = user.getScreenName();
        name = user.getName();
        homePageUrl = user.getURL();
        location = user.getLocation();
        description = user.getDescription();
        iconUrl = user.getProfileImageURL();
        statusCount = user.getStatusesCount();
        friendCount = user.getFriendsCount();
        followerCount = user.getFollowersCount();
        favoriteCount = user.getFavouritesCount();
        createdAt = user.getCreatedAt();
        isProtected = user.isProtected();
        IconCache.checkIconCache(UserModel.this);
        return this;
    }

    public User getRawUser()
    {
        Future<User> resp = new GetUserTask(userId).callAsync();
        try
        {
            return resp.get();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public boolean isMe()
    {
        return userId == Client.getMainAccount().getUserId();
    }

    private Relationship getRelationship(boolean force)
    {
        if (force)
        {
            Future<Boolean> b = new GetRelationshipTask(userId).callAsync();
            try
            {
                b.get();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return null;
            }
        }
        return RelationshipCache.get(userId);
    }

    public boolean isFriend(boolean force)
    {
        Relationship rel = getRelationship(force);
        if (rel == null)
        {
            if (force)
            {
                return false;
            }
            return isFriend(true);
        }
        return rel.isSourceFollowingTarget();
    }

    public boolean isFollower(boolean force)
    {
        Relationship rel = getRelationship(force);
        if (rel == null)
        {
            if (force)
            {
                return false;
            }
            return isFriend(true);
        }
        return rel.isSourceFollowedByTarget();
    }

    public static UserModel getNullUserModel()
    {
        UserModel user = new UserModel();
        user.screenName = "";
        user.name = "";
        user.homePageUrl = "";
        user.location = "";
        user.description = "";
        user.iconUrl = "";
        user.statusCount = 0;
        user.friendCount = 0;
        user.followerCount = 0;
        user.favoriteCount = 0;
        user.createdAt = new Date();
        user.isProtected = false;
        return user;
    }

    @Override
    public UserModel getUser()
    {
        return this;
    }

    @Override
    public String getTextTop()
    {
        StringBuilder builder = new StringBuilder();
        String style = Client.getPreferenceValue(EnumPreferenceKey.NAME_STYLE);
        if (style.equals(EnumNameStyle.S_N.get()) || style.equals(EnumNameStyle.S.get()))
        {
            builder.append(screenName);
        }
        else if (style.equals(EnumNameStyle.N_S.get()) || style.equals(EnumNameStyle.N.get()))
        {
            builder.append(name);
        }
        if (style.equals(EnumNameStyle.S_N.get()))
        {
            builder.append(" / ");
            builder.append(name);
        }
        else if (style.equals(EnumNameStyle.N_S.get()))
        {
            builder.append(" / ");
            builder.append(screenName);
        }
        return builder.toString();
    }

    @Override
    public String getTextContent()
    {
        if (description.length() > 100)
        {
            return description.substring(0, 100) + "...";
        }
        else
        {
            return description;
        }
    }

    @Override
    public String getTextBottom()
    {
        return location;
    }
}