From b228041d09c2d1721a68b9b155b5759aadb6a7ea Mon Sep 17 00:00:00 2001 From: Benjamin Reis Date: Tue, 31 Oct 2017 12:02:39 +0100 Subject: [PATCH] use chat room callbacks instead of NSNotifications --- Classes/ChatConversationView.m | 97 +++++++++++++++++----------------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/Classes/ChatConversationView.m b/Classes/ChatConversationView.m index 7a4fbc292..bfba6d344 100644 --- a/Classes/ChatConversationView.m +++ b/Classes/ChatConversationView.m @@ -100,18 +100,10 @@ static UICompositeViewDescription *compositeDescription = nil; selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; - [NSNotificationCenter.defaultCenter addObserver:self - selector:@selector(textReceivedEvent:) - name:kLinphoneMessageReceived - object:nil]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(onMessageChange:) name:UITextViewTextDidChangeNotification object:nil]; - [NSNotificationCenter.defaultCenter addObserver:self - selector:@selector(textComposeEvent:) - name:kLinphoneTextComposeEvent - object:nil]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(callUpdateEvent:) name:kLinphoneCallUpdate @@ -123,6 +115,9 @@ static UICompositeViewDescription *compositeDescription = nil; linphone_chat_room_cbs_set_participant_added(cbs, on_chat_room_participant_added); linphone_chat_room_cbs_set_participant_removed(cbs, on_chat_room_participant_removed); linphone_chat_room_cbs_set_participant_admin_status_changed(cbs, on_chat_room_participant_admin_status_changed); + linphone_chat_room_cbs_set_chat_message_received(cbs, on_chat_room_chat_message_received); + linphone_chat_room_cbs_set_chat_message_sent(cbs, on_chat_room_chat_message_sent); + linphone_chat_room_cbs_set_is_composing_received(cbs, on_chat_room_is_composing_received); linphone_chat_room_cbs_set_user_data(cbs, (__bridge void*)self); [self updateSuperposedButtons]; @@ -153,6 +148,9 @@ static UICompositeViewDescription *compositeDescription = nil; linphone_chat_room_cbs_set_participant_removed(cbs, NULL); linphone_chat_room_cbs_set_participant_admin_status_changed(cbs, NULL); linphone_chat_room_cbs_set_user_data(cbs, NULL); + linphone_chat_room_cbs_set_chat_message_received(cbs, NULL); + linphone_chat_room_cbs_set_chat_message_sent(cbs, NULL); + linphone_chat_room_cbs_set_is_composing_received(cbs, NULL); [_messageField resignFirstResponder]; @@ -264,11 +262,8 @@ static UICompositeViewDescription *compositeDescription = nil; // we must ref & unref message because in case of error, it will be destroy otherwise linphone_chat_room_send_chat_message(_chatRoom, linphone_chat_message_ref(msg)); - [_tableController addChatEntry:msg]; linphone_chat_message_unref(msg); - [_tableController scrollToBottom:true]; - if (linphone_core_lime_enabled(LC) == LinphoneLimeMandatory && !linphone_chat_room_lime_available(_chatRoom)) { [LinphoneManager.instance alertLIME:_chatRoom]; } @@ -383,44 +378,6 @@ static UICompositeViewDescription *compositeDescription = nil; } } -#pragma mark - Event Functions - -- (void)textReceivedEvent:(NSNotification *)notif { - LinphoneAddress *from = [[[notif userInfo] objectForKey:@"from_address"] pointerValue]; - LinphoneChatRoom *room = [[notif.userInfo objectForKey:@"room"] pointerValue]; - LinphoneChatMessage *chat = [[notif.userInfo objectForKey:@"message"] pointerValue]; - - if (from == NULL || chat == NULL) { - return; - } - - char *fromStr = linphone_address_as_string_uri_only(from); - const LinphoneAddress *cr_from = linphone_chat_room_get_peer_address(_chatRoom); - char *cr_from_string = linphone_address_as_string_uri_only(cr_from); - - if (fromStr && cr_from_string) { - if (strcasecmp(cr_from_string, fromStr) == 0) { - if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { - linphone_chat_room_mark_as_read(room); - } - [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneMessageReceived object:self]; - [_tableController addChatEntry:chat]; - [self setComposingVisible:FALSE withDelay:0]; - [_tableController scrollToLastUnread:TRUE]; - } - } - ms_free(fromStr); - ms_free(cr_from_string); -} - -- (void)textComposeEvent:(NSNotification *)notif { - LinphoneChatRoom *room = [[[notif userInfo] objectForKey:@"room"] pointerValue]; - if (room && room == _chatRoom) { - BOOL composing = linphone_chat_room_is_remote_composing(room); - [self setComposingVisible:composing withDelay:0.3]; - } -} - #pragma mark - UITextFieldDelegate Functions - (BOOL)growingTextViewShouldBeginEditing:(HPGrowingTextView *)growingTextView { @@ -751,4 +708,46 @@ void on_chat_room_participant_admin_status_changed(LinphoneChatRoom *cr, const L if (view) {}; } +void on_chat_room_chat_message_received(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) { + ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_callbacks(cr)); + + LinphoneChatMessage *chat = linphone_event_log_get_chat_message(event_log); + const LinphoneAddress *from = linphone_chat_message_get_from_address(chat); + + if (from == NULL || chat == NULL) { + return; + } + + char *fromStr = linphone_address_as_string_uri_only(from); + const LinphoneAddress *cr_from = linphone_chat_room_get_peer_address(view.chatRoom); + char *cr_from_string = linphone_address_as_string_uri_only(cr_from); + + if (fromStr && cr_from_string) { + if (strcasecmp(cr_from_string, fromStr) == 0) { + if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { + linphone_chat_room_mark_as_read(view.chatRoom); + } + [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneMessageReceived object:view]; + [view.tableController addChatEntry:chat]; + [view setComposingVisible:FALSE withDelay:0]; + [view.tableController scrollToLastUnread:TRUE]; + } + } + ms_free(fromStr); + ms_free(cr_from_string); +} + +void on_chat_room_chat_message_sent(LinphoneChatRoom *cr, const LinphoneEventLog *event_log) { + ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_callbacks(cr)); + LinphoneChatMessage *chat = linphone_event_log_get_chat_message(event_log); + [view.tableController addChatEntry:chat]; + [view.tableController scrollToBottom:true]; +} + +void on_chat_room_is_composing_received(LinphoneChatRoom *cr, const LinphoneAddress *remoteAddr, bool_t isComposing) { + ChatConversationView *view = (__bridge ChatConversationView *)linphone_chat_room_cbs_get_user_data(linphone_chat_room_get_callbacks(cr)); + BOOL composing = linphone_chat_room_is_remote_composing(view.chatRoom); + [view setComposingVisible:composing withDelay:0.3]; +} + @end