Repository navigation
Expand file tree
/
Copy pathMomentItem.java
More file actions
247 lines (218 loc) · 5.75 KB
/
Copy pathMomentItem.java
File metadata and controls
247 lines (218 loc) · 5.75 KB
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
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.demo.client.model;
import java.util.ArrayList;
import java.util.List;
import android.support.annotation.NonNull;
import apijson.demo.client.application.APIJSONApplication;
import apijson.demo.server.model.BaseModel;
public class MomentItem extends BaseModel {
private static final long serialVersionUID = -7437225320551780084L;
public static final int STATUS_NORMAL = 0;
public static final int STATUS_PUBLISHING = 1;
public static final int STATUS_DELETING = 2;
public static final int STATUS_DELETED = 3;
private int status;
private User user;
private Moment moment;
private List<User> userList;//点赞的用户列表
private List<CommentItem> commentItemList;
public MomentItem() {
super();
}
public MomentItem(long id) {
this();
setId(id);
}
@Override
public Long getId() {
return getMomentId();
}
@Override
public MomentItem setId(Long id) {
return setMomentId(id);
}
public Long getUserId() {
return getUser().getId();
}
public int getMyStatus() {
return status;
}
public void setMyStatus(int status) {//status莫名其妙变成状态码status 200
this.status = status;
}
public String getStatusString() {
return getStatusString(getMyStatus());
}
public static String getStatusString(int status) {
switch (status) {
case STATUS_PUBLISHING:
return "正在发布...";
case STATUS_DELETING:
return "正在删除...";
default:
return "删除";
}
}
/**if (user == null) >> user = new User();
* @return
*/
@NonNull
public User getUser() {
if (user == null) {
user = new User(getMoment().getUserId());
}
return user;
}
public MomentItem setUser(User user) {
this.user = user;
return this;
}
public long getMomentId() {
return getMoment().getId();
}
public MomentItem setMomentId(long id) {
getMoment().setId(id);
return this;
}
/**if (moment == null) >> moment = new Moment();
* @return
*/
@NonNull
public Moment getMoment() {
if (moment == null) {
moment = new Moment();
}
return moment;
}
public MomentItem setMoment(Moment moment) {
this.moment = moment;
return this;
}
@NonNull
public List<User> getUserList() {
if (userList == null) {
userList = new ArrayList<User>();
// List<Long> list = getPraiseUserIdList();
// if (list != null) {
// User u;
// for (Long id : list) {
// u = new User(id);
// u.setName("" + id);
// userList.add(u);
// }
// }
}
return userList;
}
public MomentItem setUserList(List<User> userList) {
this.userList = userList;
return this;
}
@NonNull
public List<CommentItem> getCommentItemList() {
if (commentItemList == null) {
commentItemList = new ArrayList<>();
}
return commentItemList;
}
public MomentItem setCommentItemList(List<CommentItem> commentItemList) {
this.commentItemList = commentItemList;
return this;
}
public List<Long> getPraiseUserIdList() {
return getMoment().getPraiseUserIdList();
}
private Boolean isPraised;
public boolean getIsPraised() {
return getIsPraised(APIJSONApplication.getInstance().getCurrentUserId());
}
public boolean getIsPraised(final long userId) {
if (userId <= 0) {
isPraised = false;
} else if (isPraised == null) {
isPraised = isContain(getPraiseUserIdList(), userId);
}
return value(isPraised);
}
public MomentItem setIsPraised(boolean isPraised) {
this.isPraised = isPraised;
User currentUser = APIJSONApplication.getInstance().getCurrentUser();
long userId = currentUser == null ? 0 : currentUser.getId();
List<Long> list = getPraiseUserIdList();
if (list == null) {
list = new ArrayList<>();
}
if (userList == null) {
userList = new ArrayList<User>();
}
if (isPraised == false) {
list.remove(userId);
if (userList.isEmpty() == false) {
User[] users = userList.toArray(new User[]{});
for (User user : users) {
if (user != null && user.getId() == userId) {
userList.remove(user);
break;
}
}
}
} else {
if (list.contains(userId) == false) {
list.add(userId);
userList.add(currentUser);
}
}
getMoment().setPraiseUserIdList(list);
setPraiseCount(praiseCount + (isPraised ? 1 : -1));
return this;
}
private int praiseCount;
public int getPraiseCount() {
return praiseCount;
}
public void setPraiseCount(int praiseCount) {
int idCount = count(getPraiseUserIdList());
if (praiseCount < idCount) {
praiseCount = idCount;
}
this.praiseCount = praiseCount;
}
private Boolean isCommented;
public boolean getIsCommented() {
return getIsCommented(APIJSONApplication.getInstance().getCurrentUserId());
}
public boolean getIsCommented(final long userId) {
if (userId <= 0) {
isCommented = false;
} else if (isCommented == null) {
isCommented = false;
List<CommentItem> commentItemList = getCommentItemList();
if (commentItemList != null) {
for (CommentItem comment : commentItemList) {
if (comment != null && comment.getComment().getUserId() == userId) {
isCommented = true;
break;
}
}
}
}
return value(isCommented);
}
private int commentCount;
public int getCommentCount() {
return commentCount;
}
public MomentItem setCommentCount(int commentCount) {
this.commentCount = commentCount;
return this;
}
}