From b045117ed777395f41a992209b0bada817dc59b7 Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Fri, 21 Apr 2023 16:45:26 +0200 Subject: [PATCH] Add LinphoneFriendPresenceUpdate observer in ChatConversationTableViewSwift Remove LinphoneFriendPresenceUpdate observer of NotificationCenter when view disappear --- Classes/ChatConversationCreateTableView.m | 5 ++-- Classes/ChatConversationInfoView.m | 5 ++-- Classes/ChatsListTableView.m | 6 +++-- Classes/ContactDetailsView.m | 6 +++-- Classes/ContactsListTableView.m | 9 +++++-- Classes/HistoryDetailsView.m | 5 ++-- Classes/HistoryListTableView.m | 5 ++-- .../ChatConversationTableViewSwift.swift | 27 +++++++++++++++++++ 8 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Classes/ChatConversationCreateTableView.m b/Classes/ChatConversationCreateTableView.m index a5a73481c..c376d08b9 100644 --- a/Classes/ChatConversationCreateTableView.m +++ b/Classes/ChatConversationCreateTableView.m @@ -84,12 +84,12 @@ NSDictionary* userInfo; [NSNotificationCenter.defaultCenter addObserver:self - selector: @selector(receiveTestNotification:) + selector: @selector(receivePresenceNotification:) name: @"LinphoneFriendPresenceUpdate" object: userInfo]; } --(void) receiveTestNotification:(NSNotification*)notification +-(void) receivePresenceNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"]) { @@ -121,6 +121,7 @@ - (void) viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LinphoneFriendPresenceUpdate" object:nil]; _notFirstTime = FALSE; } diff --git a/Classes/ChatConversationInfoView.m b/Classes/ChatConversationInfoView.m index 0f7e5112e..d5cab748d 100644 --- a/Classes/ChatConversationInfoView.m +++ b/Classes/ChatConversationInfoView.m @@ -100,12 +100,12 @@ static UICompositeViewDescription *compositeDescription = nil; NSDictionary* userInfo; [NSNotificationCenter.defaultCenter addObserver:self - selector: @selector(receiveTestNotification:) + selector: @selector(receivePresenceNotification:) name: @"LinphoneFriendPresenceUpdate" object: userInfo]; } --(void) receiveTestNotification:(NSNotification*)notification +-(void) receivePresenceNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"]) { @@ -134,6 +134,7 @@ static UICompositeViewDescription *compositeDescription = nil; - (void)viewWillDisappear:(BOOL)animated { [NSNotificationCenter.defaultCenter removeObserver:self]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LinphoneFriendPresenceUpdate" object:nil]; if (!_room || !_chatRoomCbs) return; diff --git a/Classes/ChatsListTableView.m b/Classes/ChatsListTableView.m index 6ab88aa0b..2b747f0e0 100644 --- a/Classes/ChatsListTableView.m +++ b/Classes/ChatsListTableView.m @@ -56,12 +56,12 @@ NSDictionary* userInfo; [NSNotificationCenter.defaultCenter addObserver:self - selector: @selector(receiveTestNotification:) + selector: @selector(receivePresenceNotification:) name: @"LinphoneFriendPresenceUpdate" object: userInfo]; } --(void) receiveTestNotification:(NSNotification*)notification +-(void) receivePresenceNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"]) { @@ -111,6 +111,8 @@ } _chatRooms = _chatRooms->next; } + + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LinphoneFriendPresenceUpdate" object:nil]; } - (void)layoutSubviews { diff --git a/Classes/ContactDetailsView.m b/Classes/ContactDetailsView.m index b86abd0a6..5a885ea5e 100644 --- a/Classes/ContactDetailsView.m +++ b/Classes/ContactDetailsView.m @@ -305,12 +305,12 @@ NSDictionary* userInfo; [NSNotificationCenter.defaultCenter addObserver:self - selector: @selector(receiveTestNotification:) + selector: @selector(receivePresenceNotification:) name: @"LinphoneFriendPresenceUpdate" object: userInfo]; } --(void) receiveTestNotification:(NSNotification*)notification +-(void) receivePresenceNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"]) { @@ -368,6 +368,8 @@ if (rm) { [LinphoneManager.instance.fastAddressBook deleteContact:_contact]; } + + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LinphoneFriendPresenceUpdate" object:nil]; } #pragma mark - UICompositeViewDelegate Functions diff --git a/Classes/ContactsListTableView.m b/Classes/ContactsListTableView.m index f2f773a3a..7d86ab6cb 100644 --- a/Classes/ContactsListTableView.m +++ b/Classes/ContactsListTableView.m @@ -62,12 +62,17 @@ } NSDictionary* userInfo; [NSNotificationCenter.defaultCenter addObserver:self - selector: @selector(receiveTestNotification:) + selector: @selector(receivePresenceNotification:) name: @"LinphoneFriendPresenceUpdate" object: userInfo]; } --(void) receiveTestNotification:(NSNotification*)notification +-(void)viewDidDisappear:(BOOL)animated{ + [super viewDidDisappear:animated]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LinphoneFriendPresenceUpdate" object:nil]; +} + +-(void) receivePresenceNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"]) { diff --git a/Classes/HistoryDetailsView.m b/Classes/HistoryDetailsView.m index 1ac93dad2..512b47db5 100644 --- a/Classes/HistoryDetailsView.m +++ b/Classes/HistoryDetailsView.m @@ -96,12 +96,12 @@ static UICompositeViewDescription *compositeDescription = nil; NSDictionary* userInfo; [NSNotificationCenter.defaultCenter addObserver:self - selector: @selector(receiveTestNotification:) + selector: @selector(receivePresenceNotification:) name: @"LinphoneFriendPresenceUpdate" object: userInfo]; } --(void) receiveTestNotification:(NSNotification*)notification +-(void) receivePresenceNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"]) { @@ -112,6 +112,7 @@ static UICompositeViewDescription *compositeDescription = nil; - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [NSNotificationCenter.defaultCenter removeObserver:self]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LinphoneFriendPresenceUpdate" object:nil]; } #pragma mark - Event Functions diff --git a/Classes/HistoryListTableView.m b/Classes/HistoryListTableView.m index 12ab5d8c4..261397f57 100644 --- a/Classes/HistoryListTableView.m +++ b/Classes/HistoryListTableView.m @@ -73,12 +73,12 @@ [self loadData]; NSDictionary* userInfo; [NSNotificationCenter.defaultCenter addObserver:self - selector: @selector(receiveTestNotification:) + selector: @selector(receivePresenceNotification:) name: @"LinphoneFriendPresenceUpdate" object: userInfo]; } --(void) receiveTestNotification:(NSNotification*)notification +-(void) receivePresenceNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"]) { @@ -109,6 +109,7 @@ [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneAddressBookUpdate object:nil]; [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneCoreUpdate object:nil]; [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneCallUpdate object:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LinphoneFriendPresenceUpdate" object:nil]; } #pragma mark - Event Functions diff --git a/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift b/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift index f10993141..d6a32fbac 100644 --- a/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift +++ b/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift @@ -99,7 +99,34 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour scrollToBottom(animated: false) } collectionView.reloadData() + + NotificationCenter.default.addObserver(self, selector: #selector(self.receivePresenceNotification(notification:)), name: Notification.Name("LinphoneFriendPresenceUpdate"), object: nil) } + + override func viewWillDisappear(_ animated: Bool) { + NotificationCenter.default.removeObserver(self, name: Notification.Name("LinphoneFriendPresenceUpdate"), object: nil) + NotificationCenter.default.removeObserver(self) + } + + @objc func receivePresenceNotification(notification: NSNotification) { + if (notification.name.rawValue == "LinphoneFriendPresenceUpdate"){ + let userInfo = notification.userInfo + let friend = userInfo!["friend"] + + let indexPathsVisible = self.collectionView.indexPathsForVisibleItems + for i in 0...indexPathsVisible.count-1 { + let contact = ChatConversationTableViewModel.sharedModel.getMessage(index: indexPathsVisible[i].row)?.chatMessage?.fromAddress + if (contact != nil){ + let uri = "sip:" + contact!.username + "@" + contact!.domain + + if(uri == friend as! String){ + let indexPath = indexPathsVisible[i] + collectionView.reloadItems(at: [indexPath]) + } + } + } + } + } func scrollToMessage(message: ChatMessage){ let messageIndex = ChatConversationTableViewModel.sharedModel.getIndexMessage(message: message)