Skip to content

Commit a884da2

Browse files
committed
poll
1 parent c348656 commit a884da2

14 files changed

Lines changed: 256 additions & 67 deletions

Coding_iOS.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
4E9113AC1A1CB19900AC9431 /* icon_file_state_pause@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 4E9113A81A1CB19900AC9431 /* icon_file_state_pause@2x.png */; };
141141
4E923E781A2F040D00427514 /* messageLeft_bg_img@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 4E923E761A2F040D00427514 /* messageLeft_bg_img@2x.png */; };
142142
4E923E791A2F040D00427514 /* messageRight_bg_img@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 4E923E771A2F040D00427514 /* messageRight_bg_img@2x.png */; };
143+
4E969CD01AF0EB87005C0CCE /* NSTimer+Common.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E969CCF1AF0EB87005C0CCE /* NSTimer+Common.m */; };
143144
4E96E7BB1A1B25D40037C098 /* DirectoryWatcher.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E96E7BA1A1B25D40037C098 /* DirectoryWatcher.m */; };
144145
4E96E7C81A1B46740037C098 /* QuickLook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4E96E7C71A1B46740037C098 /* QuickLook.framework */; };
145146
4E96E7D81A1B537E0037C098 /* icon_file_ai@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 4E96E7CA1A1B537E0037C098 /* icon_file_ai@2x.png */; };
@@ -817,6 +818,8 @@
817818
4E9113A81A1CB19900AC9431 /* icon_file_state_pause@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "icon_file_state_pause@2x.png"; sourceTree = "<group>"; };
818819
4E923E761A2F040D00427514 /* messageLeft_bg_img@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "messageLeft_bg_img@2x.png"; sourceTree = "<group>"; };
819820
4E923E771A2F040D00427514 /* messageRight_bg_img@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "messageRight_bg_img@2x.png"; sourceTree = "<group>"; };
821+
4E969CCE1AF0EB87005C0CCE /* NSTimer+Common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSTimer+Common.h"; sourceTree = "<group>"; };
822+
4E969CCF1AF0EB87005C0CCE /* NSTimer+Common.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSTimer+Common.m"; sourceTree = "<group>"; };
820823
4E96E7B91A1B25D40037C098 /* DirectoryWatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DirectoryWatcher.h; sourceTree = "<group>"; };
821824
4E96E7BA1A1B25D40037C098 /* DirectoryWatcher.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DirectoryWatcher.m; sourceTree = "<group>"; };
822825
4E96E7C71A1B46740037C098 /* QuickLook.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuickLook.framework; path = System/Library/Frameworks/QuickLook.framework; sourceTree = SDKROOT; };
@@ -1996,6 +1999,8 @@
19961999
4EB0C1FF1A807ED00042FC4F /* NSURL+Common.m */,
19972000
4EE5481C1AE7B7CA00A92306 /* UISearchBar+Common.h */,
19982001
4EE5481D1AE7B7CA00A92306 /* UISearchBar+Common.m */,
2002+
4E969CCE1AF0EB87005C0CCE /* NSTimer+Common.h */,
2003+
4E969CCF1AF0EB87005C0CCE /* NSTimer+Common.m */,
19992004
);
20002005
path = OC_Category;
20012006
sourceTree = "<group>";
@@ -3804,6 +3809,7 @@
38043809
4E8765651A22E5B40090CFB9 /* NSMutableArray+SWUtilityButtons.m in Sources */,
38053810
8ECA8BB419FB42CC00C598C6 /* UILongPressMenuImageView.m in Sources */,
38063811
8EA6D1BE19E240C40076D59C /* Register.m in Sources */,
3812+
4E969CD01AF0EB87005C0CCE /* NSTimer+Common.m in Sources */,
38073813
8EF643B019FF7E2900F7EEB0 /* CodingTipCell.m in Sources */,
38083814
8EA6D1BA19E240C40076D59C /* Projects.m in Sources */,
38093815
4E996BDF1ABA7CE100C704F1 /* UserInfoDetailUserCell.m in Sources */,

Coding_iOS/Controllers/ConversationViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313

1414
@interface ConversationViewController : BaseViewController<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, QBImagePickerControllerDelegate, UIMessageInputViewDelegate>
1515
@property (strong, nonatomic) PrivateMessages *myPriMsgs;
16-
- (void)refreshLoadMore:(BOOL)willLoadMore;
16+
- (void)doPoll;
1717
@end

Coding_iOS/Controllers/ConversationViewController.m

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@
1515
#import "UsersViewController.h"
1616
#import "Helper.h"
1717
#import "WebViewController.h"
18+
#import "NSTimer+Common.h"
1819

1920
@interface ConversationViewController ()<TTTAttributedLabelDelegate>
2021
@property (nonatomic, strong) UITableView *myTableView;
2122
@property (nonatomic, strong) ODRefreshControl *refreshControl;
2223
@property (nonatomic, strong) UIMessageInputView *myMsgInputView;
2324
@property (nonatomic, assign) CGFloat preContentHeight;
2425
@property (nonatomic, strong) PrivateMessage *messageToResendOrDelete;
26+
@property (strong, nonatomic) NSTimer *pollTimer;
2527
@end
2628

2729
@implementation ConversationViewController
2830

31+
static const NSTimeInterval kPollTimeInterval = 10.0;
32+
2933
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
3034
{
3135
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
@@ -79,6 +83,7 @@ - (void)viewWillDisappear:(BOOL)animated{
7983
if (_myMsgInputView) {
8084
[_myMsgInputView prepareToDismiss];
8185
}
86+
[self stopPolling];
8287
}
8388

8489
- (void)viewDidAppear:(BOOL)animated{
@@ -88,6 +93,18 @@ - (void)viewDidAppear:(BOOL)animated{
8893
[_myMsgInputView prepareToShow];
8994
}
9095
[self.myTableView reloadData];
96+
[self startPolling];
97+
}
98+
99+
- (void)dataChangedWithError:(BOOL)hasError scrollToBottom:(BOOL)scrollToBottom animated:(BOOL)animated{
100+
[self.myTableView reloadData];
101+
if (scrollToBottom) {
102+
[self scrollToBottomAnimated:animated];
103+
}
104+
__weak typeof(self) weakSelf = self;
105+
[self.view configBlankPage:EaseBlankPageTypePrivateMsg hasData:(self.myPriMsgs.dataList.count > 0) hasError:hasError reloadButtonBlock:^(id sender) {
106+
[weakSelf refreshLoadMore:NO];
107+
}];
91108
}
92109

93110
- (void)didReceiveMemoryWarning
@@ -164,6 +181,39 @@ - (void)refreshLoadMore:(BOOL)willLoadMore{
164181
}];
165182
}
166183

184+
#pragma mark Poll
185+
186+
- (void)startPolling{
187+
[self stopPolling];
188+
__weak ConversationViewController *weakSelf = self;
189+
_pollTimer = [NSTimer scheduledTimerWithTimeInterval:kPollTimeInterval block:^{
190+
__strong ConversationViewController *strongSelf = weakSelf;
191+
[strongSelf doPoll];
192+
} repeats:YES];
193+
}
194+
195+
- (void)stopPolling{
196+
[_pollTimer invalidate];
197+
_pollTimer = nil;
198+
}
199+
200+
- (void)doPoll{
201+
if (!_myPriMsgs || _myPriMsgs.isLoading) {
202+
return;
203+
}
204+
if (_myPriMsgs.list.count <= 0) {
205+
[self refreshLoadMore:NO];
206+
return;
207+
}
208+
__weak typeof(self) weakSelf = self;
209+
[[Coding_NetAPIManager sharedManager] request_Fresh_PrivateMessages:_myPriMsgs andBlock:^(id data, NSError *error) {
210+
if (data) {
211+
[weakSelf.myPriMsgs configWithPollArray:data];
212+
[weakSelf dataChangedWithError:NO scrollToBottom:YES animated:YES];
213+
}
214+
}];
215+
}
216+
167217
#pragma mark Table M
168218

169219
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
@@ -327,16 +377,14 @@ - (void)sendPrivateMessage:(id)obj{
327377

328378
- (void)sendPrivateMessageWithMsg:(PrivateMessage *)nextMsg{
329379
[_myPriMsgs sendNewMessage:nextMsg];
330-
[self.myTableView reloadData];
331-
[self scrollToBottomAnimated:YES];
380+
[self dataChangedWithError:NO scrollToBottom:YES animated:YES];
332381

333382
__weak typeof(self) weakSelf = self;
334383
[[Coding_NetAPIManager sharedManager] request_SendPrivateMessage:nextMsg andBlock:^(id data, NSError *error) {
335384
if (data) {
336385
[weakSelf.myPriMsgs sendSuccessMessage:data andOldMessage:nextMsg];
337386
}
338-
[weakSelf.myTableView reloadData];
339-
[weakSelf scrollToBottomAnimated:YES];
387+
[weakSelf dataChangedWithError:NO scrollToBottom:YES animated:YES];
340388
} progerssBlock:^(CGFloat progressValue) {
341389
DebugLog(@"\n%.2f", progressValue);
342390
}];
@@ -348,11 +396,11 @@ - (void)deletePrivateMessageWithMsg:(PrivateMessage *)curMsg{
348396
__weak typeof(self) weakSelf = self;
349397
if (curMsg.sendStatus == PrivateMessageStatusSendFail) {
350398
[_myPriMsgs deleteMessage:curMsg];
351-
[_myTableView reloadData];
399+
[self dataChangedWithError:NO scrollToBottom:NO animated:NO];
352400
}else if (curMsg.sendStatus == PrivateMessageStatusSendSucess) {
353401
[[Coding_NetAPIManager sharedManager] request_DeletePrivateMessage:curMsg andBlock:^(id data, NSError *error) {
354402
[weakSelf.myPriMsgs deleteMessage:curMsg];
355-
[weakSelf.myTableView reloadData];
403+
[self dataChangedWithError:NO scrollToBottom:NO animated:NO];
356404
}];
357405
}
358406
}
@@ -437,5 +485,6 @@ - (void)dealloc
437485
{
438486
_myTableView.delegate = nil;
439487
_myTableView.dataSource = nil;
488+
[_pollTimer invalidate];
440489
}
441490
@end

Coding_iOS/Controllers/RootControllers/BaseViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ + (UIViewController *)analyseVCFromLinkStr:(NSString *)linkStr analyseMethod:(An
222222
if ([presentingVC isKindOfClass:[ConversationViewController class]]) {
223223
ConversationViewController *vc = (ConversationViewController *)presentingVC;
224224
if ([vc.myPriMsgs.curFriend.global_key isEqualToString:user_global_key]) {
225-
[vc refreshLoadMore:NO];
225+
[vc doPoll];
226226
analyseVCIsNew = NO;
227227
analyseVC = vc;
228228
}

Coding_iOS/Models/PrivateMessages.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@
1111

1212
@interface PrivateMessages : NSObject
1313
@property (readwrite, nonatomic, strong) NSNumber *page, *pageSize, *totalPage, *totalRow;
14-
@property (assign, nonatomic) BOOL canLoadMore, willLoadMore, isLoading;
14+
@property (assign, nonatomic) BOOL canLoadMore, willLoadMore, isLoading, isPolling;
1515
@property (readwrite, nonatomic, strong) NSDictionary *propertyArrayMap;
16-
@property (readwrite, nonatomic, strong) NSMutableArray *list, *nextMessages;
16+
@property (readwrite, nonatomic, strong) NSMutableArray *list, *nextMessages, *dataList;
1717
@property (readwrite, nonatomic, strong) User *curFriend;
18-
//@property (readwrite, nonatomic, strong) NSString *nextContent;
1918
+ (PrivateMessages *)priMsgsWithUser:(User *)user;
2019

2120
- (NSString *)localPrivateMessagesPath;
2221
- (NSString *)toPath;
2322
- (NSDictionary *)toParams;
24-
- (void)configWithObj:(PrivateMessages *)priMsgs;
2523

24+
- (NSString *)toPollPath;
25+
- (NSDictionary *)toPollParams;
26+
27+
- (void)configWithObj:(PrivateMessages *)priMsgs;
28+
- (void)configWithPollArray:(NSArray *)pollList;
2629
- (void)sendNewMessage:(PrivateMessage *)nextMsg;
2730
- (void)sendSuccessMessage:(PrivateMessage *)sucessMsg andOldMessage:(PrivateMessage *)oldMsg;
28-
2931
- (void)deleteMessage:(PrivateMessage *)msg;
32+
3033
@end

Coding_iOS/Models/PrivateMessages.m

Lines changed: 98 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)