2017-01-10 16:56:30 +00:00
|
|
|
/*
|
2017-02-06 08:26:57 +00:00
|
|
|
Copyright 2017 OpenMarket Ltd
|
2017-02-03 16:44:28 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2017-01-10 16:56:30 +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 "ContactsTableViewController.h"
|
|
|
|
|
2017-03-09 10:50:12 +00:00
|
|
|
#import "UIViewController+RiotSearch.h"
|
2017-01-13 01:24:50 +00:00
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
#import "AppDelegate.h"
|
|
|
|
|
2017-01-26 22:36:26 +00:00
|
|
|
#define CONTACTS_TABLEVC_LOCALCONTACTS_BITWISE 0x01
|
|
|
|
#define CONTACTS_TABLEVC_KNOWNCONTACTS_BITWISE 0x02
|
|
|
|
|
|
|
|
#define CONTACTS_TABLEVC_DEFAULT_SECTION_HEADER_HEIGHT 30.0
|
|
|
|
#define CONTACTS_TABLEVC_LOCALCONTACTS_SECTION_HEADER_HEIGHT 65.0
|
2017-01-13 16:36:34 +00:00
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
@interface ContactsTableViewController ()
|
|
|
|
{
|
|
|
|
// Observe kAppDelegateDidTapStatusBarNotification to handle tap on clock status bar.
|
|
|
|
id kAppDelegateDidTapStatusBarNotificationObserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation ContactsTableViewController
|
|
|
|
|
2017-01-13 01:24:50 +00:00
|
|
|
#pragma mark - Class methods
|
|
|
|
|
|
|
|
+ (UINib *)nib
|
|
|
|
{
|
|
|
|
return [UINib nibWithNibName:NSStringFromClass([ContactsTableViewController class])
|
|
|
|
bundle:[NSBundle bundleForClass:[ContactsTableViewController class]]];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype)contactsTableViewController
|
|
|
|
{
|
|
|
|
return [[[self class] alloc] initWithNibName:NSStringFromClass([ContactsTableViewController class])
|
|
|
|
bundle:[NSBundle bundleForClass:[ContactsTableViewController class]]];
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
- (void)finalizeInit
|
|
|
|
{
|
|
|
|
[super finalizeInit];
|
|
|
|
|
|
|
|
// Setup `MXKViewControllerHandling` properties
|
2017-03-08 15:14:41 +00:00
|
|
|
self.defaultBarTintColor = kRiotNavBarTintColor;
|
2017-01-10 16:56:30 +00:00
|
|
|
self.enableBarTintColorStatusChange = NO;
|
|
|
|
self.rageShakeManager = [RageShakeManager sharedManager];
|
|
|
|
|
2017-04-06 12:27:45 +00:00
|
|
|
_screenName = @"ContactsTable";
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
|
|
|
// Do any additional setup after loading the view, typically from a nib.
|
2017-01-13 01:24:50 +00:00
|
|
|
|
|
|
|
// Check whether the view controller has been pushed via storyboard
|
2017-04-07 14:42:16 +00:00
|
|
|
if (!self.contactsTableView)
|
2017-01-13 01:24:50 +00:00
|
|
|
{
|
|
|
|
// Instantiate view controller objects
|
|
|
|
[[[self class] nib] instantiateWithOwner:self options:nil];
|
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
// Finalize table view configuration
|
|
|
|
self.contactsTableView.delegate = self;
|
|
|
|
self.contactsTableView.dataSource = contactsDataSource; // Note: dataSource may be nil here
|
|
|
|
|
|
|
|
[self.contactsTableView registerClass:ContactTableViewCell.class forCellReuseIdentifier:ContactTableViewCell.defaultReuseIdentifier];
|
2017-04-06 12:27:45 +00:00
|
|
|
|
2017-01-13 01:24:50 +00:00
|
|
|
// Hide line separators of empty cells
|
2017-04-07 14:42:16 +00:00
|
|
|
self.contactsTableView.tableFooterView = [[UIView alloc] init];
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didReceiveMemoryWarning
|
|
|
|
{
|
|
|
|
[super didReceiveMemoryWarning];
|
|
|
|
// Dispose of any resources that can be recreated.
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)destroy
|
|
|
|
{
|
|
|
|
[super destroy];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
2017-01-13 01:24:50 +00:00
|
|
|
// Screen tracking (via Google Analytics)
|
|
|
|
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
|
|
|
|
if (tracker)
|
|
|
|
{
|
2017-04-06 12:27:45 +00:00
|
|
|
[tracker set:kGAIScreenName value:_screenName];
|
2017-01-13 01:24:50 +00:00
|
|
|
[tracker send:[[GAIDictionaryBuilder createScreenView] build]];
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
// Observe kAppDelegateDidTapStatusBarNotification.
|
|
|
|
kAppDelegateDidTapStatusBarNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kAppDelegateDidTapStatusBarNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
[self.contactsTableView setContentOffset:CGPointMake(-self.contactsTableView.contentInset.left, -self.contactsTableView.contentInset.top) animated:YES];
|
2017-01-10 16:56:30 +00:00
|
|
|
|
|
|
|
}];
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
[self refreshContactsTable];
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillDisappear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillDisappear:animated];
|
|
|
|
|
|
|
|
if (kAppDelegateDidTapStatusBarNotificationObserver)
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:kAppDelegateDidTapStatusBarNotificationObserver];
|
|
|
|
kAppDelegateDidTapStatusBarNotificationObserver = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
- (void)displayList:(ContactsDataSource*)listDataSource
|
2017-01-12 16:30:09 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// Cancel registration on existing dataSource if any
|
|
|
|
if (contactsDataSource)
|
2017-01-12 16:30:09 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
contactsDataSource.delegate = nil;
|
2017-01-12 16:30:09 +00:00
|
|
|
}
|
2017-01-10 16:56:30 +00:00
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
contactsDataSource = listDataSource;
|
|
|
|
contactsDataSource.delegate = self;
|
2017-01-10 16:56:30 +00:00
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
if (self.contactsTableView)
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// Set up table data source
|
|
|
|
self.contactsTableView.dataSource = contactsDataSource;
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
- (void)refreshContactsTable
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
[self.contactsTableView reloadData];
|
2017-01-10 16:56:30 +00:00
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
if (_shouldScrollToTopOnRefresh)
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
[self scrollToTop:NO];
|
|
|
|
_shouldScrollToTopOnRefresh = NO;
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
// In case of split view controller where the primary and secondary view controllers are displayed side-by-side on screen,
|
|
|
|
// the selected room (if any) is updated and kept visible.
|
|
|
|
// Note: 'isCollapsed' property is available in UISplitViewController for iOS 8 and later.
|
|
|
|
if (self.splitViewController && (![self.splitViewController respondsToSelector:@selector(isCollapsed)] || !self.splitViewController.isCollapsed))
|
|
|
|
{
|
|
|
|
[self refreshCurrentSelectedCell:YES];
|
|
|
|
}
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
- (void)refreshCurrentSelectedCell:(BOOL)forceVisible
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// Update here the index of the current selected cell (if any) - Useful in landscape mode with split view controller.
|
|
|
|
NSIndexPath *currentSelectedCellIndexPath = nil;
|
|
|
|
MasterTabBarController *masterTabBarController = [AppDelegate theDelegate].masterTabBarController;
|
|
|
|
if (masterTabBarController.currentContactDetailViewController)
|
|
|
|
{
|
|
|
|
// Look for the rank of this selected contact in displayed recents
|
|
|
|
currentSelectedCellIndexPath = [contactsDataSource cellIndexPathWithContact:masterTabBarController.selectedContact];
|
|
|
|
}
|
2017-01-10 16:56:30 +00:00
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
if (currentSelectedCellIndexPath)
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// Select the right row
|
|
|
|
[self.contactsTableView selectRowAtIndexPath:currentSelectedCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
|
|
|
|
|
|
|
|
if (forceVisible)
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// Scroll table view to make the selected row appear at second position
|
|
|
|
NSInteger topCellIndexPathRow = currentSelectedCellIndexPath.row ? currentSelectedCellIndexPath.row - 1: currentSelectedCellIndexPath.row;
|
|
|
|
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:topCellIndexPathRow inSection:currentSelectedCellIndexPath.section];
|
|
|
|
[self.contactsTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
2017-04-07 14:42:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSIndexPath *indexPath = [self.contactsTableView indexPathForSelectedRow];
|
|
|
|
if (indexPath)
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
[self.contactsTableView deselectRowAtIndexPath:indexPath animated:NO];
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
#pragma mark - MXKDataSourceDelegate
|
2017-01-10 16:56:30 +00:00
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
- (Class<MXKCellRendering>)cellViewClassForCellData:(MXKCellData*)cellData
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
if ([cellData isKindOfClass:MXKContact.class])
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
return ContactTableViewCell.class;
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
2017-01-13 16:36:34 +00:00
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
return nil;
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
- (NSString *)cellReuseIdentifierForCellData:(MXKCellData*)cellData
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
if ([cellData isKindOfClass:MXKContact.class])
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
return [ContactTableViewCell defaultReuseIdentifier];
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
return nil;
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
- (void)dataSource:(MXKDataSource *)dataSource didCellChange:(id)changes
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
[self refreshContactsTable];
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
#pragma mark - Internal methods
|
|
|
|
|
|
|
|
- (void)scrollToTop:(BOOL)animated
|
2017-01-10 16:56:30 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// Scroll to the top
|
|
|
|
[self.contactsTableView setContentOffset:CGPointMake(-self.contactsTableView.contentInset.left, -self.contactsTableView.contentInset.top) animated:animated];
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITableView delegate
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
|
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
return [contactsDataSource heightForHeaderInSection:section];
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
|
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
return [contactsDataSource viewForHeaderInSection:section withFrame:[tableView rectForHeaderInSection:section]];
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
if ([contactsDataSource contactAtIndexPath:indexPath])
|
2017-02-08 12:54:58 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// Return the default height of the contact cell
|
|
|
|
return 74.0;
|
2017-02-08 12:54:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
return 50;
|
2017-01-10 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-01-12 16:30:09 +00:00
|
|
|
if (self.contactsTableViewControllerDelegate)
|
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
MXKContact *mxkContact = [contactsDataSource contactAtIndexPath:indexPath];
|
2017-01-12 16:30:09 +00:00
|
|
|
|
|
|
|
if (mxkContact)
|
|
|
|
{
|
|
|
|
[self.contactsTableViewControllerDelegate contactsTableViewController:self didSelectContact:mxkContact];
|
2017-04-07 14:42:16 +00:00
|
|
|
|
|
|
|
// Keep selected the cell by default.
|
|
|
|
return;
|
2017-01-12 16:30:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Else do nothing by default - `ContactsTableViewController-inherited` instance must override this method.
|
2017-01-10 16:56:30 +00:00
|
|
|
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
}
|
|
|
|
|
2017-01-13 01:24:50 +00:00
|
|
|
#pragma mark - UISearchBar delegate
|
|
|
|
|
|
|
|
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
|
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
[contactsDataSource searchWithPattern:searchText forceReset:NO];
|
2017-01-13 01:24:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
|
|
|
|
{
|
|
|
|
// "Done" key has been pressed.
|
|
|
|
|
2017-04-07 14:42:16 +00:00
|
|
|
if (self.contactsTableViewControllerDelegate)
|
2017-01-13 01:24:50 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// Check whether the current search input is a valid email or a Matrix user ID
|
|
|
|
MXKContact* filedContact = [contactsDataSource searchInputContact];
|
|
|
|
if (filedContact)
|
2017-01-13 01:24:50 +00:00
|
|
|
{
|
2017-04-07 14:42:16 +00:00
|
|
|
// Select the contact related to the search input, rather than having to hit +
|
|
|
|
[self.contactsTableViewControllerDelegate contactsTableViewController:self didSelectContact:filedContact];
|
2017-01-13 01:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dismiss keyboard
|
|
|
|
[searchBar resignFirstResponder];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
|
|
|
|
{
|
|
|
|
searchBar.text = nil;
|
|
|
|
|
|
|
|
// Reset filtering
|
2017-04-07 14:42:16 +00:00
|
|
|
[contactsDataSource searchWithPattern:nil forceReset:NO];
|
2017-01-13 01:24:50 +00:00
|
|
|
|
|
|
|
// Leave search
|
|
|
|
[searchBar resignFirstResponder];
|
|
|
|
|
|
|
|
[self withdrawViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:56:30 +00:00
|
|
|
@end
|