Perf: optimize checkbox tableviews deletion by using custom remover

This commit is contained in:
Gautier Pelloux-Prayer 2015-11-24 10:10:05 +01:00
parent ddaa3cef89
commit 647fb4a53d
10 changed files with 83 additions and 19 deletions

View file

@ -190,27 +190,32 @@
#pragma mark - UITableViewDelegate Functions
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
LinphoneChatMessage *chat = ms_list_nth_data(messageList, (int)[indexPath row]);
return [UIChatBubbleTextCell ViewHeightForMessage:chat withWidth:self.view.frame.size.width].height;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
LinphoneChatMessage *chat = ms_list_nth_data(messageList, (int)[indexPath row]);
if (chat) {
linphone_chat_room_delete_message(chatRoom, chat);
messageList = ms_list_remove(messageList, chat);
linphone_chat_room_delete_message(chatRoom, chat);
messageList = ms_list_remove(messageList, chat);
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationBottom];
}
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationBottom];
[tableView endUpdates];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
LinphoneChatMessage *chat = ms_list_nth_data(messageList, (int)[indexPath row]);
return [UIChatBubbleTextCell ViewHeightForMessage:chat withWidth:self.view.frame.size.width].height;
- (void)removeSelectionUsing:(void (^)(NSIndexPath *))remover {
[super removeSelectionUsing:^(NSIndexPath *indexPath) {
LinphoneChatMessage *chat = ms_list_nth_data(messageList, (int)[indexPath row]);
linphone_chat_room_delete_message(chatRoom, chat);
messageList = ms_list_remove(messageList, chat);
}];
}
@end

View file

@ -498,7 +498,7 @@ static UICompositeViewDescription *compositeDescription = nil;
[self onEditionChangeClick:nil];
}
onConfirmationClick:^() {
[_tableController removeSelection];
[_tableController removeSelectionUsing:nil];
[_tableController loadData];
[self onEditionChangeClick:nil];
}];

View file

@ -176,4 +176,30 @@ static void chatTable_free_chatrooms(void *data) {
}
}
- (void)removeSelectionUsing:(void (^)(NSIndexPath *))remover {
[super removeSelectionUsing:^(NSIndexPath *indexPath) {
LinphoneChatRoom *chatRoom = (LinphoneChatRoom *)ms_list_nth_data(data, (int)[indexPath row]);
LinphoneChatMessage *last_msg = linphone_chat_room_get_user_data(chatRoom);
if (last_msg) {
linphone_chat_message_unref(last_msg);
linphone_chat_room_set_user_data(chatRoom, NULL);
}
FileTransferDelegate *ftdToDelete = nil;
for (FileTransferDelegate *ftd in [[LinphoneManager instance] fileTransferDelegates]) {
if (linphone_chat_message_get_chat_room(ftd.message) == chatRoom) {
ftdToDelete = ftd;
break;
}
}
[ftdToDelete cancel];
linphone_core_delete_chat_room(linphone_chat_room_get_core(chatRoom), chatRoom);
data = ms_list_remove(data, chatRoom);
// will force a call to [self loadData]
[[NSNotificationCenter defaultCenter] postNotificationName:kLinphoneMessageReceived object:self];
}];
}
@end

View file

@ -88,7 +88,7 @@ static UICompositeViewDescription *compositeDescription = nil;
[self onEditionChangeClick:nil];
}
onConfirmationClick:^() {
[_tableController removeSelection];
[_tableController removeSelectionUsing:nil];
[_tableController loadData];
[self onEditionChangeClick:nil];
}];

View file

@ -247,4 +247,20 @@ static void sync_address_book(ABAddressBookRef addressBook, CFDictionaryRef info
ABAddressBookRegisterExternalChangeCallback(addressBook, sync_address_book, (__bridge void *)(self));
}
}
- (void)removeSelectionUsing:(void (^)(NSIndexPath *))remover {
[super removeSelectionUsing:^(NSIndexPath *indexPath) {
ABAddressBookUnregisterExternalChangeCallback(addressBook, sync_address_book, (__bridge void *)(self));
OrderedDictionary *subDic = [addressBookMap objectForKey:[addressBookMap keyAtIndex:[indexPath section]]];
NSString *key = [[subDic allKeys] objectAtIndex:[indexPath row]];
ABRecordRef contact = (__bridge ABRecordRef)([subDic objectForKey:key]);
NSString *firstChar = [[self displayNameForContact:contact] substringToIndex:1];
[[addressBookMap objectForKey:firstChar] removeObjectForKey:[self displayNameForContact:contact]];
if ([self.tableView numberOfRowsInSection:indexPath.section] == 1) {
[addressBookMap removeObjectForKey:firstChar];
}
[[[LinphoneManager instance] fastAddressBook] removeContact:contact];
}];
}
@end

View file

@ -202,7 +202,7 @@ static UICompositeViewDescription *compositeDescription = nil;
[self onEditionChangeClick:nil];
}
onConfirmationClick:^() {
[tableController removeSelection];
[tableController removeSelectionUsing:nil];
[tableController loadData];
}];
}

View file

@ -222,4 +222,17 @@
}
}
- (void)removeSelectionUsing:(void (^)(NSIndexPath *))remover {
[super removeSelectionUsing:^(NSIndexPath *indexPath) {
id log = [_sections objectForKey:_sortedDays[indexPath.section]][indexPath.row];
LinphoneCallLog *callLog = [log pointerValue];
linphone_core_remove_call_log([LinphoneManager getLc], callLog);
[[_sections objectForKey:_sortedDays[indexPath.section]] removeObject:log];
if (((NSArray *)[_sections objectForKey:_sortedDays[indexPath.section]]).count == 0) {
[_sections removeObjectForKey:_sortedDays[indexPath.section]];
[_sortedDays removeObjectAtIndex:indexPath.section];
}
}];
}
@end

View file

@ -99,7 +99,7 @@ static UICompositeViewDescription *compositeDescription = nil;
[self onEditionChangeClick:nil];
}
onConfirmationClick:^() {
[_tableController removeSelection];
[_tableController removeSelectionUsing:nil];
[_tableController loadData];
[self onEditionChangeClick:nil];
}];

View file

@ -21,7 +21,7 @@
- (void)loadData;
- (void)accessoryForCell:(UITableViewCell *)cell atPath:(NSIndexPath *)indexPath;
- (void)removeSelection;
- (void)removeSelectionUsing:(void (^)(NSIndexPath *indexPath))remover;
- (IBAction)onSelectionToggle:(id)sender;
- (IBAction)onEditClick:(id)sender;

View file

@ -107,16 +107,20 @@
_editButton.enabled = _emptyView.hidden = ([self totalNumberOfItems] > 0);
}
- (void)removeSelection {
- (void)removeSelectionUsing:(void (^)(NSIndexPath *indexPath))remover {
// we must iterate through selected items in reverse order
[_selectedItems sortUsingComparator:^(NSIndexPath *obj1, NSIndexPath *obj2) {
return [obj2 compare:obj1];
}];
NSArray *copy = [[NSArray alloc] initWithArray:_selectedItems];
for (NSIndexPath *indexPath in copy) {
[self tableView:self.tableView
commitEditingStyle:UITableViewCellEditingStyleDelete
forRowAtIndexPath:indexPath];
if (remover) {
remover(indexPath);
} else {
[self tableView:self.tableView
commitEditingStyle:UITableViewCellEditingStyleDelete
forRowAtIndexPath:indexPath];
}
}
[_selectedItems removeAllObjects];
[self setEditing:NO animated:YES];