2016-09-06 15:37:22 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2017-03-08 15:14:41 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2016-09-06 15:37:22 +00:00
|
|
|
|
|
|
|
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 "StartChatViewController.h"
|
|
|
|
|
|
|
|
#import "AppDelegate.h"
|
2018-11-30 14:56:10 +00:00
|
|
|
#import "Riot-Swift.h"
|
2016-09-06 15:37:22 +00:00
|
|
|
|
|
|
|
@interface StartChatViewController ()
|
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// The contact used to describe the current user.
|
|
|
|
MXKContact *userContact;
|
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
// Section indexes
|
|
|
|
NSInteger participantsSection;
|
|
|
|
|
|
|
|
// The current list of participants.
|
|
|
|
NSMutableArray<MXKContact*> *participants;
|
|
|
|
|
2016-09-07 07:33:30 +00:00
|
|
|
// Navigation bar items
|
2016-09-06 15:37:22 +00:00
|
|
|
UIBarButtonItem *cancelBarButtonItem;
|
|
|
|
UIBarButtonItem *createBarButtonItem;
|
|
|
|
|
2016-09-07 07:33:30 +00:00
|
|
|
// HTTP Request
|
2016-09-06 15:37:22 +00:00
|
|
|
MXHTTPOperation *roomCreationRequest;
|
|
|
|
|
|
|
|
// This dictionary tells for each display name whether it appears several times in participants list
|
|
|
|
NSMutableDictionary <NSString*, NSNumber*> *isMultiUseNameByDisplayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation StartChatViewController
|
|
|
|
|
|
|
|
#pragma mark - Class methods
|
|
|
|
|
|
|
|
+ (UINib *)nib
|
|
|
|
{
|
|
|
|
return [UINib nibWithNibName:NSStringFromClass([StartChatViewController class])
|
|
|
|
bundle:[NSBundle bundleForClass:[StartChatViewController class]]];
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
+ (instancetype)startChatViewController
|
2016-09-06 15:37:22 +00:00
|
|
|
{
|
|
|
|
return [[[self class] alloc] initWithNibName:NSStringFromClass([StartChatViewController class])
|
|
|
|
bundle:[NSBundle bundleForClass:[StartChatViewController class]]];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-01-03 13:40:23 +00:00
|
|
|
- (void)finalizeInit
|
|
|
|
{
|
|
|
|
[super finalizeInit];
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
self.screenName = @"StartChat";
|
2017-01-12 16:30:09 +00:00
|
|
|
|
2017-01-03 13:40:23 +00:00
|
|
|
_isAddParticipantSearchBarEditing = NO;
|
|
|
|
|
|
|
|
// Prepare room participants
|
|
|
|
participants = [NSMutableArray array];
|
2017-01-13 11:08:24 +00:00
|
|
|
|
|
|
|
// Assign itself as delegate
|
|
|
|
self.contactsTableViewControllerDelegate = self;
|
2017-01-03 13:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
|
|
|
// Do any additional setup after loading the view, typically from a nib.
|
|
|
|
|
|
|
|
// Adjust Top and Bottom constraints to take into account potential navBar and tabBar.
|
|
|
|
[NSLayoutConstraint deactivateConstraints:@[_searchBarTopConstraint, _tableViewBottomConstraint]];
|
|
|
|
|
|
|
|
_searchBarTopConstraint = [NSLayoutConstraint constraintWithItem:self.topLayoutGuide
|
|
|
|
attribute:NSLayoutAttributeBottom
|
|
|
|
relatedBy:NSLayoutRelationEqual
|
|
|
|
toItem:self.searchBarHeader
|
|
|
|
attribute:NSLayoutAttributeTop
|
|
|
|
multiplier:1.0f
|
|
|
|
constant:0.0f];
|
|
|
|
|
|
|
|
_tableViewBottomConstraint = [NSLayoutConstraint constraintWithItem:self.bottomLayoutGuide
|
|
|
|
attribute:NSLayoutAttributeTop
|
|
|
|
relatedBy:NSLayoutRelationEqual
|
2017-04-07 14:42:16 +00:00
|
|
|
toItem:self.contactsTableView
|
2016-09-06 15:37:22 +00:00
|
|
|
attribute:NSLayoutAttributeBottom
|
|
|
|
multiplier:1.0f
|
|
|
|
constant:0.0f];
|
|
|
|
|
|
|
|
[NSLayoutConstraint activateConstraints:@[_searchBarTopConstraint, _tableViewBottomConstraint]];
|
|
|
|
|
|
|
|
self.navigationItem.title = NSLocalizedStringFromTable(@"room_creation_title", @"Vector", nil);
|
|
|
|
|
2017-01-11 17:58:11 +00:00
|
|
|
// Add each matrix session by default.
|
|
|
|
NSArray *sessions = [AppDelegate theDelegate].mxSessions;
|
|
|
|
for (MXSession *mxSession in sessions)
|
|
|
|
{
|
|
|
|
[self addMatrixSession:mxSession];
|
|
|
|
}
|
2017-07-28 13:02:11 +00:00
|
|
|
|
|
|
|
// Prepare its data source
|
|
|
|
ContactsDataSource *dataSource = [[ContactsDataSource alloc] initWithMatrixSession:self.mainSession]; // TO TEST
|
|
|
|
dataSource.areSectionsShrinkable = YES;
|
|
|
|
dataSource.displaySearchInputInContactsList = YES;
|
|
|
|
dataSource.forceMatrixIdInDisplayName = YES;
|
|
|
|
// Add a plus icon to the contact cell when a search session is in progress,
|
|
|
|
// in order to make it more understandable for the end user.
|
|
|
|
dataSource.contactCellAccessoryImage = [UIImage imageNamed:@"plus_icon"];
|
|
|
|
|
|
|
|
[self displayList:dataSource];
|
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
cancelBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onButtonPressed:)];
|
|
|
|
self.navigationItem.leftBarButtonItem = cancelBarButtonItem;
|
|
|
|
|
|
|
|
createBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(@"start", @"Vector", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPressed:)];
|
|
|
|
self.navigationItem.rightBarButtonItem = createBarButtonItem;
|
|
|
|
|
2017-01-11 17:58:11 +00:00
|
|
|
_searchBarView.placeholder = NSLocalizedStringFromTable(@"room_creation_invite_another_user", @"Vector", nil);
|
2016-09-06 15:37:22 +00:00
|
|
|
_searchBarView.returnKeyType = UIReturnKeyDone;
|
2017-08-11 14:56:09 +00:00
|
|
|
_searchBarView.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
2016-09-06 15:37:22 +00:00
|
|
|
[self refreshSearchBarItemsColor:_searchBarView];
|
|
|
|
|
|
|
|
// Hide line separators of empty cells
|
2017-04-07 14:42:16 +00:00
|
|
|
self.contactsTableView.tableFooterView = [[UIView alloc] init];
|
|
|
|
|
|
|
|
[self.contactsTableView registerClass:ContactTableViewCell.class forCellReuseIdentifier:@"ParticipantTableViewCellId"];
|
2017-01-10 16:56:30 +00:00
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
// Redirect table data source
|
|
|
|
self.contactsTableView.dataSource = self;
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 14:56:09 +00:00
|
|
|
- (void)userInterfaceThemeDidChange
|
|
|
|
{
|
|
|
|
[super userInterfaceThemeDidChange];
|
|
|
|
|
|
|
|
[self refreshSearchBarItemsColor:_searchBarView];
|
|
|
|
|
2019-01-11 10:45:27 +00:00
|
|
|
_searchBarHeaderBorder.backgroundColor = ThemeService.shared.theme.headerBorderColor;
|
2017-08-11 14:56:09 +00:00
|
|
|
|
|
|
|
// Check the table view style to select its bg color.
|
2019-01-11 10:45:27 +00:00
|
|
|
self.contactsTableView.backgroundColor = ((self.contactsTableView.style == UITableViewStylePlain) ? ThemeService.shared.theme.backgroundColor : ThemeService.shared.theme.headerBackgroundColor);
|
2017-08-11 14:56:09 +00:00
|
|
|
self.view.backgroundColor = self.contactsTableView.backgroundColor;
|
2019-02-18 11:53:13 +00:00
|
|
|
self.contactsTableView.separatorColor = ThemeService.shared.theme.lineBreakColor;
|
2017-08-11 14:56:09 +00:00
|
|
|
|
|
|
|
if (self.contactsTableView.dataSource)
|
|
|
|
{
|
|
|
|
[self.contactsTableView reloadData];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIStatusBarStyle)preferredStatusBarStyle
|
|
|
|
{
|
2019-01-11 10:45:27 +00:00
|
|
|
return ThemeService.shared.theme.statusBarStyle;
|
2017-08-11 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
- (void)destroy
|
|
|
|
{
|
|
|
|
if (roomCreationRequest)
|
|
|
|
{
|
|
|
|
[roomCreationRequest cancel];
|
|
|
|
roomCreationRequest = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelBarButtonItem = nil;
|
|
|
|
createBarButtonItem = nil;
|
|
|
|
|
|
|
|
isMultiUseNameByDisplayName = nil;
|
|
|
|
|
|
|
|
participants = nil;
|
2017-01-03 16:52:42 +00:00
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
[super destroy];
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:29:31 +00:00
|
|
|
- (void)addMatrixSession:(MXSession *)mxSession
|
|
|
|
{
|
|
|
|
[super addMatrixSession:mxSession];
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
// FIXME: Handle multi accounts
|
|
|
|
NSString *displayName = NSLocalizedStringFromTable(@"you", @"Vector", nil);
|
|
|
|
userContact = [[MXKContact alloc] initMatrixContactWithDisplayName:displayName andMatrixID:self.mainSession.myUser.userId];
|
|
|
|
|
2017-01-09 18:29:31 +00:00
|
|
|
[self refreshParticipants];
|
|
|
|
}
|
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
|
|
|
// Active the search session if the current participant list is empty
|
|
|
|
if (!participants.count)
|
|
|
|
{
|
|
|
|
self.isAddParticipantSearchBarEditing = YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Refresh display
|
2017-04-07 14:42:16 +00:00
|
|
|
[self refreshContactsTable];
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillDisappear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillDisappear:animated];
|
|
|
|
|
|
|
|
// cancel any pending search
|
|
|
|
[self searchBarCancelButtonClicked:_searchBarView];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-01-09 18:29:31 +00:00
|
|
|
- (void)setIsAddParticipantSearchBarEditing:(BOOL)isAddParticipantSearchBarEditing
|
2016-09-06 15:37:22 +00:00
|
|
|
{
|
2017-01-09 18:29:31 +00:00
|
|
|
if (_isAddParticipantSearchBarEditing != isAddParticipantSearchBarEditing)
|
2016-09-06 15:37:22 +00:00
|
|
|
{
|
2017-01-09 18:29:31 +00:00
|
|
|
if (isAddParticipantSearchBarEditing)
|
2016-09-06 15:37:22 +00:00
|
|
|
{
|
|
|
|
self.navigationItem.rightBarButtonItem = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self.navigationItem.rightBarButtonItem = createBarButtonItem;
|
|
|
|
|
|
|
|
[self refreshParticipants];
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:29:31 +00:00
|
|
|
_isAddParticipantSearchBarEditing = isAddParticipantSearchBarEditing;
|
2016-09-06 15:37:22 +00:00
|
|
|
|
|
|
|
// Switch the display between search result and participants list
|
2017-04-07 14:42:16 +00:00
|
|
|
[self refreshContactsTable];
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Internals
|
|
|
|
|
|
|
|
- (void)refreshParticipants
|
|
|
|
{
|
2017-01-10 16:56:30 +00:00
|
|
|
// Refer all participants in ignored contacts dictionary.
|
2016-09-06 15:37:22 +00:00
|
|
|
isMultiUseNameByDisplayName = [NSMutableDictionary dictionary];
|
2017-04-07 14:42:16 +00:00
|
|
|
[contactsDataSource.ignoredContactsByMatrixId removeAllObjects];
|
|
|
|
[contactsDataSource.ignoredContactsByEmail removeAllObjects];
|
2016-09-06 15:37:22 +00:00
|
|
|
|
|
|
|
for (MXKContact* contact in participants)
|
|
|
|
{
|
2017-01-09 18:29:31 +00:00
|
|
|
NSArray *identifiers = contact.matrixIdentifiers;
|
|
|
|
if (identifiers.count)
|
2016-09-06 15:37:22 +00:00
|
|
|
{
|
2017-01-09 18:29:31 +00:00
|
|
|
// Here the contact can only have one identifier
|
2019-01-07 23:24:11 +00:00
|
|
|
contactsDataSource.ignoredContactsByMatrixId[identifiers.firstObject] = contact;
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 18:29:31 +00:00
|
|
|
NSArray *emails = contact.emailAddresses;
|
|
|
|
if (emails.count)
|
|
|
|
{
|
|
|
|
// Here the contact can only have one email
|
|
|
|
MXKEmail *email = emails.firstObject;
|
2019-01-07 23:24:11 +00:00
|
|
|
contactsDataSource.ignoredContactsByEmail[email.emailAddress] = contact;
|
2017-01-09 18:29:31 +00:00
|
|
|
}
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
isMultiUseNameByDisplayName[contact.displayName] = (isMultiUseNameByDisplayName[contact.displayName] ? @(YES) : @(NO));
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
if (userContact)
|
|
|
|
{
|
2019-01-07 23:24:11 +00:00
|
|
|
contactsDataSource.ignoredContactsByMatrixId[self.mainSession.myUser.userId] = userContact;
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITableView data source
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
|
|
{
|
|
|
|
NSInteger count = 0;
|
|
|
|
|
|
|
|
if (_isAddParticipantSearchBarEditing)
|
|
|
|
{
|
2017-01-10 16:56:30 +00:00
|
|
|
participantsSection = -1;
|
2017-04-07 14:42:16 +00:00
|
|
|
count = [contactsDataSource numberOfSectionsInTableView:tableView];
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
participantsSection = count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
|
|
{
|
|
|
|
NSInteger count = 0;
|
|
|
|
|
2017-01-13 11:08:24 +00:00
|
|
|
if (_isAddParticipantSearchBarEditing)
|
2017-01-09 18:29:31 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
count = [contactsDataSource tableView:tableView numberOfRowsInSection:section];
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
2017-01-13 11:08:24 +00:00
|
|
|
else if (section == participantsSection)
|
2016-09-06 15:37:22 +00:00
|
|
|
{
|
2017-01-13 11:08:24 +00:00
|
|
|
count = participants.count + 1;
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
2017-01-10 16:56:30 +00:00
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-01-10 16:56:30 +00:00
|
|
|
UITableViewCell *cell;
|
2016-09-06 15:37:22 +00:00
|
|
|
|
2017-01-13 11:08:24 +00:00
|
|
|
if (_isAddParticipantSearchBarEditing)
|
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
cell = [contactsDataSource tableView:tableView cellForRowAtIndexPath:indexPath];
|
2017-01-13 11:08:24 +00:00
|
|
|
}
|
|
|
|
else if (indexPath.section == participantsSection)
|
2016-09-06 15:37:22 +00:00
|
|
|
{
|
2017-01-10 16:56:30 +00:00
|
|
|
ContactTableViewCell* participantCell = [tableView dequeueReusableCellWithIdentifier:@"ParticipantTableViewCellId" forIndexPath:indexPath];
|
|
|
|
|
|
|
|
MXKContact *contact;
|
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
if (indexPath.row == 0)
|
|
|
|
{
|
|
|
|
// oneself dedicated cell
|
|
|
|
contact = userContact;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSInteger index = indexPath.row - 1;
|
|
|
|
|
|
|
|
if (index < participants.count)
|
|
|
|
{
|
|
|
|
contact = participants[index];
|
|
|
|
|
|
|
|
// Disambiguate the display name when it appears several times.
|
2017-01-06 14:09:18 +00:00
|
|
|
if (contact.displayName)
|
2016-09-06 15:37:22 +00:00
|
|
|
{
|
2017-01-06 14:09:18 +00:00
|
|
|
participantCell.showMatrixIdInDisplayName = [isMultiUseNameByDisplayName[contact.displayName] isEqualToNumber:@(YES)];
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
participantCell.selectionStyle = UITableViewCellSelectionStyleNone;
|
2017-01-10 16:56:30 +00:00
|
|
|
[participantCell render:contact];
|
2017-01-09 18:29:31 +00:00
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
cell = participantCell;
|
2017-01-09 18:29:31 +00:00
|
|
|
}
|
2017-10-23 13:48:13 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Return a fake cell to prevent app from crashing.
|
|
|
|
cell = [[UITableViewCell alloc] init];
|
|
|
|
}
|
2016-09-06 15:37:22 +00:00
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
return cell;
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
if (indexPath.section == participantsSection && indexPath.row != 0)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
|
|
|
|
{
|
|
|
|
// iOS8 requires this method to enable editing (see editActionsForRowAtIndexPath).
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITableView delegate
|
|
|
|
|
2017-08-11 14:56:09 +00:00
|
|
|
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
|
|
|
|
{
|
2019-01-11 10:45:27 +00:00
|
|
|
cell.backgroundColor = ThemeService.shared.theme.backgroundColor;
|
2017-08-11 14:56:09 +00:00
|
|
|
|
|
|
|
// Update the selected background view
|
2019-01-11 10:45:27 +00:00
|
|
|
if (ThemeService.shared.theme.selectedBackgroundColor)
|
2017-08-11 14:56:09 +00:00
|
|
|
{
|
|
|
|
cell.selectedBackgroundView = [[UIView alloc] init];
|
2019-01-11 10:45:27 +00:00
|
|
|
cell.selectedBackgroundView.backgroundColor = ThemeService.shared.theme.selectedBackgroundColor;
|
2017-08-11 14:56:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tableView.style == UITableViewStylePlain)
|
|
|
|
{
|
|
|
|
cell.selectedBackgroundView = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cell.selectedBackgroundView.backgroundColor = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:29:31 +00:00
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
|
|
|
|
{
|
2017-01-10 16:56:30 +00:00
|
|
|
CGFloat height = 0.0;
|
2017-01-09 18:29:31 +00:00
|
|
|
|
2017-01-13 11:08:24 +00:00
|
|
|
if (_isAddParticipantSearchBarEditing)
|
2017-01-09 18:29:31 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
height = [contactsDataSource heightForHeaderInSection:section];
|
2017-01-09 18:29:31 +00:00
|
|
|
}
|
2017-01-10 16:56:30 +00:00
|
|
|
|
|
|
|
return height;
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
if (_isAddParticipantSearchBarEditing)
|
|
|
|
{
|
|
|
|
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 74;
|
|
|
|
}
|
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-01-13 11:08:24 +00:00
|
|
|
if (_isAddParticipantSearchBarEditing)
|
2017-01-09 18:29:31 +00:00
|
|
|
{
|
2017-01-13 11:08:24 +00:00
|
|
|
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
|
2017-01-09 18:29:31 +00:00
|
|
|
}
|
2017-01-13 11:08:24 +00:00
|
|
|
else
|
2017-01-09 18:29:31 +00:00
|
|
|
{
|
2017-01-13 11:08:24 +00:00
|
|
|
// Do nothing
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
NSMutableArray* actions;
|
|
|
|
|
|
|
|
// add the swipe to delete only on participants sections
|
|
|
|
if (indexPath.section == participantsSection && indexPath.row != 0)
|
|
|
|
{
|
|
|
|
actions = [[NSMutableArray alloc] init];
|
|
|
|
|
|
|
|
// Patch: Force the width of the button by adding whitespace characters into the title string.
|
|
|
|
UITableViewRowAction *leaveAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
|
|
|
|
|
|
|
|
[self onDeleteAt:indexPath];
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
2019-01-11 10:45:27 +00:00
|
|
|
leaveAction.backgroundColor = [MXKTools convertImageToPatternColor:@"remove_icon" backgroundColor:ThemeService.shared.theme.headerBackgroundColor patternSize:CGSizeMake(74, 74) resourceSize:CGSizeMake(24, 24)];
|
2016-09-06 15:37:22 +00:00
|
|
|
[actions insertObject:leaveAction atIndex:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Actions
|
|
|
|
|
|
|
|
- (void)onDeleteAt:(NSIndexPath*)path
|
|
|
|
{
|
|
|
|
NSInteger row = path.row;
|
|
|
|
row --;
|
|
|
|
|
|
|
|
if (row < participants.count)
|
|
|
|
{
|
|
|
|
[participants removeObjectAtIndex:row];
|
|
|
|
|
|
|
|
[self refreshParticipants];
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
[self refreshContactsTable];
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)onButtonPressed:(id)sender
|
|
|
|
{
|
|
|
|
if (sender == createBarButtonItem)
|
|
|
|
{
|
|
|
|
// Disable button to prevent multiple request
|
|
|
|
createBarButtonItem.enabled = NO;
|
|
|
|
[self startActivityIndicator];
|
|
|
|
|
2016-10-19 12:00:10 +00:00
|
|
|
// Prepare the invited participant data
|
|
|
|
NSMutableArray *inviteArray = [NSMutableArray array];
|
|
|
|
NSMutableArray *invite3PIDArray = [NSMutableArray array];
|
|
|
|
|
|
|
|
// Check whether some users must be invited
|
|
|
|
for (MXKContact *contact in participants)
|
|
|
|
{
|
|
|
|
NSArray *identifiers = contact.matrixIdentifiers;
|
|
|
|
if (identifiers.count)
|
|
|
|
{
|
|
|
|
[inviteArray addObject:identifiers.firstObject];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-06 14:09:18 +00:00
|
|
|
// This is a text entered by the user, or a local contact
|
|
|
|
NSString *participantId;
|
|
|
|
|
|
|
|
if (contact.emailAddresses.count)
|
|
|
|
{
|
|
|
|
// This is a local contact, consider the first email by default.
|
|
|
|
// TODO: Prompt the user to select the right email.
|
|
|
|
MXKEmail *email = contact.emailAddresses.firstObject;
|
|
|
|
participantId = email.emailAddress;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This is the text filled by the user.
|
|
|
|
participantId = contact.displayName;
|
|
|
|
}
|
2016-10-19 12:00:10 +00:00
|
|
|
|
|
|
|
// Is it an email or a Matrix user ID?
|
|
|
|
if ([MXTools isEmailAddress:participantId])
|
|
|
|
{
|
|
|
|
// The identity server must be defined
|
|
|
|
if (!self.mainSession.matrixRestClient.identityServer)
|
|
|
|
{
|
|
|
|
MXError *error = [[MXError alloc] initWithErrorCode:kMXSDKErrCodeStringMissingParameters error:@"No supplied identity server URL"];
|
|
|
|
NSLog(@"[StartChatViewController] Invite %@ failed", participantId);
|
|
|
|
// Alert user
|
|
|
|
[[AppDelegate theDelegate] showErrorAsAlert:[error createNSError]];
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The hostname of the identity server must not have the protocol part
|
|
|
|
NSString *identityServer = self.mainSession.matrixRestClient.identityServer;
|
|
|
|
if ([identityServer hasPrefix:@"http://"] || [identityServer hasPrefix:@"https://"])
|
|
|
|
{
|
|
|
|
identityServer = [identityServer substringFromIndex:[identityServer rangeOfString:@"://"].location + 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
MXInvite3PID *invite3PID = [[MXInvite3PID alloc] init];
|
|
|
|
invite3PID.identityServer = identityServer;
|
2017-01-23 17:40:30 +00:00
|
|
|
invite3PID.medium = kMX3PIDMediumEmail;
|
2016-10-19 12:00:10 +00:00
|
|
|
invite3PID.address = participantId;
|
|
|
|
|
|
|
|
[invite3PIDArray addObject:invite3PID];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[inviteArray addObject:participantId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:54:28 +00:00
|
|
|
// Is it a direct chat?
|
2016-11-02 15:35:45 +00:00
|
|
|
BOOL isDirect = ((inviteArray.count + invite3PIDArray.count == 1) ? YES : NO);
|
|
|
|
|
2016-11-02 16:54:28 +00:00
|
|
|
// In case of a direct chat with only one user id, we open the first available direct chat
|
|
|
|
// or creates a new one (if it doesn't exist).
|
|
|
|
if (isDirect && inviteArray.count)
|
|
|
|
{
|
|
|
|
[[AppDelegate theDelegate] startDirectChatWithUserId:inviteArray.firstObject completion:^{
|
|
|
|
|
|
|
|
[self stopActivityIndicator];
|
|
|
|
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ensure direct chat are created with equal ops on both sides (the trusted_private_chat preset)
|
|
|
|
MXRoomPreset preset = (isDirect ? kMXRoomPresetTrustedPrivateChat : nil);
|
|
|
|
|
|
|
|
// Create new room
|
|
|
|
roomCreationRequest = [self.mainSession createRoom:nil
|
|
|
|
visibility:kMXRoomDirectoryVisibilityPrivate
|
|
|
|
roomAlias:nil
|
|
|
|
topic:nil
|
|
|
|
invite:(inviteArray.count ? inviteArray : nil)
|
|
|
|
invite3PID:(invite3PIDArray.count ? invite3PIDArray : nil)
|
|
|
|
isDirect:isDirect
|
|
|
|
preset:preset
|
|
|
|
success:^(MXRoom *room) {
|
|
|
|
|
|
|
|
roomCreationRequest = nil;
|
|
|
|
|
|
|
|
[self stopActivityIndicator];
|
|
|
|
|
2018-07-16 20:30:55 +00:00
|
|
|
[[AppDelegate theDelegate] showRoom:room.roomId andEventId:nil withMatrixSession:self.mainSession];
|
2016-11-02 16:54:28 +00:00
|
|
|
|
|
|
|
} failure:^(NSError *error) {
|
|
|
|
|
|
|
|
createBarButtonItem.enabled = YES;
|
|
|
|
|
|
|
|
roomCreationRequest = nil;
|
|
|
|
[self stopActivityIndicator];
|
|
|
|
|
|
|
|
NSLog(@"[StartChatViewController] Create room failed");
|
|
|
|
|
|
|
|
// Alert user
|
|
|
|
[[AppDelegate theDelegate] showErrorAsAlert:error];
|
|
|
|
|
|
|
|
}];
|
|
|
|
}
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
else if (sender == self.navigationItem.leftBarButtonItem)
|
|
|
|
{
|
|
|
|
// Cancel has been pressed
|
2016-09-08 08:08:25 +00:00
|
|
|
if (_isAddParticipantSearchBarEditing && participants.count)
|
2016-09-06 15:37:22 +00:00
|
|
|
{
|
|
|
|
// Cancel the search process
|
|
|
|
[self searchBarCancelButtonClicked:_searchBarView];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Cancel the new chat creation
|
|
|
|
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UISearchBar delegate
|
|
|
|
|
|
|
|
- (void)refreshSearchBarItemsColor:(UISearchBar *)searchBar
|
|
|
|
{
|
|
|
|
// bar tint color
|
2019-01-11 10:45:27 +00:00
|
|
|
searchBar.barTintColor = searchBar.tintColor = ThemeService.shared.theme.tintColor;
|
|
|
|
searchBar.tintColor = ThemeService.shared.theme.tintColor;
|
2016-09-06 15:37:22 +00:00
|
|
|
|
|
|
|
// FIXME: this all seems incredibly fragile and tied to gutwrenching the current UISearchBar internals.
|
|
|
|
|
|
|
|
// text color
|
|
|
|
UITextField *searchBarTextField = [searchBar valueForKey:@"_searchField"];
|
2019-01-11 10:45:27 +00:00
|
|
|
searchBarTextField.textColor = ThemeService.shared.theme.textSecondaryColor;
|
2016-09-06 15:37:22 +00:00
|
|
|
|
|
|
|
// Magnifying glass icon.
|
|
|
|
UIImageView *leftImageView = (UIImageView *)searchBarTextField.leftView;
|
|
|
|
leftImageView.image = [leftImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
|
2019-01-11 10:45:27 +00:00
|
|
|
leftImageView.tintColor = ThemeService.shared.theme.tintColor;
|
2016-09-06 15:37:22 +00:00
|
|
|
|
|
|
|
// remove the gray background color
|
|
|
|
UIView *effectBackgroundTop = [searchBarTextField valueForKey:@"_effectBackgroundTop"];
|
|
|
|
UIView *effectBackgroundBottom = [searchBarTextField valueForKey:@"_effectBackgroundBottom"];
|
|
|
|
effectBackgroundTop.hidden = YES;
|
|
|
|
effectBackgroundBottom.hidden = YES;
|
|
|
|
|
|
|
|
// place holder
|
2017-08-11 14:56:09 +00:00
|
|
|
if (searchBarTextField.placeholder)
|
|
|
|
{
|
|
|
|
searchBarTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:searchBarTextField.placeholder
|
|
|
|
attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
|
2019-01-11 10:45:27 +00:00
|
|
|
NSUnderlineColorAttributeName: ThemeService.shared.theme.tintColor,
|
|
|
|
NSForegroundColorAttributeName: ThemeService.shared.theme.tintColor}];
|
2017-08-11 14:56:09 +00:00
|
|
|
}
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
|
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
[contactsDataSource searchWithPattern:searchText forceReset:NO];
|
2016-09-06 15:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
|
2017-04-07 14:42:16 +00:00
|
|
|
{
|
2017-01-05 14:44:00 +00:00
|
|
|
self.isAddParticipantSearchBarEditing = YES;
|
|
|
|
searchBar.showsCancelButton = NO;
|
2016-09-06 15:37:22 +00:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
|
|
|
|
{
|
2017-01-10 16:56:30 +00:00
|
|
|
searchBar.text = nil;
|
2016-09-06 15:37:22 +00:00
|
|
|
self.isAddParticipantSearchBarEditing = NO;
|
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
// Reset filtering
|
2017-04-07 14:42:16 +00:00
|
|
|
[contactsDataSource searchWithPattern:nil forceReset:NO];
|
2017-01-10 16:56:30 +00:00
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
// Leave search
|
|
|
|
[searchBar resignFirstResponder];
|
|
|
|
}
|
|
|
|
|
2017-01-13 11:08:24 +00:00
|
|
|
#pragma mark - ContactsTableViewControllerDelegate
|
|
|
|
|
|
|
|
- (void)contactsTableViewController:(ContactsTableViewController *)contactsTableViewController didSelectContact:(MXKContact*)contact
|
|
|
|
{
|
|
|
|
if (contact)
|
|
|
|
{
|
|
|
|
// Update here the mutable list of participants
|
|
|
|
[participants addObject:contact];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh display by leaving search session
|
|
|
|
[self searchBarCancelButtonClicked:_searchBarView];
|
|
|
|
}
|
|
|
|
|
2016-09-06 15:37:22 +00:00
|
|
|
@end
|