From 448020b32122b88e144c2fff26c13a1270b16d3b Mon Sep 17 00:00:00 2001 From: Guillaume BIENKOWSKI Date: Wed, 26 Nov 2014 15:46:10 +0100 Subject: [PATCH] Store the last user message into the chatroom user data. This should speed up the display of the last message in the ChatView and prevent useless multiple calls to get_history() (which goes into SQLite). --- Classes/ChatTableViewController.m | 39 ++++++++++++++++++++++++------- Classes/ChatViewController.m | 1 - Classes/LinphoneUI/UIChatCell.m | 26 +-------------------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/Classes/ChatTableViewController.m b/Classes/ChatTableViewController.m index bb4f88c71..f47aca591 100644 --- a/Classes/ChatTableViewController.m +++ b/Classes/ChatTableViewController.m @@ -50,10 +50,8 @@ #pragma mark - static int sorted_history_comparison(LinphoneChatRoom *to_insert, LinphoneChatRoom *elem){ - MSList* new_history = linphone_chat_room_get_history(to_insert, 1); - LinphoneChatMessage* last_new_message = new_history? new_history->data : NULL; - MSList* elem_history = linphone_chat_room_get_history(elem, 1); - LinphoneChatMessage* last_elem_message = elem_history?elem_history->data:NULL; + LinphoneChatMessage* last_new_message = linphone_chat_room_get_user_data(to_insert); + LinphoneChatMessage* last_elem_message = linphone_chat_room_get_user_data(elem); if( last_new_message && last_elem_message ){ time_t new = linphone_chat_message_get_time(last_new_message); @@ -70,13 +68,36 @@ static int sorted_history_comparison(LinphoneChatRoom *to_insert, LinphoneChatRo MSList* iter = unsorted; while (iter) { - sorted = ms_list_insert_sorted(sorted, iter->data, (MSCompareFunc)sorted_history_comparison); + // store last message in user data + MSList* history = linphone_chat_room_get_history(iter->data, 1); + LinphoneChatMessage* last_msg = history? history->data : NULL; + if( last_msg ){ + linphone_chat_room_set_user_data(iter->data, linphone_chat_message_ref(last_msg)); + } + ms_list_free_with_data(history, (void (*)(void *))linphone_chat_message_unref); + + sorted = ms_list_insert_sorted(sorted, + linphone_chat_room_ref(iter->data), + (MSCompareFunc)sorted_history_comparison); + iter = iter->next; } return sorted; } +static void chatTable_free_chatrooms(void *data){ + LinphoneChatMessage* lastMsg = linphone_chat_room_get_user_data(data); + if( lastMsg ){ + linphone_chat_message_unref(linphone_chat_room_get_user_data(data)); + linphone_chat_room_set_user_data(data, NULL); + } + linphone_chat_room_unref(data); +} + - (void)loadData { + if( data != NULL ){ + ms_list_free_with_data(data, chatTable_free_chatrooms); + } data = [self sortChatRooms]; [[self tableView] reloadData]; } @@ -97,7 +118,6 @@ static int sorted_history_comparison(LinphoneChatRoom *to_insert, LinphoneChatRo if (cell == nil) { cell = [[[UIChatCell alloc] initWithIdentifier:kCellId] autorelease]; - // Background View UACellBackgroundView *selectedBackgroundView = [[[UACellBackgroundView alloc] initWithFrame:CGRectZero] autorelease]; cell.selectedBackgroundView = selectedBackgroundView; @@ -137,12 +157,13 @@ static int sorted_history_comparison(LinphoneChatRoom *to_insert, LinphoneChatRo LinphoneChatRoom *chatRoom = (LinphoneChatRoom*)ms_list_nth_data(data, (int)[indexPath row]); linphone_chat_room_delete_history(chatRoom); - linphone_chat_room_destroy(chatRoom); - data = linphone_core_get_chat_rooms([LinphoneManager getLc]); + linphone_chat_room_unref(chatRoom); + + // will force a call to [self loadData] + [[NSNotificationCenter defaultCenter] postNotificationName:kLinphoneTextReceived object:self]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; - [[NSNotificationCenter defaultCenter] postNotificationName:kLinphoneTextReceived object:self]; } } diff --git a/Classes/ChatViewController.m b/Classes/ChatViewController.m index aab106dbb..437717dbb 100644 --- a/Classes/ChatViewController.m +++ b/Classes/ChatViewController.m @@ -67,7 +67,6 @@ if([tableController isEditing]) [tableController setEditing:FALSE animated:FALSE]; [editButton setOff]; - [tableController loadData]; } - (void)viewWillDisappear:(BOOL)animated { diff --git a/Classes/LinphoneUI/UIChatCell.m b/Classes/LinphoneUI/UIChatCell.m index f5c03e651..6662669ac 100644 --- a/Classes/LinphoneUI/UIChatCell.m +++ b/Classes/LinphoneUI/UIChatCell.m @@ -75,30 +75,6 @@ return [NSString stringWithFormat:@"%@ - %@ (%ld)", addressLabel.text, chatContentLabel.text, (long)[unreadMessageLabel.text integerValue]]; } -- (LinphoneChatMessage*)getLastIncomingMessage { - LinphoneChatMessage* last_message = nil; - MSList* last_message_list = linphone_chat_room_get_history(chatRoom, 20); - MSList* iterator = nil; - - // find last element of the list: - iterator = last_message_list; - while (iterator && iterator->next) { - iterator = iterator->next; - } - - // walk the list in reverse to find the last incoming message - while (iterator) { - LinphoneChatMessage* msg = iterator->data; - if( !linphone_chat_message_is_outgoing(msg)){ - last_message = msg; - break; - } - iterator = iterator->prev; - } - ms_list_free(last_message_list); - - return last_message; -} - (void)update { NSString *displayName = nil; @@ -133,7 +109,7 @@ } [avatarImage setImage:image]; - LinphoneChatMessage* last_message = [self getLastIncomingMessage]; + LinphoneChatMessage* last_message = linphone_chat_room_get_user_data(chatRoom); if( last_message ){