2017-04-07 08:38:05 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations 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 "DirectoryServerPickerViewController.h"
|
|
|
|
#import "DirectoryServerTableViewCell.h"
|
2017-05-03 07:31:10 +00:00
|
|
|
#import "DirectoryServerDetailTableViewCell.h"
|
2017-04-07 08:38:05 +00:00
|
|
|
|
|
|
|
#import "AppDelegate.h"
|
2019-01-10 15:35:34 +00:00
|
|
|
#import "Riot-Swift.h"
|
2017-04-07 08:38:05 +00:00
|
|
|
|
|
|
|
@interface DirectoryServerPickerViewController ()
|
|
|
|
{
|
|
|
|
MXKDirectoryServersDataSource *dataSource;
|
|
|
|
|
|
|
|
// Observe kAppDelegateDidTapStatusBarNotification to handle tap on clock status bar.
|
|
|
|
id kAppDelegateDidTapStatusBarNotificationObserver;
|
2017-04-07 10:45:14 +00:00
|
|
|
|
2017-04-21 14:35:27 +00:00
|
|
|
void (^onCompleteBlock)(id<MXKDirectoryServerCellDataStoring> cellData);
|
2017-04-24 14:31:27 +00:00
|
|
|
|
|
|
|
// Current alert (if any).
|
2017-07-14 14:41:25 +00:00
|
|
|
UIAlertController *currentAlert;
|
2017-04-24 14:31:27 +00:00
|
|
|
|
|
|
|
// Current request in progress.
|
|
|
|
MXHTTPOperation *mxCurrentOperation;
|
2017-07-21 09:28:17 +00:00
|
|
|
|
|
|
|
// Observe kRiotDesignValuesDidChangeThemeNotification to handle user interface theme change.
|
|
|
|
id kRiotDesignValuesDidChangeThemeNotificationObserver;
|
2017-04-07 08:38:05 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DirectoryServerPickerViewController
|
|
|
|
|
|
|
|
- (void)finalizeInit
|
|
|
|
{
|
|
|
|
[super finalizeInit];
|
|
|
|
|
|
|
|
// Setup `MXKViewControllerHandling` properties
|
|
|
|
self.enableBarTintColorStatusChange = NO;
|
|
|
|
self.rageShakeManager = [RageShakeManager sharedManager];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)destroy
|
|
|
|
{
|
|
|
|
dataSource.delegate = nil;
|
|
|
|
dataSource = nil;
|
2017-04-07 10:45:14 +00:00
|
|
|
onCompleteBlock = nil;
|
2017-07-21 09:28:17 +00:00
|
|
|
|
|
|
|
if (kRiotDesignValuesDidChangeThemeNotificationObserver)
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:kRiotDesignValuesDidChangeThemeNotificationObserver];
|
|
|
|
kRiotDesignValuesDidChangeThemeNotificationObserver = nil;
|
|
|
|
}
|
2017-04-07 08:38:05 +00:00
|
|
|
|
|
|
|
if (kAppDelegateDidTapStatusBarNotificationObserver)
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:kAppDelegateDidTapStatusBarNotificationObserver];
|
|
|
|
kAppDelegateDidTapStatusBarNotificationObserver = nil;
|
|
|
|
}
|
|
|
|
|
2017-04-24 14:31:27 +00:00
|
|
|
// Close any pending actionsheet
|
|
|
|
if (currentAlert)
|
|
|
|
{
|
2017-07-14 14:41:25 +00:00
|
|
|
[currentAlert dismissViewControllerAnimated:NO completion:nil];
|
2017-04-24 14:31:27 +00:00
|
|
|
currentAlert = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mxCurrentOperation)
|
|
|
|
{
|
|
|
|
[mxCurrentOperation cancel];
|
|
|
|
mxCurrentOperation = nil;
|
|
|
|
}
|
|
|
|
|
2017-04-07 08:38:05 +00:00
|
|
|
[super destroy];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
2017-04-07 09:41:01 +00:00
|
|
|
self.title = NSLocalizedStringFromTable(@"directory_server_picker_title", @"Vector", nil);
|
2017-04-07 08:38:05 +00:00
|
|
|
|
|
|
|
self.tableView.delegate = self;
|
|
|
|
|
2017-05-03 09:16:44 +00:00
|
|
|
// Register view cell classes
|
2017-04-07 08:38:05 +00:00
|
|
|
[self.tableView registerClass:DirectoryServerTableViewCell.class forCellReuseIdentifier:DirectoryServerTableViewCell.defaultReuseIdentifier];
|
2017-05-03 09:16:44 +00:00
|
|
|
[self.tableView registerClass:DirectoryServerDetailTableViewCell.class forCellReuseIdentifier:DirectoryServerDetailTableViewCell.defaultReuseIdentifier];
|
2017-04-07 08:38:05 +00:00
|
|
|
|
2017-04-07 09:41:01 +00:00
|
|
|
// Add a cancel button
|
2017-04-07 08:38:05 +00:00
|
|
|
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancel:)];
|
2017-04-24 14:31:27 +00:00
|
|
|
self.navigationItem.leftBarButtonItem.accessibilityIdentifier = @"DirectoryServerPickerVCCancelButton";
|
|
|
|
|
|
|
|
// Add a + button
|
|
|
|
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onAdd:)];
|
|
|
|
self.navigationItem.rightBarButtonItem.accessibilityIdentifier = @"DirectoryServerPickerVCAddButton";
|
2017-04-07 08:38:05 +00:00
|
|
|
|
|
|
|
// Hide line separators of empty cells
|
|
|
|
self.tableView.tableFooterView = [[UIView alloc] init];
|
2017-07-21 09:28:17 +00:00
|
|
|
|
|
|
|
// Observe user interface theme change.
|
|
|
|
kRiotDesignValuesDidChangeThemeNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kRiotDesignValuesDidChangeThemeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
|
|
|
|
|
|
|
|
[self userInterfaceThemeDidChange];
|
|
|
|
|
|
|
|
}];
|
|
|
|
[self userInterfaceThemeDidChange];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)userInterfaceThemeDidChange
|
|
|
|
{
|
2019-01-10 15:35:34 +00:00
|
|
|
[RiotDesignValues.theme applyStyleOnNavigationBar:self.navigationController.navigationBar];
|
2018-11-29 16:31:41 +00:00
|
|
|
|
2017-09-11 12:49:06 +00:00
|
|
|
self.activityIndicator.backgroundColor = kRiotOverlayColor;
|
2017-08-11 14:56:09 +00:00
|
|
|
|
|
|
|
// Check the table view style to select its bg color.
|
2019-01-10 16:50:26 +00:00
|
|
|
self.tableView.backgroundColor = ((self.tableView.style == UITableViewStylePlain) ? RiotDesignValues.theme.backgroundColor : kRiotSecondaryBgColor);
|
2017-08-11 14:56:09 +00:00
|
|
|
self.view.backgroundColor = self.tableView.backgroundColor;
|
|
|
|
|
|
|
|
if (self.tableView.dataSource)
|
|
|
|
{
|
|
|
|
[self.tableView reloadData];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIStatusBarStyle)preferredStatusBarStyle
|
|
|
|
{
|
2019-01-10 16:08:32 +00:00
|
|
|
return RiotDesignValues.theme.statusBarStyle;
|
2017-04-07 08:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
2017-12-22 12:19:40 +00:00
|
|
|
// Screen tracking
|
2018-06-27 07:55:06 +00:00
|
|
|
[[Analytics sharedInstance] trackScreen:@"DirectoryServerPicker"];
|
2017-04-07 08:38:05 +00:00
|
|
|
|
|
|
|
// Observe kAppDelegateDidTapStatusBarNotificationObserver.
|
|
|
|
kAppDelegateDidTapStatusBarNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kAppDelegateDidTapStatusBarNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
|
|
|
|
|
2017-09-27 07:26:34 +00:00
|
|
|
[self.tableView setContentOffset:CGPointMake(-self.tableView.mxk_adjustedContentInset.left, -self.tableView.mxk_adjustedContentInset.top) animated:YES];
|
2017-04-07 08:38:05 +00:00
|
|
|
|
|
|
|
}];
|
|
|
|
|
2017-04-07 09:41:01 +00:00
|
|
|
[dataSource loadData];
|
2017-04-07 08:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillDisappear:(BOOL)animated
|
|
|
|
{
|
|
|
|
if (kAppDelegateDidTapStatusBarNotificationObserver)
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:kAppDelegateDidTapStatusBarNotificationObserver];
|
|
|
|
kAppDelegateDidTapStatusBarNotificationObserver = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
[super viewWillDisappear:animated];
|
|
|
|
}
|
|
|
|
|
2017-04-07 10:45:14 +00:00
|
|
|
- (void)displayWithDataSource:(MXKDirectoryServersDataSource*)theDataSource
|
2017-04-21 14:35:27 +00:00
|
|
|
onComplete:(void (^)(id<MXKDirectoryServerCellDataStoring> cellData))onComplete;
|
2017-04-07 08:38:05 +00:00
|
|
|
{
|
|
|
|
dataSource = theDataSource;
|
2017-04-07 10:45:14 +00:00
|
|
|
onCompleteBlock = onComplete;
|
|
|
|
|
|
|
|
// Let the data source provide cells
|
2017-04-07 08:38:05 +00:00
|
|
|
self.tableView.dataSource = dataSource;
|
|
|
|
|
|
|
|
dataSource.delegate = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - MXKDataSourceDelegate
|
|
|
|
|
|
|
|
- (Class<MXKCellRendering>)cellViewClassForCellData:(MXKCellData*)cellData
|
|
|
|
{
|
2017-05-03 07:31:10 +00:00
|
|
|
id<MXKDirectoryServerCellDataStoring> directoryCellData = (id<MXKDirectoryServerCellDataStoring>)cellData;
|
|
|
|
|
|
|
|
if (directoryCellData.homeserver)
|
|
|
|
{
|
|
|
|
return DirectoryServerDetailTableViewCell.class;
|
|
|
|
}
|
2017-04-07 08:38:05 +00:00
|
|
|
return DirectoryServerTableViewCell.class;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)cellReuseIdentifierForCellData:(MXKCellData*)cellData
|
|
|
|
{
|
2017-05-03 07:31:10 +00:00
|
|
|
id<MXKDirectoryServerCellDataStoring> directoryCellData = (id<MXKDirectoryServerCellDataStoring>)cellData;
|
|
|
|
|
|
|
|
if (directoryCellData.homeserver)
|
|
|
|
{
|
|
|
|
return DirectoryServerDetailTableViewCell.defaultReuseIdentifier;
|
|
|
|
}
|
2017-04-07 08:38:05 +00:00
|
|
|
return DirectoryServerTableViewCell.defaultReuseIdentifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dataSource:(MXKDataSource*)dataSource didCellChange:(id /* @TODO*/)changes
|
|
|
|
{
|
2017-04-07 09:41:01 +00:00
|
|
|
[self.tableView reloadData];
|
2017-04-07 08:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dataSource:(MXKDataSource*)dataSource2 didStateChange:(MXKDataSourceState)state
|
|
|
|
{
|
|
|
|
if (state == MXKDataSourceStatePreparing)
|
|
|
|
{
|
|
|
|
[self startActivityIndicator];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self stopActivityIndicator];
|
|
|
|
[self.tableView reloadData];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - UITableViewDelegate
|
|
|
|
|
2017-08-11 14:56:09 +00:00
|
|
|
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
|
|
|
|
{
|
2019-01-10 16:50:26 +00:00
|
|
|
cell.backgroundColor = RiotDesignValues.theme.backgroundColor;
|
2017-08-11 14:56:09 +00:00
|
|
|
|
|
|
|
// Update the selected background view
|
|
|
|
if (kRiotSelectedBgColor)
|
|
|
|
{
|
|
|
|
cell.selectedBackgroundView = [[UIView alloc] init];
|
|
|
|
cell.selectedBackgroundView.backgroundColor = kRiotSelectedBgColor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tableView.style == UITableViewStylePlain)
|
|
|
|
{
|
|
|
|
cell.selectedBackgroundView = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cell.selectedBackgroundView.backgroundColor = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 08:38:05 +00:00
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
return DirectoryServerTableViewCell.cellHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-04-07 10:45:14 +00:00
|
|
|
id<MXKDirectoryServerCellDataStoring> cellData = [dataSource cellDataAtIndexPath:indexPath];
|
|
|
|
|
|
|
|
if (onCompleteBlock)
|
|
|
|
{
|
2017-04-21 14:35:27 +00:00
|
|
|
onCompleteBlock(cellData);
|
2017-04-07 10:45:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 08:00:01 +00:00
|
|
|
[self withdrawViewControllerAnimated:YES completion:nil];
|
2017-04-07 08:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - User actions
|
|
|
|
|
|
|
|
- (IBAction)onCancel:(id)sender
|
|
|
|
{
|
2017-04-07 10:45:14 +00:00
|
|
|
if (onCompleteBlock)
|
|
|
|
{
|
2017-04-21 14:35:27 +00:00
|
|
|
onCompleteBlock(nil);
|
2017-04-07 10:45:14 +00:00
|
|
|
}
|
2017-04-07 08:38:05 +00:00
|
|
|
|
2017-04-21 08:00:01 +00:00
|
|
|
[self withdrawViewControllerAnimated:YES completion:nil];
|
2017-04-07 08:38:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 14:31:27 +00:00
|
|
|
- (IBAction)onAdd:(id)sender
|
|
|
|
{
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
|
|
2017-07-14 14:41:25 +00:00
|
|
|
[currentAlert dismissViewControllerAnimated:NO completion:nil];
|
2017-04-24 14:31:27 +00:00
|
|
|
|
|
|
|
// Prompt the user to enter a homeserver
|
2017-07-14 14:41:25 +00:00
|
|
|
currentAlert = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedStringFromTable(@"directory_server_type_homeserver", @"Vector", nil) preferredStyle:UIAlertControllerStyleAlert];
|
2017-04-24 14:31:27 +00:00
|
|
|
|
|
|
|
[currentAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
|
|
|
|
textField.secureTextEntry = NO;
|
|
|
|
textField.placeholder = NSLocalizedStringFromTable(@"directory_server_placeholder", @"Vector", nil);
|
|
|
|
textField.keyboardType = UIKeyboardTypeDefault;
|
|
|
|
}];
|
2017-07-14 14:41:25 +00:00
|
|
|
|
|
|
|
[currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"cancel"]
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction * action) {
|
|
|
|
|
|
|
|
if (weakSelf)
|
|
|
|
{
|
|
|
|
typeof(self) self = weakSelf;
|
|
|
|
self->currentAlert = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
}]];
|
|
|
|
|
|
|
|
[currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"ok"]
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction * action) {
|
|
|
|
|
|
|
|
if (weakSelf)
|
|
|
|
{
|
|
|
|
typeof(self) self = weakSelf;
|
|
|
|
|
|
|
|
UITextField *textField = [self->currentAlert textFields].firstObject;
|
|
|
|
|
|
|
|
self->currentAlert = nil;
|
|
|
|
|
|
|
|
NSString *homeserver = textField.text;
|
|
|
|
if (homeserver.length)
|
|
|
|
{
|
|
|
|
// Test if the homeserver exists
|
|
|
|
[self.activityIndicator startAnimating];
|
|
|
|
|
|
|
|
self->mxCurrentOperation = [self->dataSource.mxSession.matrixRestClient publicRoomsOnServer:homeserver limit:20 since:nil filter:nil thirdPartyInstanceId:nil includeAllNetworks:YES success:^(MXPublicRoomsResponse *publicRoomsResponse) {
|
|
|
|
|
|
|
|
if (weakSelf && self->mxCurrentOperation)
|
|
|
|
{
|
|
|
|
// The homeserver is valid
|
|
|
|
self->mxCurrentOperation = nil;
|
|
|
|
[self.activityIndicator stopAnimating];
|
|
|
|
|
|
|
|
if (self->onCompleteBlock)
|
|
|
|
{
|
|
|
|
// Prepare response argument
|
|
|
|
MXKDirectoryServerCellData *cellData = [[MXKDirectoryServerCellData alloc] initWithHomeserver:homeserver includeAllNetworks:YES];
|
|
|
|
|
|
|
|
self->onCompleteBlock(cellData);
|
|
|
|
}
|
|
|
|
|
|
|
|
[self withdrawViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
} failure:^(NSError *error) {
|
|
|
|
|
|
|
|
if (weakSelf && self->mxCurrentOperation)
|
|
|
|
{
|
|
|
|
// The homeserver is not valid
|
|
|
|
self->mxCurrentOperation = nil;
|
|
|
|
[self.activityIndicator stopAnimating];
|
|
|
|
|
|
|
|
[[AppDelegate theDelegate] showErrorAsAlert:error];
|
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}]];
|
|
|
|
|
|
|
|
[self presentViewController:currentAlert animated:YES completion:nil];
|
2017-04-24 14:31:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 08:38:05 +00:00
|
|
|
@end
|