@@ -17,14 +17,46 @@ - (instancetype)init
1717 _propertyArrayMap = [NSDictionary dictionaryWithObjectsAndKeys:
1818 @" PrivateMessage" , @" list" , nil ];
1919 _canLoadMore = YES ;
20- _isLoading = _willLoadMore = NO ;
20+ _isLoading = _willLoadMore = _isPolling = NO ;
2121 _page = [NSNumber numberWithInteger: 1 ];
22- _pageSize = [NSNumber numberWithInteger: 30 ];
22+ _pageSize = [NSNumber numberWithInteger: 10 ];
2323 _curFriend = nil ;
2424 }
2525 return self;
2626}
2727
28+ - (NSMutableArray *)list {
29+ if (!_list) {
30+ _list = [[NSMutableArray alloc ] init ];
31+ }
32+ return _list;
33+ }
34+
35+ - (NSMutableArray *)nextMessages {
36+ if (!_nextMessages) {
37+ _nextMessages = [[NSMutableArray alloc ] init ];
38+ }
39+ return _nextMessages;
40+ }
41+
42+ - (NSMutableArray *)dataList {
43+ if (!_dataList) {
44+ _dataList = [[NSMutableArray alloc ] init ];
45+ }
46+ return _dataList;
47+ }
48+
49+ - (NSMutableArray *)reset_dataList {
50+ [self .dataList removeAllObjects ];
51+ if (_list.count > 0 ) {
52+ self.dataList = [_list mutableCopy ];
53+ }
54+ if (_nextMessages.count > 0 ) {
55+ [self .dataList addObjectsFromArray: _nextMessages];
56+ }
57+ return _dataList;
58+ }
59+
2860+ (PrivateMessages *)priMsgsWithUser : (User *)user {
2961 PrivateMessages *priMsgs = [[PrivateMessages alloc ] init ];
3062 priMsgs.curFriend = user;
@@ -53,66 +85,91 @@ - (NSDictionary *)toParams{
5385 return @{@" page" : _willLoadMore? [NSNumber numberWithInt: _page.intValue +1 ]: [NSNumber numberWithInt: 1 ],
5486 @" pageSize" : _pageSize};
5587}
88+
89+ - (NSString *)toPollPath {
90+ return [NSString stringWithFormat: @" api/message/conversations/%@ /last" , _curFriend.global_key];
91+ }
92+ - (NSDictionary *)toPollParams {
93+
94+ return @{@" id" : [NSNumber numberWithInteger: [self p_lastId ]]};
95+ }
96+
97+ - (NSInteger )p_lastId {
98+ NSInteger last_id;
99+ if (!_list || _list.count <= 0 ) {
100+ last_id = 0 ;
101+ }else {
102+ PrivateMessage *last_Msg = [_list firstObject ];
103+ last_id = last_Msg.id .integerValue ;
104+ }
105+ return last_id;
106+ }
107+
56108- (void )configWithObj : (PrivateMessages *)priMsgs {
57109 self.page = priMsgs.page ;
58110 self.pageSize = priMsgs.pageSize ;
59111 self.totalPage = priMsgs.totalPage ;
60- if (_willLoadMore) {
61- [self .list addObjectsFromArray: priMsgs.list];
62- }else {
63- self.list = [NSMutableArray arrayWithArray: priMsgs.list];
112+ if (!_willLoadMore) {
113+ [self .list removeAllObjects ];
64114 }
115+ [self .list addObjectsFromArray: priMsgs.list];
116+ [self reset_dataList ];
117+
65118 _canLoadMore = _page.intValue < _totalPage.intValue ;
66119}
67120
68- - (void )sendNewMessage : (PrivateMessage *)nextMsg {
69- if (!nextMsg ) {
121+ - (void )configWithPollArray : ( NSArray *)pollList {
122+ if (pollList. count <= 0 ) {
70123 return ;
71124 }
72- if (!_nextMessages) {
73- _nextMessages = [[NSMutableArray alloc ] initWithCapacity: 1 ];
125+ NSInteger last_id = [self p_lastId ];
126+ __block NSInteger bridge_index;
127+ __block BOOL hasNewData = NO ;
128+ [pollList enumerateObjectsWithOptions: NSEnumerationReverse usingBlock: ^(PrivateMessage *obj, NSUInteger idx, BOOL *stop) {
129+ if (obj.id .integerValue > last_id) {
130+ hasNewData = YES ;
131+ bridge_index = idx;
132+ *stop = YES ;
133+ }
134+ }];
135+ if (hasNewData) {
136+ NSRange freshDataRange = NSMakeRange (0 , bridge_index +1 );
137+ NSArray *freshDataList = [pollList subarrayWithRange: freshDataRange];
138+ [self .list insertObjects: freshDataList atIndexes: [NSIndexSet indexSetWithIndexesInRange: freshDataRange]];
139+ [self reset_dataList ];
74140 }
75- if (!_list) {
76- _list = [[NSMutableArray alloc ] initWithCapacity: 1 ];
141+ }
142+
143+ - (void )sendNewMessage : (PrivateMessage *)nextMsg {
144+ [self p_addObj: nextMsg toArray: self .nextMessages];
145+ [self reset_dataList ];
146+ }
147+ - (void )p_addObj : (id )anObj toArray : (NSMutableArray *)list {
148+ if (!anObj || !list) {
149+ return ;
77150 }
78- NSUInteger index = [_nextMessages indexOfObject: nextMsg ];
151+ NSUInteger index = [list indexOfObject: anObj ];
79152 if (index == NSNotFound ) {
80- [_nextMessages insertObject: nextMsg atIndex: 0 ];
81- [_list insertObject: nextMsg atIndex: 0 ];
82- }else {
83- if (index != 0 ) {
84- [_nextMessages exchangeObjectAtIndex: index withObjectAtIndex: 0 ];
85- }
86- NSUInteger indexInList = [_list indexOfObject: nextMsg];
87- if (indexInList && indexInList!= 0 ) {
88- [_list exchangeObjectAtIndex: indexInList withObjectAtIndex: 0 ];
89- }
153+ [list insertObject: anObj atIndex: 0 ];
154+ }else if (index != 0 ){
155+ [list exchangeObjectAtIndex: index withObjectAtIndex: 0 ];
90156 }
91157}
158+
92159- (void )sendSuccessMessage : (PrivateMessage *)sucessMsg andOldMessage : (PrivateMessage *)oldMsg {
93- if (_nextMessages) {
94- [_nextMessages removeObject: oldMsg];
95- }
96- NSUInteger index = [_list indexOfObject: oldMsg];
97- if (index != NSNotFound ) {
98- [_list replaceObjectAtIndex: index withObject: sucessMsg];
160+ if (!sucessMsg || !oldMsg) {
161+ DebugLog (@" sucessMsg and oldMsg should not be nil" );
162+ return ;
99163 }
164+ [self .nextMessages removeObject: oldMsg];
165+ [self .list insertObject: sucessMsg atIndex: 0 ];
166+ [self reset_dataList ];
100167}
101168- (void )deleteMessage : (PrivateMessage *)msg {
102- [_list removeObject: msg];
169+ [self .list removeObject: msg];
170+ [self reset_dataList ];
103171}
104172@end
105173
106174
107175
108-
109-
110-
111-
112-
113-
114-
115-
116-
117-
118-
0 commit comments