Skip to content

Commit

Permalink
Merge branch 'hotfix/clear_chats'
Browse files Browse the repository at this point in the history
  • Loading branch information
normansander committed Mar 8, 2015
2 parents acdaf0d + 1418e15 commit 6511ad1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CriticalMass/CriticalMaps-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>0.5.4.1</string>
<string>0.5.4.2</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSRequiresIPhoneOS</key>
Expand Down
3 changes: 2 additions & 1 deletion CriticalMass/PLChatModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 27 additions & 5 deletions CriticalMass/PLChatModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
}

Expand Down
10 changes: 5 additions & 5 deletions CriticalMass/PLChatViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,23 @@ - (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
{

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"];
Expand Down Expand Up @@ -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];
}

Expand Down
6 changes: 3 additions & 3 deletions CriticalMass/PLConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down

0 comments on commit 6511ad1

Please sign in to comment.