2015-01-20 12:33:01 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "ContactTableCell.h"
|
|
|
|
|
2015-01-23 14:53:39 +00:00
|
|
|
#import "MatrixSDKHandler.h"
|
2015-01-21 08:15:28 +00:00
|
|
|
|
|
|
|
#import "MXTools.h"
|
|
|
|
|
|
|
|
@interface ContactTableCell() {
|
|
|
|
id membersListener;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2015-01-20 12:33:01 +00:00
|
|
|
@implementation ContactTableCell
|
2015-01-20 15:29:42 +00:00
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-01-21 08:15:28 +00:00
|
|
|
|
|
|
|
if (membersListener) {
|
2015-01-23 14:53:39 +00:00
|
|
|
[[MatrixSDKHandler sharedHandler].mxSession removeListener:membersListener];
|
2015-01-21 08:15:28 +00:00
|
|
|
membersListener = nil;
|
|
|
|
}
|
2015-01-20 15:29:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 13:22:02 +00:00
|
|
|
- (void)setContact:(MXCContact *)aContact {
|
2015-01-23 14:53:39 +00:00
|
|
|
MatrixSDKHandler *mxHandler = [MatrixSDKHandler sharedHandler];
|
2015-01-20 15:29:42 +00:00
|
|
|
|
2015-01-21 08:15:28 +00:00
|
|
|
_contact = aContact;
|
2015-01-20 15:29:42 +00:00
|
|
|
|
|
|
|
// remove any pending observers
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-01-20 17:03:03 +00:00
|
|
|
|
2015-01-21 08:15:28 +00:00
|
|
|
// remove the matrix info until they are retrieved from the Matrix SDK
|
|
|
|
if (membersListener) {
|
|
|
|
[mxHandler.mxSession removeListener:membersListener];
|
|
|
|
membersListener = nil;
|
|
|
|
}
|
2015-01-21 09:48:38 +00:00
|
|
|
self.thumbnailView.layer.borderWidth = 0;
|
2015-01-20 17:03:03 +00:00
|
|
|
|
2015-01-21 09:48:38 +00:00
|
|
|
// be warned when the matrix ID and the thumbnail is updated
|
2015-01-23 13:22:02 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMatrixIdUpdate:) name:kMXCContactMatrixIdentifierUpdateNotification object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onThumbnailUpdate:) name:kMXCContactThumbnailUpdateNotification object:nil];
|
2015-01-21 08:15:28 +00:00
|
|
|
|
|
|
|
// Register a listener for events that concern room members
|
|
|
|
NSArray *mxMembersEvents = @[
|
|
|
|
kMXEventTypeStringPresence
|
|
|
|
];
|
|
|
|
membersListener = [mxHandler.mxSession listenToEventsOfTypes:mxMembersEvents onEvent:^(MXEvent *event, MXEventDirection direction, id customObject) {
|
|
|
|
// consider only live event
|
|
|
|
if (direction == MXEventDirectionForwards) {
|
|
|
|
NSString* matrixUserID = nil;
|
|
|
|
|
|
|
|
// get the matrix identifiers
|
|
|
|
NSArray* matrixIdentifiers = self.contact.matrixIdentifiers;
|
|
|
|
|
|
|
|
if (matrixIdentifiers.count > 0) {
|
|
|
|
matrixUserID = [self.contact.matrixIdentifiers objectAtIndex:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// the event is the current user event
|
|
|
|
if (matrixUserID && [matrixUserID isEqualToString:event.userId]) {
|
|
|
|
[self refreshPresenceUserRing:[MXTools presence:event.content[@"presence"]]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2015-01-21 09:48:38 +00:00
|
|
|
// init the contact info
|
|
|
|
self.contactDisplayNameLabel.text = _contact.displayName;
|
|
|
|
[self refreshContactThumbnail];
|
|
|
|
[self manageMatrixIcon];
|
2015-01-21 08:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)refreshUserPresence {
|
|
|
|
// search the linked mxUser
|
2015-01-23 14:53:39 +00:00
|
|
|
MatrixSDKHandler *mxHandler = [MatrixSDKHandler sharedHandler];
|
2015-01-21 08:15:28 +00:00
|
|
|
|
|
|
|
// get the matrix identifiers
|
|
|
|
NSArray* matrixIdentifiers = self.contact.matrixIdentifiers;
|
|
|
|
|
|
|
|
// if defined
|
|
|
|
if (matrixIdentifiers.count > 0) {
|
|
|
|
// get the first matrix identifier
|
|
|
|
NSString* matrixUserID = [self.contact.matrixIdentifiers objectAtIndex:0];
|
|
|
|
|
|
|
|
// check if already known as a Matrix user
|
|
|
|
MXUser* mxUser = [mxHandler.mxSession userWithUserId:matrixUserID];
|
|
|
|
|
2015-01-21 09:48:38 +00:00
|
|
|
// check if the mxUser is known
|
|
|
|
// if it is not known, the presence cannot be retrieved
|
|
|
|
if (mxUser) {
|
|
|
|
[self refreshPresenceUserRing:mxUser.presence];
|
|
|
|
// we know that this user is a matrix one
|
|
|
|
self.matrixUserIconView.hidden = NO;
|
2015-01-21 08:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 09:48:38 +00:00
|
|
|
- (void)refreshContactThumbnail {
|
|
|
|
self.thumbnailView.image = [self.contact thumbnailWithPreferedSize:self.thumbnailView.frame.size];
|
|
|
|
|
|
|
|
if (!self.thumbnailView.image) {
|
|
|
|
self.thumbnailView.image = [UIImage imageNamed:@"default-profile"];
|
|
|
|
}
|
|
|
|
|
|
|
|
// display the thumbnail in a circle
|
|
|
|
if (self.thumbnailView.layer.cornerRadius != self.thumbnailView.frame.size.width / 2) {
|
|
|
|
self.thumbnailView.layer.cornerRadius = self.thumbnailView.frame.size.width / 2;
|
|
|
|
self.thumbnailView.clipsToBounds = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 08:15:28 +00:00
|
|
|
- (void)refreshPresenceUserRing:(MXPresence)presenceStatus {
|
2015-01-23 14:53:39 +00:00
|
|
|
UIColor* ringColor = [[MatrixSDKHandler sharedHandler] getPresenceRingColor:presenceStatus];
|
2015-01-21 08:15:28 +00:00
|
|
|
|
|
|
|
// if the thumbnail is defined
|
|
|
|
if (ringColor) {
|
2015-01-21 09:48:38 +00:00
|
|
|
self.thumbnailView.layer.borderWidth = 2;
|
|
|
|
self.thumbnailView.layer.borderColor = ringColor.CGColor;
|
2015-01-21 08:15:28 +00:00
|
|
|
} else {
|
|
|
|
// remove the border
|
|
|
|
// else it draws black border
|
2015-01-21 09:48:38 +00:00
|
|
|
self.thumbnailView.layer.borderWidth = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)manageMatrixIcon {
|
|
|
|
self.matrixUserIconView.hidden = (0 == _contact.matrixIdentifiers.count);
|
|
|
|
|
|
|
|
// try to update the thumbnail with the matrix thumbnail
|
|
|
|
if (_contact.matrixIdentifiers) {
|
|
|
|
[self refreshContactThumbnail];
|
2015-01-21 08:15:28 +00:00
|
|
|
}
|
2015-01-21 09:48:38 +00:00
|
|
|
|
|
|
|
[self refreshUserPresence];
|
2015-01-20 15:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)onMatrixIdUpdate:(NSNotification *)notif {
|
|
|
|
// sanity check
|
|
|
|
if ([notif.object isKindOfClass:[NSString class]]) {
|
|
|
|
NSString* matrixID = notif.object;
|
|
|
|
|
|
|
|
if ([matrixID isEqualToString:self.contact.contactID]) {
|
2015-01-21 09:48:38 +00:00
|
|
|
[self manageMatrixIcon];
|
2015-01-20 17:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)onThumbnailUpdate:(NSNotification *)notif {
|
|
|
|
// sanity check
|
|
|
|
if ([notif.object isKindOfClass:[NSString class]]) {
|
|
|
|
NSString* matrixID = notif.object;
|
|
|
|
|
|
|
|
if ([matrixID isEqualToString:self.contact.contactID]) {
|
2015-01-21 09:48:38 +00:00
|
|
|
[self refreshContactThumbnail];
|
|
|
|
self.matrixUserIconView.hidden = (0 == _contact.matrixIdentifiers.count);
|
2015-01-22 11:01:12 +00:00
|
|
|
|
|
|
|
[self refreshUserPresence];
|
2015-01-20 15:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 12:33:01 +00:00
|
|
|
@end
|