Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
Improve the people invite screens
Browse files Browse the repository at this point in the history
element-hq/element-ios#904

- Discover matrix users by using msisdn
  • Loading branch information
giomfo committed Mar 6, 2017
1 parent 59aae8e commit 3c1c9a7
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 112 deletions.
4 changes: 2 additions & 2 deletions MatrixKit/Models/Contact/MXKContact.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ extern NSString *const kMXKContactDefaultContactPrefixId;
- (BOOL)matchedWithPatterns:(NSArray*)patterns;

/**
Internationalize the contact phonenumbers
The default ISO 3166-1 country code used to internationalize the contact phone numbers.
*/
- (void)internationalizePhonenumbers:(NSString*)countryCode;
@property (nonatomic) NSString *defaultCountryCode;

@end
40 changes: 35 additions & 5 deletions MatrixKit/Models/Contact/MXKContact.m
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ - (BOOL)hasPrefix:(NSString*)prefix
}
}

// Check phones
for (MXKPhoneNumber* phone in _phoneNumbers)
{
if ([phone hasPrefix:prefix])
{
return YES;
}
}

return NO;
}

Expand Down Expand Up @@ -429,12 +438,14 @@ - (BOOL)matchedWithPatterns:(NSArray*)patterns
return matched;
}

- (void)internationalizePhonenumbers:(NSString*)countryCode
- (void)setDefaultCountryCode:(NSString *)defaultCountryCode
{
for(MXKPhoneNumber* phonenumber in _phoneNumbers)
for (MXKPhoneNumber* phonenumber in _phoneNumbers)
{
phonenumber.countryCode = countryCode;
phonenumber.defaultCountryCode = defaultCountryCode;
}

_defaultCountryCode = defaultCountryCode;
}

#pragma mark - getter/setter
Expand All @@ -448,15 +459,15 @@ - (NSArray*)matrixIdentifiers
[identifiers addObject:matrixIdField.matrixID];
}

for(MXKEmail* email in _emailAddresses)
for (MXKEmail* email in _emailAddresses)
{
if (email.matrixID && ([identifiers indexOfObject:email.matrixID] == NSNotFound))
{
[identifiers addObject:email.matrixID];
}
}

for(MXKPhoneNumber* pn in _phoneNumbers)
for (MXKPhoneNumber* pn in _phoneNumbers)
{
if (pn.matrixID && ([identifiers indexOfObject:pn.matrixID] == NSNotFound))
{
Expand Down Expand Up @@ -541,6 +552,25 @@ - (UIImage*)thumbnailWithPreferedSize:(CGSize)size
}
}

if (_phoneNumbers.count > 0)
{
// list the linked phones
// search if one phone field has a dedicated thumbnail
for (MXKPhoneNumber* phoneNb in _phoneNumbers)
{
if (phoneNb.avatarImage)
{
matrixThumbnail = phoneNb.avatarImage;
_matrixAvatarURL = phoneNb.matrixAvatarURL;
return matrixThumbnail;
}
else if (!firstField && phoneNb.matrixID)
{
firstField = phoneNb;
}
}
}

// if no thumbnail has been found
// try to load the first field one
if (firstField)
Expand Down
5 changes: 3 additions & 2 deletions MatrixKit/Models/Contact/MXKContactField.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

@interface MXKContactField : NSObject<NSCoding>

// contact ID where the email has been found
/**
The identifier of the contact to whom the data belongs to.
*/
@property (nonatomic, readonly) NSString *contactID;

/**
The linked matrix identifier if any
*/
Expand Down
106 changes: 50 additions & 56 deletions MatrixKit/Models/Contact/MXKContactManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#import "MXKContactManager.h"

#import "MXKContact.h"
#import "MXKEmail.h"

#import "MXKAppSettings.h"
#import "MXKTools.h"
Expand Down Expand Up @@ -59,8 +58,9 @@ Listeners registered on matrix presence and membership events (one by matrix ses
NSMutableDictionary* localContactByContactID;
NSMutableArray* localContactsWithMethods;
NSMutableArray* splitLocalContacts;

// Matrix id linked to 3PID.
NSMutableDictionary* matrixIDBy3PID;
NSMutableDictionary<NSString*, NSString*> *matrixIDBy3PID;

/**
Matrix contacts handling
Expand Down Expand Up @@ -319,8 +319,7 @@ - (NSArray*)localContactsWithMethods
// Check whether the array must be prepared
if (!localContactsWithMethods)
{
// List all the local contacts with emails
// TODO: Add the contacts with msisdn when msisdn 3PIDs will be supported
// List all the local contacts with emails and/or phones
NSArray *localContacts = self.localContacts;
localContactsWithMethods = [NSMutableArray arrayWithCapacity:localContacts.count];

Expand All @@ -330,6 +329,10 @@ - (NSArray*)localContactsWithMethods
{
[localContactsWithMethods addObject:contact];
}
else if (contact.phoneNumbers)
{
[localContactsWithMethods addObject:contact];
}
}
}

Expand Down Expand Up @@ -359,12 +362,11 @@ - (NSArray*)localContactsSplitByContactMethod
[splitLocalContacts addObject:splitContact];
}

// TODO: Add contacts with msisdn when msisdn 3PIDs will be supported
// for (MXKPhoneNumber *phone in phones)
// {
// MXKContact *splitContact = [[MXKContact alloc] initContactWithDisplayName:contact.displayName emails:nil phoneNumbers:@[phone] andThumbnail:contact.thumbnail];
// [splitLocalContacts addObject:splitContact];
// }
for (MXKPhoneNumber *phone in phones)
{
MXKContact *splitContact = [[MXKContact alloc] initContactWithDisplayName:contact.displayName emails:nil phoneNumbers:@[phone] andThumbnail:contact.thumbnail];
[splitLocalContacts addObject:splitContact];
}
}
else if (emails.count + phones.count)
{
Expand Down Expand Up @@ -612,7 +614,7 @@ - (void)refreshLocalContacts

if (countryCode)
{
[contact internationalizePhonenumbers:countryCode];
contact.defaultCountryCode = countryCode;
}

// update the local contacts list
Expand Down Expand Up @@ -690,16 +692,10 @@ - (void)updateMatrixIDsForLocalContact:(MXKContact *)contact

for (MXKPhoneNumber* phone in contact.phoneNumbers)
{
if (phone.isValidPhoneNumber)
if (phone.msisdn)
{
NSString *phoneNumber = phone.internationalPhoneNumber ? phone.internationalPhoneNumber : phone.cleanedPhonenumber;
if ([phoneNumber hasPrefix:@"+"])
{
phoneNumber = [phoneNumber substringFromIndex:1];
}

[lookup3pidsArray addObject:@[kMX3PIDMediumMSISDN, phoneNumber]];
[threepids addObject:phoneNumber];
[lookup3pidsArray addObject:@[kMX3PIDMediumMSISDN, phone.msisdn]];
[threepids addObject:phone.msisdn];
}
}

Expand Down Expand Up @@ -800,19 +796,13 @@ - (void)updateMatrixIDsForAllLocalContacts

for (MXKPhoneNumber* phone in contact.phoneNumbers)
{
if (phone.isValidPhoneNumber)
if (phone.msisdn)
{
NSString *phoneNumber = phone.internationalPhoneNumber ? phone.internationalPhoneNumber : phone.cleanedPhonenumber;
if ([phoneNumber hasPrefix:@"+"])
{
phoneNumber = [phoneNumber substringFromIndex:1];
}

// Not yet added
if ([threepids indexOfObject:phoneNumber] == NSNotFound)
if ([threepids indexOfObject:phone.msisdn] == NSNotFound)
{
[lookup3pidsArray addObject:@[kMX3PIDMediumMSISDN, phoneNumber]];
[threepids addObject:phoneNumber];
[lookup3pidsArray addObject:@[kMX3PIDMediumMSISDN, phone.msisdn]];
[threepids addObject:phone.msisdn];
}
}
}
Expand All @@ -833,8 +823,14 @@ - (void)updateMatrixIDsForAllLocalContacts
// Sanity check
if (discoveredUser.count == 3)
{
[threepids addObject:discoveredUser[1]];
[userIds addObject:discoveredUser[2]];
id threepid = discoveredUser[1];
id userId = discoveredUser[2];

if ([threepid isKindOfClass:[NSString class]] && [userId isKindOfClass:[NSString class]])
{
[threepids addObject:threepid];
[userIds addObject:userId];
}
}
}

Expand Down Expand Up @@ -922,9 +918,9 @@ - (void)internationalizePhoneNumbers:(NSString*)countryCode
dispatch_async(processingQueue, ^{
NSArray* contactsSnapshot = [localContactByContactID allValues];

for(MXKContact* contact in contactsSnapshot)
for (MXKContact* contact in contactsSnapshot)
{
[contact internationalizePhonenumbers:countryCode];
contact.defaultCountryCode = countryCode;
}

[self cacheLocalContacts];
Expand Down Expand Up @@ -1266,33 +1262,31 @@ - (void)updateMatrixContactWithID:(NSString*)matrixId

- (void)updateLocalContactMatrixIDs:(MXKContact*) contact
{
// the phonenumbers wil be managed later
/*for(MXKPhoneNumber* pn in contact.phoneNumbers)
{
if (pn.textNumber.length > 0)
{
// not yet added
if ([pids indexOfObject:pn.textNumber] == NSNotFound)
{
[pids addObject:pn.textNumber];
[medias addObject:@"msisdn"];
}
}
}*/
for (MXKPhoneNumber* phoneNumber in contact.phoneNumbers)
{
if (phoneNumber.msisdn)
{
NSString* matrixID = [matrixIDBy3PID objectForKey:phoneNumber.msisdn];

dispatch_async(dispatch_get_main_queue(), ^{

[phoneNumber setMatrixID:matrixID];

});
}
}

for (MXKEmail* email in contact.emailAddresses)
{
if (email.emailAddress.length > 0)
{
id matrixID = [matrixIDBy3PID objectForKey:email.emailAddress];
NSString *matrixID = [matrixIDBy3PID objectForKey:email.emailAddress];

if ([matrixID isKindOfClass:[NSString class]])
{
dispatch_async(dispatch_get_main_queue(), ^{
[email setMatrixID:matrixID];
});
}
dispatch_async(dispatch_get_main_queue(), ^{

[email setMatrixID:matrixID];

});
}
}
}
Expand All @@ -1308,7 +1302,7 @@ - (void)updateAllLocalContactsMatrixIDs
NSArray* localContacts = [localContactByContactID allValues];

// update the contacts info
for(MXKContact* contact in localContacts)
for (MXKContact* contact in localContacts)
{
[self updateLocalContactMatrixIDs:contact];
}
Expand Down
53 changes: 47 additions & 6 deletions MatrixKit/Models/Contact/MXKPhoneNumber.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2015 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -18,20 +19,60 @@

#import "MXKContactField.h"

#import "NBPhoneNumberUtil.h"

@interface MXKPhoneNumber : MXKContactField

// phonenumber info
/**
The phone number information
*/
@property (nonatomic, readonly) NSString *type;
@property (nonatomic, readonly) NSString *textNumber;
@property (nonatomic, readonly) NSString *internationalPhoneNumber;
@property (nonatomic, readonly) NSString *cleanedPhonenumber;

// defines a country code will internationalize it
@property (nonatomic, readwrite) NSString *countryCode;
@property (nonatomic, readonly) BOOL isValidPhoneNumber;
/**
When the number is considered to be a possible number. We expose here
the corresponding NBPhoneNumber instance. Use the NBPhoneNumberUtil interface
to format this phone number, or check whether the number is actually a
valid number.
*/
@property (nonatomic, readonly) NBPhoneNumber* nbPhoneNumber;

/**
The default ISO 3166-1 country code used to parse the text number,
and create the nbPhoneNumber instance.
*/
@property (nonatomic) NSString *defaultCountryCode;

/**
The Mobile Station International Subscriber Directory Number.
Available when the nbPhoneNumber is not nil.
*/
@property (nonatomic, readonly) NSString *msisdn;

- (id)initWithTextNumber:(NSString*)textNumber type:(NSString*)aType contactID:(NSString*)aContactID matrixID:(NSString*)matrixID;
/**
Create a new MXKPhoneNumber instance
@param textNumber the phone number
@param type
@param contactID The identifier of the contact to whom the data belongs to.
@param matrixID The linked matrix identifier if any.
*/
- (id)initWithTextNumber:(NSString*)textNumber type:(NSString*)type contactID:(NSString*)contactID matrixID:(NSString*)matrixID;

/**
Return YES when all the provided patterns are found in the phone number or its msisdn.
@param patterns an array of patterns (The potential "+" (or "00") prefix is ignored during the msisdn handling).
*/
- (BOOL)matchedWithPatterns:(NSArray*)patterns;

/**
Tell whether the phone number or its msisdn has the provided prefix.
@param prefix a non empty string (The potential "+" (or "00") prefix is ignored during the msisdn handling).
@return YES when the phone number or its msisdn has the provided prefix.
*/
- (BOOL)hasPrefix:(NSString*)prefix;

@end

0 comments on commit 3c1c9a7

Please sign in to comment.