-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathPrivateMessages.m
executable file
·243 lines (218 loc) · 6.58 KB
/
PrivateMessages.m
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
//
// PrivateMessages.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-29.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "PrivateMessages.h"
#include "Login.h"
@interface PrivateMessages ()
@property (strong, nonatomic) NSNumber *p_lastId;
@end
@implementation PrivateMessages
- (instancetype)init
{
self = [super init];
if (self) {
_propertyArrayMap = [NSDictionary dictionaryWithObjectsAndKeys:
@"PrivateMessage", @"list", nil];
_canLoadMore = YES;
_isLoading = _willLoadMore = _isPolling = NO;
_page = [NSNumber numberWithInteger:1];
_pageSize = [NSNumber numberWithInteger:20];
_curFriend = nil;
}
return self;
}
- (NSMutableArray *)list{
if (!_list) {
_list = [[NSMutableArray alloc] init];
}
return _list;
}
- (NSMutableArray *)nextMessages{
if (!_nextMessages) {
_nextMessages = [[NSMutableArray alloc] init];
}
return _nextMessages;
}
- (NSMutableArray *)dataList{
if (!_dataList) {
_dataList = [[NSMutableArray alloc] init];
}
return _dataList;
}
- (NSMutableArray *)reset_dataList{
[self.dataList removeAllObjects];
if (_list.count > 0) {
self.dataList = [_list mutableCopy];
}
if (_nextMessages.count > 0) {
[self.dataList insertObjects:_nextMessages atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, _nextMessages.count)]];
}
return _dataList;
}
+ (PrivateMessages *)priMsgsWithUser:(User *)user{
PrivateMessages *priMsgs = [[PrivateMessages alloc] init];
priMsgs.curFriend = user;
return priMsgs;
}
+ (id)analyzeResponseData:(NSDictionary *)responseData{
id data = [responseData valueForKeyPath:@"data"];
if (!data) {//旧数据直接保存的data属性
data = responseData;
}
id resultA = nil;
if ([data isKindOfClass:[NSArray class]]) {
resultA = [NSObject arrayFromJSON:data ofObjects:@"PrivateMessage"];
}else if (data){
resultA = [NSObject objectOfClass:@"PrivateMessages" fromJSON:data];
}
return resultA;
}
- (NSString *)localPrivateMessagesPath{
NSString *path;
if (_curFriend) {
path = [NSString stringWithFormat:@"conversations_%@", _curFriend.global_key];
}else{
path = @"conversations";
}
return path;
}
- (NSString *)toPath{
NSString *path;
if (_curFriend) {
path = [NSString stringWithFormat:@"api/message/conversations/%@/prev", _curFriend.global_key];
}else{
path = @"api/message/conversations";
}
return path;
}
- (NSDictionary *)toParams{
NSDictionary *params = nil;
if (_curFriend) {
NSNumber *prevId = kDefaultLastId;
if (_willLoadMore && _list.count > 0) {
PrivateMessage *prev_Msg = [_list lastObject];
prevId = prev_Msg.id;
}
params = @{@"id" : prevId,
@"pageSize" : _pageSize};
}else{
params = @{@"page" : _willLoadMore? [NSNumber numberWithInt:_page.intValue +1]: [NSNumber numberWithInt:1],
@"pageSize" : _pageSize};
}
return params;
}
- (NSString *)toPollPath{
return [NSString stringWithFormat:@"api/message/conversations/%@/last", _curFriend.global_key];
}
- (NSDictionary *)toPollParams{
return @{@"id" : self.p_lastId};
}
- (void)freshLastId:(NSNumber *)last_id{
self.p_lastId = last_id;
}
- (NSNumber *)p_lastId{
if (!_p_lastId) {
_p_lastId = @0;
[_list enumerateObjectsUsingBlock:^(PrivateMessage *obj, NSUInteger idx, BOOL *stop) {
if (obj.id.integerValue > 0) {
_p_lastId = obj.id;
*stop = YES;
}
}];
}
return _p_lastId;
}
- (BOOL)p_addMsg:(PrivateMessage *)aMsg{
NSInteger curId = aMsg.id.integerValue;
__block NSInteger add_index;
__block BOOL needToAdd = NO;
if (self.list.count > 0) {
[self.list enumerateObjectsUsingBlock:^(PrivateMessage *obj, NSUInteger idx, BOOL *stop) {
if (curId == obj.id.integerValue) {
needToAdd = NO;
*stop = YES;
}else if (curId > obj.id.integerValue){
needToAdd = YES;
add_index = idx;
*stop = YES;
}
}];
if (needToAdd) {
[self.list insertObject:aMsg atIndex:add_index];
}
}else{
needToAdd = YES;
[self.list addObject:aMsg];
}
return needToAdd;
}
- (void)configWithObj:(id)anObj{
if ([anObj isKindOfClass:[PrivateMessages class]]) {
PrivateMessages *priMsgs = (PrivateMessages *)anObj;
self.page = priMsgs.page;
self.pageSize = priMsgs.pageSize;
self.totalPage = priMsgs.totalPage;
if (!_willLoadMore) {
[self.list removeAllObjects];
}
[self.list addObjectsFromArray:priMsgs.list];
self.canLoadMore = _page.intValue < _totalPage.intValue;
}else if ([anObj isKindOfClass:[NSArray class]]){
NSArray *list = (NSArray *)anObj;
if (!_willLoadMore) {
[self.list removeAllObjects];
}
[self.list addObjectsFromArray:list];
self.canLoadMore = list.count > 0;
}
[self reset_dataList];
}
- (void)configWithPollArray:(NSArray *)pollList{
if (pollList.count <= 0) {
return;
}
__block BOOL hasNewData = NO;
__weak typeof(self) weakSelf = self;
[pollList enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(PrivateMessage *obj, NSUInteger idx, BOOL *stop) {
if ([weakSelf p_addMsg:obj]) {
hasNewData = YES;
}
}];
if (hasNewData) {
[self reset_dataList];
}
}
- (void)sendNewMessage:(PrivateMessage *)nextMsg{
[self p_addObj:nextMsg toArray:self.nextMessages];
[self reset_dataList];
}
- (void)p_addObj:(id)anObj toArray:(NSMutableArray *)list{
if (!anObj || !list) {
return;
}
NSUInteger index = [list indexOfObject:anObj];
if (index == NSNotFound) {
[list insertObject:anObj atIndex:0];
}else if (index != 0){
[list exchangeObjectAtIndex:index withObjectAtIndex:0];
}
}
- (void)sendSuccessMessage:(PrivateMessage *)sucessMsg andOldMessage:(PrivateMessage *)oldMsg{
if (!sucessMsg || !oldMsg) {
DebugLog(@"sucessMsg and oldMsg should not be nil");
return;
}
[self.nextMessages removeObject:oldMsg];
[self p_addMsg:sucessMsg];
[self reset_dataList];
}
- (void)deleteMessage:(PrivateMessage *)msg{
[self.list removeObject:msg];
[self.nextMessages removeObject:msg];
[self reset_dataList];
}
@end