Add LinphoneFriendPresenceUpdate observer in ChatConversationTableViewSwift

Remove LinphoneFriendPresenceUpdate observer of NotificationCenter when view disappear
This commit is contained in:
Benoit Martins 2023-04-21 16:45:26 +02:00 committed by QuentinArguillere
parent ac9f26b5aa
commit b045117ed7
8 changed files with 54 additions and 14 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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