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).

This commit is contained in:
Guillaume BIENKOWSKI 2014-11-26 15:46:10 +01:00
parent d377a112e6
commit 448020b321
3 changed files with 31 additions and 35 deletions

View file

@ -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];
}
}

View file

@ -67,7 +67,6 @@
if([tableController isEditing])
[tableController setEditing:FALSE animated:FALSE];
[editButton setOff];
[tableController loadData];
}
- (void)viewWillDisappear:(BOOL)animated {

View file

@ -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 ){