Home: Improve handling of room alias text input

This commit is contained in:
giomfo 2014-12-19 15:54:01 +01:00
parent 974d86e09f
commit 35b0b06c60
2 changed files with 53 additions and 28 deletions

View file

@ -500,7 +500,7 @@
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" horizontalHuggingPriority="249" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="(e.g. #foo:homeserver)" minimumFontSize="10" translatesAutoresizingMaskIntoConstraints="NO" id="Rfz-WH-4KQ">
<rect key="frame" x="109" y="86" width="483" height="30"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" returnKeyType="done"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" returnKeyType="done"/>
<connections>
<outlet property="delegate" destination="ldZ-75-BUU" id="cVW-bM-tAs"/>
</connections>

View file

@ -30,6 +30,8 @@
NSMutableArray *filteredPublicRooms;
BOOL searchBarShouldEndEditing;
UIView *savedTableHeaderView;
NSString *homeServerSuffix;
}
@property (weak, nonatomic) IBOutlet UITableView *publicRoomsTable;
@ -82,8 +84,9 @@
[self.tableView scrollRectToVisible:_roomCreationLabel.frame animated:NO];
if ([[MatrixHandler sharedHandler] isLogged]) {
homeServerSuffix = [NSString stringWithFormat:@":%@",[MatrixHandler sharedHandler].homeServer];
// Update alias placeholder
_roomAliasTextField.placeholder = [NSString stringWithFormat:@"(e.g. #foo:%@)", [MatrixHandler sharedHandler].homeServer];
_roomAliasTextField.placeholder = [NSString stringWithFormat:@"(e.g. #foo%@)", homeServerSuffix];
// Refresh listed public rooms
[self refreshPublicRooms];
}
@ -160,9 +163,13 @@
// Remove '#' character
alias = [alias substringFromIndex:1];
// Remove homeserver
NSString *suffix = [NSString stringWithFormat:@":%@",[MatrixHandler sharedHandler].homeServer];
NSRange range = [alias rangeOfString:suffix];
alias = [alias stringByReplacingCharactersInRange:range withString:@""];
NSRange range = [alias rangeOfString:homeServerSuffix];
if (range.location == NSNotFound) {
NSLog(@"Wrong room alias has been set (%@)", _roomAliasTextField.text);
alias = nil;
} else {
alias = [alias stringByReplacingCharactersInRange:range withString:@""];
}
}
if (! alias.length) {
@ -211,10 +218,7 @@
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == _roomAliasTextField) {
textField.text = self.alias;
textField.placeholder = @"foo";
} else if (textField == _participantsTextField) {
if (textField == _participantsTextField) {
if (textField.text.length == 0) {
textField.text = @"@";
}
@ -223,14 +227,17 @@
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == _roomAliasTextField) {
// Compute the new alias with this string change
NSString * alias = textField.text;
if (alias.length) {
// add homeserver as suffix
textField.text = [NSString stringWithFormat:@"#%@:%@", alias, [MatrixHandler sharedHandler].homeServer];
// Check whether homeserver suffix should be added
NSRange range = [textField.text rangeOfString:@":"];
if (range.location == NSNotFound) {
textField.text = [textField.text stringByAppendingString:homeServerSuffix];
}
// Check whether the alias is valid
if (!self.alias) {
// reset text field
textField.text = nil;
[self onTextFieldChange:nil];
}
textField.placeholder = [NSString stringWithFormat:@"(e.g. #foo:%@)", [MatrixHandler sharedHandler].homeServer];
} else if (textField == _participantsTextField) {
NSArray *participants = self.participantsList;
textField.text = [participants componentsJoinedByString:@"; "];
@ -242,23 +249,41 @@
if (textField == _participantsTextField) {
// Auto completion is active only when the change concerns the end of the current string
if (range.location == textField.text.length) {
NSString *participants = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([string isEqualToString:@";"]) {
// Add '@' character
participants = [participants stringByAppendingString:@" @"];
textField.text = [textField.text stringByAppendingString:@"; @"];
// Update Create button status
[self onTextFieldChange:nil];
return NO;
} else if ([string isEqualToString:@":"]) {
// Add homeserver
if ([MatrixHandler sharedHandler].homeServer) {
participants = [participants stringByAppendingString:[MatrixHandler sharedHandler].homeServer];
}
textField.text = [textField.text stringByAppendingString:homeServerSuffix];
// Update Create button status
[self onTextFieldChange:nil];
return NO;
}
}
} else if (textField == _roomAliasTextField) {
// Add # if none
if (!textField.text.length) {
if ([string isEqualToString:@"#"] == NO) {
if ([string isEqualToString:@":"]) {
textField.text = [NSString stringWithFormat:@"#%@",homeServerSuffix];
} else {
textField.text = [NSString stringWithFormat:@"#%@",string];
}
// Update Create button status
[self onTextFieldChange:nil];
return NO;
}
} else {
// Add homeserver automatically when user adds ':' at the end
if (range.location == textField.text.length && [string isEqualToString:@":"]) {
textField.text = [textField.text stringByAppendingString:homeServerSuffix];
// Update Create button status
[self onTextFieldChange:nil];
return NO;
}
textField.text = participants;
// Update Create button status
[self onTextFieldChange:nil];
return NO;
}
}
return YES;