Add presence delegate for all views

This commit is contained in:
Benoit Martins 2023-04-17 17:24:47 +02:00
parent 15481cf227
commit cd553df74e
8 changed files with 211 additions and 54 deletions

View file

@ -82,6 +82,41 @@
[self searchBar:_searchBar textDidChange:_searchBar.text];
self.tableView.accessibilityIdentifier = @"Suggested addresses";
NSDictionary* userInfo;
[NSNotificationCenter.defaultCenter addObserver:self
selector: @selector(receiveTestNotification:)
name: @"LinphoneFriendPresenceUpdate"
object: userInfo];
}
-(void) receiveTestNotification:(NSNotification*)notification
{
if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"])
{
NSDictionary* userInfo = notification.userInfo;
NSString* friend = (NSString*)userInfo[@"friend"];
for (int i = 0; i < _addresses.count; i++)
{
NSString *key = [_addresses objectAtIndex:i];
Contact *contact = [LinphoneManager.instance.fastAddressBook.addressBookMap objectForKey:[FastAddressBook normalizeSipURI:key use_prefix:[CallManager.instance applyInternationalPrefix]]];
if (!contact) {
contact = [_ldapAndProvisioningContactAddressBookMap objectForKey:key];
}
if (contact.friend != nil) {
char *curi = linphone_address_as_string_uri_only(linphone_friend_get_address(contact.friend));
NSString *uri = [NSString stringWithUTF8String:curi];
if([uri isEqual:friend]){
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:0];
NSArray* indexArray = [NSArray arrayWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
}
}
}
}
}
- (void) viewWillDisappear:(BOOL)animated {

View file

@ -96,6 +96,39 @@ static UICompositeViewDescription *compositeDescription = nil;
selector:@selector(onLinphoneCoreReady:)
name:kLinphoneGlobalStateUpdate
object:nil];
NSDictionary* userInfo;
[NSNotificationCenter.defaultCenter addObserver:self
selector: @selector(receiveTestNotification:)
name: @"LinphoneFriendPresenceUpdate"
object: userInfo];
}
-(void) receiveTestNotification:(NSNotification*)notification
{
if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"])
{
NSDictionary* userInfo = notification.userInfo;
NSString* friend = (NSString*)userInfo[@"friend"];
for (int i = 0; i < _contacts.count; i++)
{
NSString *uri = _contacts[i];
LinphoneAddress *addr = linphone_address_new(uri.UTF8String);
if (addr != nil) {
char *curi = linphone_address_as_string_uri_only(addr);
NSString *uri = [NSString stringWithUTF8String:curi];
if([uri isEqual:friend]){
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:0];
NSArray* indexArray = [NSArray arrayWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
}
}
}
}
}
- (void)viewWillDisappear:(BOOL)animated {

View file

@ -67,9 +67,25 @@
{
NSDictionary* userInfo = notification.userInfo;
NSString* friend = (NSString*)userInfo[@"friend"];
BOOL friendIsOnline = (BOOL)userInfo[@"isOnline"];
//NSLog(@"Successfully received test notification! %@ %d", friend, friendIsOnline);
for (int i = 0; i < bctbx_list_size(_data); i++)
{
LinphoneChatRoom *chatRoom = (LinphoneChatRoom *)bctbx_list_nth_data(_data, i);
bctbx_list_t *participants = linphone_chat_room_get_participants(chatRoom);
LinphoneParticipant *firstParticipant = participants ? (LinphoneParticipant *)participants->data : NULL;
char *curi = linphone_address_as_string_uri_only(linphone_participant_get_address(firstParticipant));
NSString *uri = [NSString stringWithUTF8String:curi];
LinphoneChatRoomCapabilitiesMask capabilities = linphone_chat_room_get_capabilities(chatRoom);
bool oneToOne = capabilities & LinphoneChatRoomCapabilitiesOneToOne;
if(oneToOne && [uri isEqual:friend]){
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:0];
NSArray* indexArray = [NSArray arrayWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
}
}
}
}

View file

@ -302,6 +302,20 @@
[self updateBackOrCancelButton];
[self recomputeTableViewSize:FALSE];
NSDictionary* userInfo;
[NSNotificationCenter.defaultCenter addObserver:self
selector: @selector(receiveTestNotification:)
name: @"LinphoneFriendPresenceUpdate"
object: userInfo];
}
-(void) receiveTestNotification:(NSNotification*)notification
{
if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"])
{
[self resetData];
}
}
- (void)deviceOrientationDidChange:(NSNotification*)notif {

View file

@ -60,6 +60,41 @@
[view setContact:nil];
}
}
NSDictionary* userInfo;
[NSNotificationCenter.defaultCenter addObserver:self
selector: @selector(receiveTestNotification:)
name: @"LinphoneFriendPresenceUpdate"
object: userInfo];
}
-(void) receiveTestNotification:(NSNotification*)notification
{
if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"])
{
NSDictionary* userInfo = notification.userInfo;
NSString* friend = (NSString*)userInfo[@"friend"];
self.tableView.indexPathsForVisibleRows;
for (int i = 0; i < addressBookMap.count; i++)
{
/*
NSMutableArray *subAr = [addressBookMap objectForKey:[addressBookMap keyAtIndex:2]];
Contact *contact = subAr[1];
if (contact.friend != nil) {
char *curi = linphone_address_as_string_uri_only(linphone_friend_get_address(contact.friend));
NSString *uri = [NSString stringWithUTF8String:curi];
if([uri isEqual:friend]){
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:0];
NSArray* indexArray = [NSArray arrayWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
}
}
*/
}
}
}
- (id)init {

View file

@ -93,6 +93,20 @@ static UICompositeViewDescription *compositeDescription = nil;
selector: @selector(deviceOrientationDidChange:)
name: UIDeviceOrientationDidChangeNotification
object: nil];
NSDictionary* userInfo;
[NSNotificationCenter.defaultCenter addObserver:self
selector: @selector(receiveTestNotification:)
name: @"LinphoneFriendPresenceUpdate"
object: userInfo];
}
-(void) receiveTestNotification:(NSNotification*)notification
{
if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"])
{
[self update];
}
}
- (void)viewWillDisappear:(BOOL)animated {

View file

@ -71,6 +71,36 @@
name:kLinphoneCoreUpdate
object:nil];
[self loadData];
NSDictionary* userInfo;
[NSNotificationCenter.defaultCenter addObserver:self
selector: @selector(receiveTestNotification:)
name: @"LinphoneFriendPresenceUpdate"
object: userInfo];
}
-(void) receiveTestNotification:(NSNotification*)notification
{
if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"])
{
NSDictionary* userInfo = notification.userInfo;
NSString* friend = (NSString*)userInfo[@"friend"];
const MSList *list = linphone_core_get_call_logs(LC);
int i = 0;
while (list != NULL) {
LinphoneCallLog *log = (LinphoneCallLog *)list->data;
const char *curi = linphone_address_as_string_uri_only(linphone_call_log_get_remote_address(log));
NSString *uri = [NSString stringWithUTF8String:curi];
if([uri isEqual:friend]){
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:0];
NSArray* indexArray = [NSArray arrayWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
}
i = i + 1;
list = list->next;
}
}
}
- (void)viewWillDisappear:(BOOL)animated {

View file

@ -28,6 +28,9 @@ class Avatar : UIView {
static let groupAvatar = UIImage(named:"voip_multiple_contacts_avatar")
static let singleAvatar = UIImage(named:"avatar")
var friend: [Friend] = []
var friendDelegate: [FriendDelegate] = []
required init?(coder: NSCoder) {
initialsLabel = StyledLabel(VoipTheme.call_generated_avatar_large)
super.init(coder: coder)
@ -86,12 +89,33 @@ class Avatar : UIView {
layer.cornerRadius = self.frame.width / 2.0
}
func addDelegate(contactAddress: Contact){
var delegatePresence = false
friend.forEach { friendForEach in
if friendForEach.address?.asStringUriOnly() == Friend.getSwiftObject(cObject: (contactAddress.friend)!).address?.asStringUriOnly() {
delegatePresence = true
}
}
if delegatePresence == false {
friend.append(Friend.getSwiftObject(cObject: (contactAddress.friend)!))
let newFriendDelegate = FriendDelegateStub(
onPresenceReceived: { (linphoneFriend: Friend) -> Void in
let presenceModel = linphoneFriend.getPresenceModelForUriOrTel(uriOrTel: (linphoneFriend.address?.asStringUriOnly())!)
NotificationCenter.default.post(name: Notification.Name("LinphoneFriendPresenceUpdate"), object: nil, userInfo: ["friend": linphoneFriend.address?.asStringUriOnly() ?? "", "isOnline": presenceModel!.isOnline])
}
)
friendDelegate.append(newFriendDelegate)
friend.last?.addDelegate(delegate: friendDelegate.last!)
}
}
}
@objc class AvatarBridge : NSObject { // Ugly work around to tap into the swift Avatars, until rest of the app is reworked in Swift.
static var shared : Avatar? = nil
static let size = 50.0
@objc static func prepareIt() {
if (shared != nil) {
shared?.removeFromSuperview()
@ -116,34 +140,12 @@ class Avatar : UIView {
if contactAddress != nil {
iconPresenceView = updatePresenceImage(contact: contactAddress!)
avatarWithPresence.addSubview(avatarImageWihtoutPresence)
avatarWithPresence.addSubview(iconPresenceView)
iconPresenceView.frame = CGRect(x: 35, y: 35, width: 16, height: 16)
let friend = Friend.getSwiftObject(cObject: (contactAddress?.friend)!)
let friendDelegate = FriendDelegateStub(
onPresenceReceived: { (linphoneFriend: Friend) -> Void in
let presenceModel = linphoneFriend.getPresenceModelForUriOrTel(uriOrTel: (linphoneFriend.address?.asStringUriOnly())!)
print("Successfully received test notification linphoneFriend \(linphoneFriend.address?.displayName)")
print("Successfully received test notification friend \(friend.address?.displayName)")
print("Successfully received test notification consolidatedPresence \(presenceModel!.consolidatedPresence)")
print("Successfully received test notification consolidatedPresence \(friend.consolidatedPresence)")
print("Successfully received test notification isOnline \(presenceModel!.isOnline)")
print("Successfully received test notification friend \(friend.isPresenceReceived)")
NotificationCenter.default.post(name: Notification.Name("LinphoneFriendPresenceUpdate"), object: nil, userInfo: ["friend": linphoneFriend.address?.asStringUriOnly(), "isOnline": presenceModel!.isOnline])
}
)
friend.addDelegate(delegate: friendDelegate)
shared?.addDelegate(contactAddress: contactAddress!)
}
avatarWithPresence.addSubview(avatarImageWihtoutPresence)
avatarWithPresence.addSubview(iconPresenceView)
iconPresenceView.frame = CGRect(x: 35, y: 35, width: 16, height: 16)
return avatarWithPresence.toImage()
}
@ -162,34 +164,12 @@ class Avatar : UIView {
let avatarImageWihtoutPresence = UIImageView(image: shared?.toImage())
let iconPresenceView = updatePresenceImage(contact: contact)
avatarWithPresence.addSubview(avatarImageWihtoutPresence)
avatarWithPresence.addSubview(iconPresenceView)
iconPresenceView.frame = CGRect(x: 35, y: 35, width: 16, height: 16)
shared?.addDelegate(contactAddress: contact)
let friend = Friend.getSwiftObject(cObject: contact.friend)
let friendDelegate = FriendDelegateStub(
onPresenceReceived: { (linphoneFriend: Friend) -> Void in
let presenceModel = linphoneFriend.getPresenceModelForUriOrTel(uriOrTel: (linphoneFriend.address?.asStringUriOnly())!)
print("Successfully received test notification linphoneFriend displayName \(linphoneFriend.address?.displayName)")
print("Successfully received test notification friend displayName \(friend.address?.displayName)")
print("Successfully received test notification presenceModel consolidatedPresence \(presenceModel!.consolidatedPresence)")
print("Successfully received test notification friend consolidatedPresence \(friend.consolidatedPresence)")
print("Successfully received test notification presenceModel isOnline \(presenceModel!.isOnline)")
print("Successfully received test notification friend isPresenceReceived \(friend.isPresenceReceived)")
NotificationCenter.default.post(name: Notification.Name("LinphoneFriendPresenceUpdate"), object: nil, userInfo: ["friend": linphoneFriend.address?.asStringUriOnly(), "isOnline": presenceModel!.isOnline])
}
)
friend.addDelegate(delegate: friendDelegate)
avatarWithPresence.addSubview(avatarImageWihtoutPresence)
avatarWithPresence.addSubview(iconPresenceView)
iconPresenceView.frame = CGRect(x: 35, y: 35, width: 16, height: 16)
return avatarWithPresence.toImage()
}