talk-ios/NextcloudTalk/RoomCreationTableViewContro...

410 строки
17 KiB
Objective-C

/**
* @copyright Copyright (c) 2020 Ivan Sein <ivan@nextcloud.com>
*
* @author Ivan Sein <ivan@nextcloud.com>
*
* @license GNU GPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#import "RoomCreationTableViewController.h"
#import "NCAPIController.h"
#import "NCAppBranding.h"
#import "PlaceholderView.h"
#import "ResultMultiSelectionTableViewController.h"
#import "RoomCreation2TableViewController.h"
#import "UIImageView+AFNetworking.h"
#import "NCUtils.h"
@interface RoomCreationTableViewController () <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
{
NSMutableDictionary *_participants;
NSArray *_indexes;
UISearchController *_searchController;
ResultMultiSelectionTableViewController *_resultTableViewController;
NSMutableArray *_selectedParticipants;
PlaceholderView *_roomCreationBackgroundView;
NSTimer *_searchTimer;
NSURLSessionTask *_searchParticipantsTask;
}
@end
@implementation RoomCreationTableViewController
- (instancetype)init
{
return [self initWithParticipants:nil andIndexes:nil];
}
- (instancetype)initWithParticipants:(NSMutableDictionary *)participants andIndexes:(NSMutableArray *)indexes
{
self = [super init];
if (self) {
_participants = participants ? participants : [[NSMutableDictionary alloc] init];
_indexes = indexes ? indexes : [[NSArray alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_selectedParticipants = [[NSMutableArray alloc] init];
[self.tableView registerNib:[UINib nibWithNibName:kContactsTableCellNibName bundle:nil] forCellReuseIdentifier:kContactCellIdentifier];
self.tableView.separatorInset = UIEdgeInsetsMake(0, 72, 0, 0);
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
_resultTableViewController = [[ResultMultiSelectionTableViewController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:_resultTableViewController];
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
if (@available(iOS 13.0, *)) {
UIColor *themeColor = [NCAppBranding themeColor];
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
[appearance configureWithOpaqueBackground];
appearance.backgroundColor = themeColor;
appearance.titleTextAttributes = @{NSForegroundColorAttributeName:[NCAppBranding themeTextColor]};
self.navigationItem.standardAppearance = appearance;
self.navigationItem.compactAppearance = appearance;
self.navigationItem.scrollEdgeAppearance = appearance;
self.navigationItem.searchController = _searchController;
self.navigationItem.searchController.searchBar.searchTextField.backgroundColor = [NCUtils searchbarBGColorForColor:themeColor];
_searchController.searchBar.tintColor = [NCAppBranding themeTextColor];
UITextField *searchTextField = [_searchController.searchBar valueForKey:@"searchField"];
UIButton *clearButton = [searchTextField valueForKey:@"_clearButton"];
searchTextField.tintColor = [NCAppBranding themeTextColor];
searchTextField.textColor = [NCAppBranding themeTextColor];
dispatch_async(dispatch_get_main_queue(), ^{
// Search bar placeholder
searchTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Search", nil)
attributes:@{NSForegroundColorAttributeName:[[NCAppBranding themeTextColor] colorWithAlphaComponent:0.5]}];
// Search bar search icon
UIImageView *searchImageView = (UIImageView *)searchTextField.leftView;
searchImageView.image = [searchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[searchImageView setTintColor:[[NCAppBranding themeTextColor] colorWithAlphaComponent:0.5]];
// Search bar search clear button
UIImage *clearButtonImage = [clearButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[clearButton setImage:clearButtonImage forState:UIControlStateNormal];
[clearButton setImage:clearButtonImage forState:UIControlStateHighlighted];
[clearButton setTintColor:[NCAppBranding themeTextColor]];
});
} else if (@available(iOS 11.0, *)) {
self.navigationItem.searchController = _searchController;
_searchController.searchBar.tintColor = [NCAppBranding themeTextColor];
UITextField *searchTextField = [_searchController.searchBar valueForKey:@"searchField"];
searchTextField.tintColor = [NCAppBranding themeColor];
UIView *backgroundview = [searchTextField.subviews firstObject];
backgroundview.backgroundColor = [UIColor whiteColor];
backgroundview.layer.cornerRadius = 8;
backgroundview.clipsToBounds = YES;
} else {
self.tableView.tableHeaderView = _searchController.searchBar;
_searchController.searchBar.barTintColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.96 alpha:1.0]; //efeff4
_searchController.searchBar.layer.borderWidth = 1;
_searchController.searchBar.layer.borderColor = [[UIColor colorWithRed:0.94 green:0.94 blue:0.96 alpha:1.0] CGColor];
}
// We want ourselves to be the delegate for the result table so didSelectRowAtIndexPath is called for both tables.
_resultTableViewController.tableView.delegate = self;
_searchController.delegate = self;
_searchController.searchBar.delegate = self;
_searchController.hidesNavigationBarDuringPresentation = NO;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// Room creation placeholder view
_roomCreationBackgroundView = [[PlaceholderView alloc] init];
[_roomCreationBackgroundView.placeholderImage setImage:[UIImage imageNamed:@"contacts-placeholder"]];
[_roomCreationBackgroundView.placeholderText setText:NSLocalizedString(@"No participants found", nil)];
[_roomCreationBackgroundView.placeholderView setHidden:YES];
[_roomCreationBackgroundView.loadingView startAnimating];
self.tableView.backgroundView = _roomCreationBackgroundView;
self.definesPresentationContext = YES;
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Next", nil) style:UIBarButtonItemStylePlain
target:self action:@selector(nextButtonPressed)];
self.navigationItem.rightBarButtonItem = nextButton;
self.navigationItem.rightBarButtonItem.accessibilityHint = NSLocalizedString(@"Continue to next step of conversation creation", nil);
self.navigationController.navigationBar.tintColor = [NCAppBranding themeTextColor];
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [NCAppBranding themeColor];
[self updateCounter];
// Fix uisearchcontroller animation
self.extendedLayoutIncludesOpaqueBars = YES;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (@available(iOS 11.0, *)) {
self.navigationItem.hidesSearchBarWhenScrolling = NO;
}
[self getPossibleParticipants];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (@available(iOS 11.0, *)) {
self.navigationItem.hidesSearchBarWhenScrolling = NO;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View controller actions
- (void)cancelButtonPressed
{
[self close];
}
- (void)close
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)nextButtonPressed
{
RoomCreation2TableViewController *rc2VC = [[RoomCreation2TableViewController alloc] initForGroupRoomWithParticipants:_selectedParticipants];
[self.navigationController pushViewController:rc2VC animated:YES];
}
- (void)updateCounter
{
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [NCAppBranding themeTextColor];
titleLabel.font = [UIFont boldSystemFontOfSize:16];
titleLabel.text = NSLocalizedString(@"New group conversation", nil);
titleLabel.accessibilityLabel = NSLocalizedString(@"Add participants to new group conversation", nil);
[titleLabel sizeToFit];
UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 18, 0, 0)];
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [NCAppBranding themeTextColor];
subTitleLabel.font = [UIFont systemFontOfSize:12];
subTitleLabel.text = (_selectedParticipants.count == 1) ? NSLocalizedString(@"1 participant", nil) : [NSString stringWithFormat:NSLocalizedString(@"%ld participants", nil), _selectedParticipants.count];
subTitleLabel.accessibilityLabel = [NSString stringWithFormat:NSLocalizedString(@"%@ added to this new group conversation", nil), subTitleLabel.text];
[subTitleLabel sizeToFit];
UIView *twoLineTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAX(subTitleLabel.frame.size.width, titleLabel.frame.size.width), 30)];
[twoLineTitleView addSubview:titleLabel];
[twoLineTitleView addSubview:subTitleLabel];
float widthDiff = subTitleLabel.frame.size.width - titleLabel.frame.size.width;
if (widthDiff > 0) {
CGRect frame = titleLabel.frame;
frame.origin.x = widthDiff / 2;
titleLabel.frame = CGRectIntegral(frame);
}else{
CGRect frame = subTitleLabel.frame;
frame.origin.x = fabsf(widthDiff) / 2;
subTitleLabel.frame = CGRectIntegral(frame);
}
self.navigationItem.titleView = twoLineTitleView;
}
#pragma mark - Participants actions
- (void)getPossibleParticipants
{
[[NCAPIController sharedInstance] getContactsForAccount:[[NCDatabaseManager sharedInstance] activeAccount] forRoom:nil groupRoom:YES withSearchParam:nil andCompletionBlock:^(NSArray *indexes, NSMutableDictionary *contacts, NSMutableArray *contactList, NSError *error) {
if (!error) {
_participants = contacts;
_indexes = [NSMutableArray arrayWithArray:indexes];
[_roomCreationBackgroundView.loadingView stopAnimating];
[_roomCreationBackgroundView.loadingView setHidden:YES];
[_roomCreationBackgroundView.placeholderView setHidden:(contacts.count > 0)];
[self.tableView reloadData];
} else {
NSLog(@"Error while trying to get participants: %@", error);
}
}];
}
- (void)searchForParticipantsWithString:(NSString *)searchString
{
[_searchParticipantsTask cancel];
_searchParticipantsTask = [[NCAPIController sharedInstance] getContactsForAccount:[[NCDatabaseManager sharedInstance] activeAccount] forRoom:nil groupRoom:YES withSearchParam:searchString andCompletionBlock:^(NSArray *indexes, NSMutableDictionary *contacts, NSMutableArray *contactList, NSError *error) {
if (!error) {
[_resultTableViewController setSearchResultContacts:contacts withIndexes:indexes];
} else {
if (error.code != -999) {
NSLog(@"Error while searching for participants: %@", error);
}
}
}];
}
- (BOOL)isParticipantAlreadySelected:(NCUser *)participant
{
for (NCUser *user in _selectedParticipants) {
if ([user.userId isEqualToString:participant.userId] &&
[user.source isEqualToString:participant.source]) {
return YES;
}
}
return NO;
}
- (void)removeSelectedParticipant:(NCUser *)participant
{
NCUser *userToDelete = nil;
for (NCUser *user in _selectedParticipants) {
if ([user.userId isEqualToString:participant.userId] &&
[user.source isEqualToString:participant.source]) {
userToDelete = user;
}
}
if (userToDelete) {
[_selectedParticipants removeObject:userToDelete];
}
}
#pragma mark - Search controller
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
[_searchTimer invalidate];
_searchTimer = nil;
[_resultTableViewController showSearchingUI];
dispatch_async(dispatch_get_main_queue(), ^{
_searchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(searchForParticipants) userInfo:nil repeats:NO];
});
}
- (void)searchForParticipants
{
NSString *searchString = _searchController.searchBar.text;
if (![searchString isEqualToString:@""]) {
[self searchForParticipantsWithString:searchString];
}
}
- (void)didDismissSearchController:(UISearchController *)searchController
{
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _indexes.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *index = [_indexes objectAtIndex:section];
NSArray *participants = [_participants objectForKey:index];
return participants.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return kContactsTableCellHeight;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_indexes objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _indexes;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *index = [_indexes objectAtIndex:indexPath.section];
NSArray *participants = [_participants objectForKey:index];
NCUser *participant = [participants objectAtIndex:indexPath.row];
ContactsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kContactCellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[ContactsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kContactCellIdentifier];
}
cell.labelTitle.text = participant.name;
if ([participant.source isEqualToString:kParticipantTypeUser]) {
[cell.contactImage setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:participant.userId andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
placeholderImage:nil success:nil failure:nil];
} else if ([participant.source isEqualToString:kParticipantTypeEmail]) {
[cell.contactImage setImage:[UIImage imageNamed:@"mail-bg"]];
} else {
[cell.contactImage setImage:[UIImage imageNamed:@"group-bg"]];
}
UIImageView *checkboxChecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox-checked"]];
UIImageView *checkboxUnchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox-unchecked"]];
cell.accessoryView = ([self isParticipantAlreadySelected:participant]) ? checkboxChecked : checkboxUnchecked;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *index = nil;
NSArray *participants = nil;
if (_searchController.active) {
index = [_resultTableViewController.indexes objectAtIndex:indexPath.section];
participants = [_resultTableViewController.contacts objectForKey:index];
} else {
index = [_indexes objectAtIndex:indexPath.section];
participants = [_participants objectForKey:index];
}
NCUser *participant = [participants objectAtIndex:indexPath.row];
if (![self isParticipantAlreadySelected:participant]) {
[_selectedParticipants addObject:participant];
} else {
[self removeSelectedParticipant:participant];
}
_resultTableViewController.selectedParticipants = _selectedParticipants;
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
[self updateCounter];
}
@end