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 "ContactsViewController.h"
2015-01-21 12:25:13 +00:00
// SDK api
2015-01-23 14:53:39 +00:00
# import "MatrixSDKHandler.h"
2015-01-21 12:25:13 +00:00
// application info
# import "AppDelegate.h"
// contacts management
2015-01-20 12:33:01 +00:00
# import "ContactManager.h"
2015-01-23 13:22:02 +00:00
# import "MXCContact.h"
# import "MXCEmail.h"
# import "MXCPhoneNumber.h"
2015-01-20 12:33:01 +00:00
2015-01-21 12:25:13 +00:00
// contact cell
2015-01-20 12:33:01 +00:00
# import "ContactTableCell.h"
2015-01-21 12:25:13 +00:00
// alert
2015-01-23 13:22:02 +00:00
# import "MXCAlert.h"
2015-01-20 17:40:55 +00:00
2015-01-21 12:25:13 +00:00
NSString * const kInvitationMessage = @ "I'd like to chat with you with matrix. Please, visit the website http://matrix.org to have more information." ;
2015-01-20 17:40:55 +00:00
@ interface ContactsViewController ( )
2015-01-23 13:22:02 +00:00
@ property ( strong , nonatomic ) MXCAlert * startChatMenu ;
2015-01-20 17:40:55 +00:00
@ end
2015-01-20 12:33:01 +00:00
@ implementation ContactsViewController
- ( void ) viewDidLoad {
[ super viewDidLoad ] ;
sectionedContacts = nil ;
2015-01-20 15:29:42 +00:00
// get the system collation titles
collationTitles = [ [ UILocalizedIndexedCollation currentCollation ] sectionTitles ] ;
2015-01-20 12:33:01 +00:00
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( onContactsRefresh : ) name : kContactManagerRefreshNotification object : nil ] ;
}
# pragma mark - UITableView delegate
- ( NSInteger ) numberOfSectionsInTableView : ( UITableView * ) tableView {
if ( ! sectionedContacts ) {
ContactManager * sharedManager = [ ContactManager sharedManager ] ;
sectionedContacts = [ sharedManager getSectionedContacts : sharedManager . contacts ] ;
}
return sectionedContacts . sectionedContacts . count ;
}
- ( NSInteger ) tableView : ( UITableView * ) tableView numberOfRowsInSection : ( NSInteger ) section {
return [ [ sectionedContacts . sectionedContacts objectAtIndex : section ] count ] ;
}
2015-01-21 09:48:38 +00:00
- ( CGFloat ) tableView : ( UITableView * ) tableView heightForRowAtIndexPath : ( NSIndexPath * ) indexPath {
return 50 ;
}
2015-01-20 12:33:01 +00:00
- ( NSString * ) tableView : ( UITableView * ) aTableView titleForHeaderInSection : ( NSInteger ) section {
if ( sectionedContacts . sectionTitles . count <= section ) {
return nil ;
}
else {
return ( NSString * ) [ sectionedContacts . sectionTitles objectAtIndex : section ] ;
}
}
2015-01-20 15:29:42 +00:00
- ( NSArray * ) sectionIndexTitlesForTableView : ( UITableView * ) aTableView
{
NSMutableArray * titles = [ [ NSMutableArray alloc ] initWithCapacity : 10 ] ;
[ titles addObjectsFromArray : [ [ UILocalizedIndexedCollation currentCollation ] sectionIndexTitles ] ] ;
// force the background color
if ( [ self . tableView respondsToSelector : @ selector ( setSectionIndexBackgroundColor : ) ] ) {
[ self . tableView setSectionIndexBackgroundColor : [ UIColor clearColor ] ] ;
}
return titles ;
}
- ( NSInteger ) tableView : ( UITableView * ) aTableView sectionForSectionIndexTitle : ( NSString * ) title atIndex : ( NSInteger ) index
{
NSUInteger section ;
@ synchronized ( self )
{
section = [ sectionedContacts . sectionTitles indexOfObject : title ] ;
}
// undefined title -> jump to the first valid non empty section
if ( NSNotFound = = section ) {
NSUInteger systemCollationIndex = [ collationTitles indexOfObject : title ] ;
// find in the system collation
if ( NSNotFound ! = systemCollationIndex ) {
systemCollationIndex - - ;
while ( ( systemCollationIndex = = 0 ) && ( NSNotFound = = section ) ) {
NSString * systemTitle = [ collationTitles objectAtIndex : systemCollationIndex ] ;
section = [ sectionedContacts . sectionTitles indexOfObject : systemTitle ] ;
systemCollationIndex - - ;
}
}
}
return section ;
}
2015-01-20 12:33:01 +00:00
- ( UITableViewCell * ) tableView : ( UITableView * ) tableView cellForRowAtIndexPath : ( NSIndexPath * ) indexPath {
ContactTableCell * cell = [ tableView dequeueReusableCellWithIdentifier : @ "ContactCell" forIndexPath : indexPath ] ;
2015-01-23 13:22:02 +00:00
MXCContact * contact = nil ;
2015-01-20 12:33:01 +00:00
if ( indexPath . section < sectionedContacts . sectionedContacts . count ) {
NSArray * thisSection = [ sectionedContacts . sectionedContacts objectAtIndex : indexPath . section ] ;
if ( indexPath . row < thisSection . count ) {
contact = [ thisSection objectAtIndex : indexPath . row ] ;
}
}
2015-01-20 15:29:42 +00:00
cell . contact = contact ;
2015-01-21 09:48:38 +00:00
2015-01-20 12:33:01 +00:00
return cell ;
}
2015-01-20 17:40:55 +00:00
- ( void ) tableView : ( UITableView * ) tableView didSelectRowAtIndexPath : ( NSIndexPath * ) indexPath {
[ tableView deselectRowAtIndexPath : indexPath animated : YES ] ;
2015-01-23 13:22:02 +00:00
MXCContact * contact = nil ;
2015-01-20 17:40:55 +00:00
if ( indexPath . section < sectionedContacts . sectionedContacts . count ) {
NSArray * thisSection = [ sectionedContacts . sectionedContacts objectAtIndex : indexPath . section ] ;
if ( indexPath . row < thisSection . count ) {
contact = [ thisSection objectAtIndex : indexPath . row ] ;
}
}
2015-01-21 12:25:13 +00:00
__weak typeof ( self ) weakSelf = self ;
2015-01-20 17:40:55 +00:00
NSArray * matrixIDs = contact . matrixIdentifiers ;
2015-01-21 12:25:13 +00:00
// matrix user ?
2015-01-20 17:40:55 +00:00
if ( matrixIDs . count ) {
// Display action menu : Add attachments , Invite user . . .
NSString * matrixID = [ matrixIDs objectAtIndex : 0 ] ;
2015-01-23 13:22:02 +00:00
self . startChatMenu = [ [ MXCAlert alloc ] initWithTitle : [ NSString stringWithFormat : @ "Start chat with %@" , matrixID ] message : nil style : MXCAlertStyleAlert ] ;
2015-01-20 17:40:55 +00:00
2015-01-23 13:22:02 +00:00
[ self . startChatMenu addActionWithTitle : @ "Cancel" style : MXCAlertActionStyleDefault handler : ^ ( MXCAlert * alert ) {
2015-01-20 17:40:55 +00:00
weakSelf . startChatMenu = nil ;
} ] ;
2015-01-23 13:22:02 +00:00
[ self . startChatMenu addActionWithTitle : @ "OK" style : MXCAlertActionStyleDefault handler : ^ ( MXCAlert * alert ) {
2015-01-20 17:40:55 +00:00
weakSelf . startChatMenu = nil ;
2015-01-23 14:53:39 +00:00
MatrixSDKHandler * mxHandler = [ MatrixSDKHandler sharedHandler ] ;
2015-01-20 17:40:55 +00:00
// else create new room
[ mxHandler . mxRestClient createRoom : nil
visibility : kMXRoomVisibilityPrivate
roomAlias : nil
topic : nil
success : ^ ( MXCreateRoomResponse * response ) {
// add the user
[ mxHandler . mxRestClient inviteUser : matrixID toRoom : response . roomId success : ^ {
} failure : ^ ( NSError * error ) {
NSLog ( @ "%@ invitation failed (roomId: %@): %@" , matrixID , response . roomId , error ) ;
// Alert user
[ [ AppDelegate theDelegate ] showErrorAsAlert : error ] ;
} ] ;
// Open created room
[ [ AppDelegate theDelegate ] . masterTabBarController showRoom : response . roomId ] ;
} failure : ^ ( NSError * error ) {
NSLog ( @ "Create room failed: %@" , error ) ;
// Alert user
[ [ AppDelegate theDelegate ] showErrorAsAlert : error ] ;
} ] ;
} ] ;
[ self . startChatMenu showInViewController : self ] ;
2015-01-21 12:25:13 +00:00
} else {
// invite to use matrix
if ( ( [ MFMessageComposeViewController canSendText ] ? contact . emailAddresses . count : 0 ) + ( contact . phoneNumbers . count > 0 ) ) {
2015-01-23 13:22:02 +00:00
self . startChatMenu = [ [ MXCAlert alloc ] initWithTitle : [ NSString stringWithFormat : @ "Invite this user to use matrix with" ] message : nil style : MXCAlertStyleActionSheet ] ;
2015-01-21 12:25:13 +00:00
// check if the target can send SMSes
if ( [ MFMessageComposeViewController canSendText ] ) {
// list phonenumbers
2015-01-23 13:22:02 +00:00
for ( MXCPhoneNumber * phonenumber in contact . phoneNumbers ) {
2015-01-21 12:25:13 +00:00
2015-01-23 13:22:02 +00:00
[ self . startChatMenu addActionWithTitle : phonenumber . textNumber style : MXCAlertActionStyleDefault handler : ^ ( MXCAlert * alert ) {
2015-01-21 12:25:13 +00:00
weakSelf . startChatMenu = nil ;
// launch SMS composer
MFMessageComposeViewController * messageComposer = [ [ MFMessageComposeViewController alloc ] init ] ;
if ( messageComposer )
{
messageComposer . messageComposeDelegate = weakSelf ;
messageComposer . body = kInvitationMessage ;
messageComposer . recipients = [ NSArray arrayWithObject : phonenumber . textNumber ] ;
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
[ weakSelf presentViewController : messageComposer animated : YES completion : nil ] ;
} ) ;
}
} ] ;
}
}
// list emails
2015-01-23 13:22:02 +00:00
for ( MXCEmail * email in contact . emailAddresses ) {
2015-01-21 12:25:13 +00:00
2015-01-23 13:22:02 +00:00
[ self . startChatMenu addActionWithTitle : email . emailAddress style : MXCAlertActionStyleDefault handler : ^ ( MXCAlert * alert ) {
2015-01-21 12:25:13 +00:00
weakSelf . startChatMenu = nil ;
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
NSString * subject = [ @ "Matrix.org is magic" stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding ] ;
NSString * body = [ kInvitationMessage stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding ] ;
[ [ UIApplication sharedApplication ] openURL : [ NSURL URLWithString : [ NSString stringWithFormat : @ "mailto:%@?subject=%@&body=%@" , email . emailAddress , subject , body ] ] ] ;
} ) ;
} ] ;
}
2015-01-23 13:22:02 +00:00
[ self . startChatMenu addActionWithTitle : @ "Cancel" style : MXCAlertActionStyleDefault handler : ^ ( MXCAlert * alert ) {
2015-01-21 12:25:13 +00:00
weakSelf . startChatMenu = nil ;
} ] ;
[ self . startChatMenu showInViewController : self ] ;
}
2015-01-20 17:40:55 +00:00
}
}
2015-01-20 12:33:01 +00:00
# pragma mark - Actions
- ( void ) onContactsRefresh : ( NSNotification * ) notif {
sectionedContacts = nil ;
[ self . tableView reloadData ] ;
}
2015-01-21 12:25:13 +00:00
# pragma mark MFMessageComposeViewControllerDelegate
- ( void ) messageComposeViewController : ( MFMessageComposeViewController * ) controller didFinishWithResult : ( MessageComposeResult ) result {
[ self dismissViewControllerAnimated : YES completion : nil ] ;
}
2015-01-20 12:33:01 +00:00
@ end