2015-12-09 09:12:08 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 OpenMarket Ltd
|
2017-03-23 16:48:05 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2015-12-09 09:12:08 +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 "HomeViewController.h"
|
|
|
|
|
2016-03-17 10:32:18 +00:00
|
|
|
#import "AppDelegate.h"
|
|
|
|
|
2017-04-06 12:27:45 +00:00
|
|
|
#import "RecentsDataSource.h"
|
2015-12-09 09:12:08 +00:00
|
|
|
|
2017-05-05 13:30:35 +00:00
|
|
|
#import "TableViewCellWithCollectionView.h"
|
|
|
|
#import "RoomCollectionViewCell.h"
|
|
|
|
|
|
|
|
@interface HomeViewController ()
|
|
|
|
{
|
|
|
|
RecentsDataSource *recentsDataSource;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2015-12-09 09:12:08 +00:00
|
|
|
@implementation HomeViewController
|
|
|
|
|
2017-01-03 13:40:23 +00:00
|
|
|
- (void)finalizeInit
|
|
|
|
{
|
|
|
|
[super finalizeInit];
|
|
|
|
|
2017-04-06 12:27:45 +00:00
|
|
|
self.screenName = @"Home";
|
2017-01-03 13:40:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 09:12:08 +00:00
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
2016-02-10 21:12:17 +00:00
|
|
|
|
2017-04-06 12:27:45 +00:00
|
|
|
self.view.accessibilityIdentifier = @"HomeVCView";
|
|
|
|
self.recentsTableView.accessibilityIdentifier = @"HomeVCTableView";
|
|
|
|
|
2017-05-19 15:22:26 +00:00
|
|
|
// Tag the recents table with the its recents data source mode.
|
|
|
|
// This will be used by the shared RecentsDataSource instance for sanity checks (see UITableViewDataSource methods).
|
|
|
|
self.recentsTableView.tag = RecentsDataSourceModeHome;
|
|
|
|
|
2017-05-05 15:08:44 +00:00
|
|
|
// Add the (+) button programmatically
|
|
|
|
[self addPlusButton];
|
2017-05-05 13:30:35 +00:00
|
|
|
|
|
|
|
// Register table view cell used for rooms collection.
|
|
|
|
[self.recentsTableView registerClass:TableViewCellWithCollectionView.class forCellReuseIdentifier:TableViewCellWithCollectionView.defaultReuseIdentifier];
|
|
|
|
|
|
|
|
// Change the table data source. It must be the home view controller itself.
|
|
|
|
self.recentsTableView.dataSource = self;
|
2015-12-09 09:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillAppear:animated];
|
2016-06-16 16:06:05 +00:00
|
|
|
|
2017-04-06 13:49:47 +00:00
|
|
|
[AppDelegate theDelegate].masterTabBarController.navigationItem.title = NSLocalizedStringFromTable(@"title_home", @"Vector", nil);
|
|
|
|
|
2017-05-05 13:30:35 +00:00
|
|
|
if (recentsDataSource)
|
2015-12-09 13:39:37 +00:00
|
|
|
{
|
2017-04-06 12:27:45 +00:00
|
|
|
// Take the lead on the shared data source.
|
2017-05-05 13:30:35 +00:00
|
|
|
recentsDataSource.areSectionsShrinkable = NO;
|
2017-04-06 12:27:45 +00:00
|
|
|
[recentsDataSource setDelegate:self andRecentsDataSourceMode:RecentsDataSourceModeHome];
|
2016-10-04 16:19:31 +00:00
|
|
|
}
|
2017-06-07 15:52:12 +00:00
|
|
|
|
|
|
|
[self moveAllCollectionsToLeft];
|
2015-12-09 13:39:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 12:27:45 +00:00
|
|
|
- (void)dealloc
|
2015-12-09 09:12:08 +00:00
|
|
|
{
|
2017-04-06 12:27:45 +00:00
|
|
|
|
2015-12-09 09:12:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 12:27:45 +00:00
|
|
|
- (void)destroy
|
2016-06-16 16:06:05 +00:00
|
|
|
{
|
2017-04-06 12:27:45 +00:00
|
|
|
[super destroy];
|
2016-06-16 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 15:52:12 +00:00
|
|
|
- (void)moveAllCollectionsToLeft
|
|
|
|
{
|
|
|
|
// Scroll all rooms collections to their beginning
|
|
|
|
for (NSInteger section = 0; section < [self numberOfSectionsInTableView:self.recentsTableView]; section++)
|
|
|
|
{
|
|
|
|
UITableViewCell *firstSectionCell = [self.recentsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
|
|
|
|
if (firstSectionCell && [firstSectionCell isKindOfClass:TableViewCellWithCollectionView.class])
|
|
|
|
{
|
|
|
|
TableViewCellWithCollectionView *tableViewCell = (TableViewCellWithCollectionView*)firstSectionCell;
|
|
|
|
|
|
|
|
if ([tableViewCell.collectionView numberOfItemsInSection:0] > 0)
|
|
|
|
{
|
|
|
|
[tableViewCell.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:51:09 +00:00
|
|
|
#pragma mark - Override RecentsViewController
|
2015-12-09 09:12:08 +00:00
|
|
|
|
2017-05-05 13:30:35 +00:00
|
|
|
- (void)displayList:(MXKRecentsDataSource *)listDataSource
|
|
|
|
{
|
|
|
|
[super displayList:listDataSource];
|
|
|
|
|
|
|
|
// Change the table data source. It must be the home view controller itself.
|
|
|
|
self.recentsTableView.dataSource = self;
|
|
|
|
|
|
|
|
// Keep a ref on the recents data source
|
|
|
|
if ([listDataSource isKindOfClass:RecentsDataSource.class])
|
|
|
|
{
|
|
|
|
recentsDataSource = (RecentsDataSource*)listDataSource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:51:09 +00:00
|
|
|
- (void)refreshCurrentSelectedCell:(BOOL)forceVisible
|
|
|
|
{
|
|
|
|
// Check whether the recents data source is correctly configured.
|
2017-05-05 13:30:35 +00:00
|
|
|
if (recentsDataSource.recentsDataSourceMode != RecentsDataSourceModeHome)
|
2017-05-03 16:51:09 +00:00
|
|
|
{
|
2017-05-05 13:30:35 +00:00
|
|
|
return;
|
2017-05-03 16:51:09 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 13:30:35 +00:00
|
|
|
// TODO: refreshCurrentSelectedCell
|
|
|
|
//[super refreshCurrentSelectedCell:forceVisible];
|
2017-05-03 16:51:09 +00:00
|
|
|
}
|
2015-12-09 09:12:08 +00:00
|
|
|
|
2017-05-05 15:08:44 +00:00
|
|
|
- (void)didTapOnSectionHeader:(UIGestureRecognizer*)gestureRecognizer
|
|
|
|
{
|
|
|
|
UIView *view = gestureRecognizer.view;
|
|
|
|
NSInteger section = view.tag;
|
|
|
|
|
2017-05-12 10:18:29 +00:00
|
|
|
// Scroll to the top this section
|
|
|
|
if ([self.recentsTableView numberOfRowsInSection:section] > 0)
|
|
|
|
{
|
|
|
|
[self.recentsTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
|
|
|
|
}
|
|
|
|
|
2017-05-05 15:08:44 +00:00
|
|
|
// Scroll to the beginning the corresponding rooms collection.
|
|
|
|
UITableViewCell *firstSectionCell = [self.recentsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
|
|
|
|
if (firstSectionCell && [firstSectionCell isKindOfClass:TableViewCellWithCollectionView.class])
|
|
|
|
{
|
|
|
|
TableViewCellWithCollectionView *tableViewCell = (TableViewCellWithCollectionView*)firstSectionCell;
|
|
|
|
|
|
|
|
if ([tableViewCell.collectionView numberOfItemsInSection:0] > 0)
|
|
|
|
{
|
|
|
|
[tableViewCell.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 13:30:35 +00:00
|
|
|
#pragma mark - UITableViewDataSource
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
|
|
{
|
|
|
|
// Return the actual number of sections prepared in recents dataSource.
|
|
|
|
return [recentsDataSource numberOfSectionsInTableView:tableView];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
|
|
{
|
|
|
|
// Each rooms section is represented by only one collection view.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-05-12 15:39:05 +00:00
|
|
|
if ((indexPath.section == recentsDataSource.conversationSection && !recentsDataSource.conversationCellDataArray.count)
|
|
|
|
|| (indexPath.section == recentsDataSource.peopleSection && !recentsDataSource.peopleCellDataArray.count))
|
2017-05-05 13:30:35 +00:00
|
|
|
{
|
2017-05-12 15:39:05 +00:00
|
|
|
return [recentsDataSource tableView:tableView cellForRowAtIndexPath:indexPath];
|
2017-05-05 13:30:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 15:08:44 +00:00
|
|
|
TableViewCellWithCollectionView *tableViewCell = [tableView dequeueReusableCellWithIdentifier:TableViewCellWithCollectionView.defaultReuseIdentifier forIndexPath:indexPath];
|
|
|
|
tableViewCell.collectionView.tag = indexPath.section;
|
|
|
|
[tableViewCell.collectionView registerClass:RoomCollectionViewCell.class forCellWithReuseIdentifier:RoomCollectionViewCell.defaultReuseIdentifier];
|
|
|
|
tableViewCell.collectionView.delegate = self;
|
|
|
|
tableViewCell.collectionView.dataSource = self;
|
|
|
|
tableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
|
2017-05-05 13:30:35 +00:00
|
|
|
|
2017-05-05 15:08:44 +00:00
|
|
|
return tableViewCell;
|
2017-05-05 13:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITableView delegate
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-05-12 15:39:05 +00:00
|
|
|
if ((indexPath.section == recentsDataSource.conversationSection && !recentsDataSource.conversationCellDataArray.count)
|
|
|
|
|| (indexPath.section == recentsDataSource.peopleSection && !recentsDataSource.peopleCellDataArray.count))
|
2017-05-05 13:30:35 +00:00
|
|
|
{
|
2017-05-12 15:39:05 +00:00
|
|
|
return [recentsDataSource cellHeightAtIndexPath:indexPath];
|
2017-05-05 13:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the fixed height of the collection view cell used to display a room.
|
|
|
|
return [RoomCollectionViewCell defaultCellSize].height;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UICollectionViewDataSource
|
|
|
|
|
|
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
|
|
|
|
{
|
|
|
|
return [recentsDataSource tableView:self.recentsTableView numberOfRowsInSection:collectionView.tag];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
RoomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RoomCollectionViewCell.defaultReuseIdentifier
|
|
|
|
forIndexPath:indexPath];
|
|
|
|
|
|
|
|
id<MXKRecentCellDataStoring> cellData = [recentsDataSource cellDataAtIndexPath:[NSIndexPath indexPathForRow:indexPath.item inSection:collectionView.tag]];
|
|
|
|
|
|
|
|
if (cellData)
|
|
|
|
{
|
|
|
|
[cell render:cellData];
|
|
|
|
cell.tag = indexPath.item;
|
|
|
|
|
|
|
|
//TODO: add long tap gesture recognizer.
|
|
|
|
// UILongPressGestureRecognizer *cellLongPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onCollectionViewCellLongPress:)];
|
|
|
|
// [cell addGestureRecognizer:cellLongPressGesture];
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UICollectionViewDelegate
|
|
|
|
|
|
|
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
if (self.delegate)
|
|
|
|
{
|
|
|
|
id<MXKRecentCellDataStoring> cellData = [recentsDataSource cellDataAtIndexPath:[NSIndexPath indexPathForRow:indexPath.item inSection:collectionView.tag]];
|
|
|
|
|
|
|
|
[self.delegate recentListViewController:self didSelectRoom:cellData.roomSummary.roomId inMatrixSession:cellData.roomSummary.room.mxSession];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide the keyboard when user select a room
|
|
|
|
// do not hide the searchBar until the view controller disappear
|
|
|
|
// on tablets / iphone 6+, the user could expect to search again while looking at a room
|
|
|
|
[self.recentsSearchBar resignFirstResponder];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UICollectionViewDelegateFlowLayout
|
|
|
|
|
|
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
return [RoomCollectionViewCell defaultCellSize];
|
|
|
|
}
|
|
|
|
|
2015-12-09 09:12:08 +00:00
|
|
|
@end
|