Directory server list: Let user enter random homeserver

This commit is contained in:
manuroe
2017-04-24 16:31:27 +02:00
parent 5b254f49c0
commit 8d9455fb27
2 changed files with 106 additions and 1 deletions
@@ -27,6 +27,12 @@
id kAppDelegateDidTapStatusBarNotificationObserver;
void (^onCompleteBlock)(id<MXKDirectoryServerCellDataStoring> cellData);
// Current alert (if any).
MXKAlert *currentAlert;
// Current request in progress.
MXHTTPOperation *mxCurrentOperation;
}
@end
@@ -54,6 +60,19 @@
kAppDelegateDidTapStatusBarNotificationObserver = nil;
}
// Close any pending actionsheet
if (currentAlert)
{
[currentAlert dismiss:NO];
currentAlert = nil;
}
if (mxCurrentOperation)
{
[mxCurrentOperation cancel];
mxCurrentOperation = nil;
}
[super destroy];
}
@@ -70,7 +89,11 @@
// Add a cancel button
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancel:)];
self.navigationItem.leftBarButtonItem.accessibilityIdentifier=@"DirectoryServerPickerVCCancelButton";
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";
// Hide line separators of empty cells
self.tableView.tableFooterView = [[UIView alloc] init];
@@ -183,4 +206,84 @@
[self withdrawViewControllerAnimated:YES completion:nil];
}
- (IBAction)onAdd:(id)sender
{
__weak typeof(self) weakSelf = self;
[currentAlert dismiss:NO];
// Prompt the user to enter a homeserver
currentAlert = [[MXKAlert alloc] initWithTitle:nil message:NSLocalizedStringFromTable(@"directory_server_type_homeserver", @"Vector", nil) style:MXKAlertStyleAlert];
[currentAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = NO;
textField.placeholder = NSLocalizedStringFromTable(@"directory_server_placeholder", @"Vector", nil);
textField.keyboardType = UIKeyboardTypeDefault;
}];
currentAlert.cancelButtonIndex = [currentAlert addActionWithTitle:[NSBundle mxk_localizedStringForKey:@"cancel"] style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) {
if (weakSelf)
{
typeof(self) self = weakSelf;
self->currentAlert = nil;
}
}];
[currentAlert addActionWithTitle:[NSBundle mxk_localizedStringForKey:@"ok"] style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) {
if (weakSelf)
{
UITextField *textField = [alert textFieldAtIndex:0];
typeof(self) self = weakSelf;
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];
}
}];
}
}
}];
[currentAlert showInViewController:self];
}
@end