From 1418e1586b00157bfb0e121f42f85afdf211fbcf Mon Sep 17 00:00:00 2001 From: Norman Sander Date: Sun, 8 Mar 2015 22:45:10 +0100 Subject: [PATCH] chat optimization --- CriticalMass/CriticalMaps-Info.plist | 2 +- CriticalMass/PLChatModel.h | 3 ++- CriticalMass/PLChatModel.m | 32 +++++++++++++++++++++++----- CriticalMass/PLChatViewController.m | 10 ++++----- CriticalMass/PLConstants.m | 6 +++--- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/CriticalMass/CriticalMaps-Info.plist b/CriticalMass/CriticalMaps-Info.plist index 3894be92e..abc223cb4 100644 --- a/CriticalMass/CriticalMaps-Info.plist +++ b/CriticalMass/CriticalMaps-Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 0.5.4.1 + 0.5.4.2 LSApplicationCategoryType LSRequiresIPhoneOS diff --git a/CriticalMass/PLChatModel.h b/CriticalMass/PLChatModel.h index 80b4c6f08..aaf0c608d 100644 --- a/CriticalMass/PLChatModel.h +++ b/CriticalMass/PLChatModel.h @@ -16,10 +16,11 @@ @property(nonatomic, strong) PLDataModel *data; @property(nonatomic, strong) NSMutableArray *messages; +@property(nonatomic, strong) NSArray *sortedMessages; + (id)sharedManager; - (void)collectMessage:(NSString*)message; - (NSArray*)getMessagesArray; - (void)addMessages:(NSDictionary*)messages; -@end +@end \ No newline at end of file diff --git a/CriticalMass/PLChatModel.m b/CriticalMass/PLChatModel.m index 5cbaebac6..98c6b7641 100644 --- a/CriticalMass/PLChatModel.m +++ b/CriticalMass/PLChatModel.m @@ -54,14 +54,16 @@ - (void)collectMessage:(NSString*) text { - (void)addMessages: (NSDictionary*)messages { + // iterate obtained messages for(id key in messages){ - PLChatObject *message = [self getMessage:key]; + NSDictionary *message = [messages objectForKey:key]; + PLChatObject *co = [self getMessage:key]; - if(message){ - message.isActive = YES; + if(co){ + co.isActive = YES; + co.timestamp = [message objectForKey:@"timestamp"]; }else{ - NSDictionary *message = [messages objectForKey:key]; // create chat object PLChatObject *co = [[PLChatObject alloc] init]; @@ -77,7 +79,27 @@ - (void)addMessages: (NSDictionary*)messages { } } - // notify view + // iterate existing messages and clear old + for (int i = 0; i < [_messages count]; i++) { + PLChatObject *co = [_messages objectAtIndex:i]; + if(co.isActive){ + if (![messages objectForKey:co.identifier]) { + [_messages removeObjectAtIndex:i]; + } + } + } + + // sort + NSSortDescriptor *sortDescriptor; + sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" + ascending:YES]; + + NSArray *sortDescriptors; + sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; + + _sortedMessages = [_messages sortedArrayUsingDescriptors:sortDescriptors]; + + // Notify view [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationChatMessagesReceived object:self]; } diff --git a/CriticalMass/PLChatViewController.m b/CriticalMass/PLChatViewController.m index 86d3a8480..1467e0d09 100644 --- a/CriticalMass/PLChatViewController.m +++ b/CriticalMass/PLChatViewController.m @@ -123,7 +123,7 @@ - (void)moveContent:(BOOL)moveUp{ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return _chatModel.messages.count; + return _chatModel.sortedMessages.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath @@ -131,15 +131,15 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; - if(!_chatModel.messages){ + if(!_chatModel.sortedMessages){ return cell; } - if(!(_chatModel.messages.count > indexPath.row)){ + if(!(_chatModel.sortedMessages.count > indexPath.row)){ return cell; } - PLChatObject *message = [_chatModel.messages objectAtIndex:indexPath.row]; + PLChatObject *message = [_chatModel.sortedMessages objectAtIndex:indexPath.row]; cell.textLabel.text = message.text; cell.imageView.image = [UIImage imageNamed:@"Punk"]; @@ -182,7 +182,7 @@ - (void)onTap:(UITapGestureRecognizer *)recognizer { - (void)onMessagesReceived { [self.tableView reloadData]; - NSIndexPath* ipath = [NSIndexPath indexPathForRow: _chatModel.messages.count-1 inSection: 0]; + NSIndexPath* ipath = [NSIndexPath indexPathForRow: _chatModel.sortedMessages.count-1 inSection: 0]; [self.tableView scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionTop animated: YES]; } diff --git a/CriticalMass/PLConstants.m b/CriticalMass/PLConstants.m index 34544b8a4..3ceea4b2b 100644 --- a/CriticalMass/PLConstants.m +++ b/CriticalMass/PLConstants.m @@ -13,11 +13,11 @@ @implementation PLConstants @end // Debug -BOOL const kDebug = NO; +BOOL const kDebug = YES; BOOL const kDebugEnableTestURL = NO; -BOOL const kDebugEnableTestLocation = NO; +BOOL const kDebugEnableTestLocation = YES; BOOL const kDebugDisableHTTPRequests = NO; -BOOL const kDebugInitialTabIndex = 0; +BOOL const kDebugInitialTabIndex = 4; BOOL const kDebugShowAppirater = NO;