RoomViewController: replace existing RoomTitleView with MXKRoomTitleView class.

This commit is contained in:
giomfo
2015-05-27 16:51:46 +02:00
parent 376ce831ba
commit 32de8a245b
6 changed files with 37 additions and 565 deletions
+32 -194
View File
@@ -19,8 +19,6 @@
#import "MXKRoomBubbleTableViewCell.h"
#import "RoomTitleView.h"
#import "AppDelegate.h"
#import "RageShakeManager.h"
@@ -43,9 +41,7 @@
id pushedViewController;
}
@property (weak, nonatomic) IBOutlet UINavigationItem *roomNavItem;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *showRoomMembersButtonItem;
@property (weak, nonatomic) IBOutlet RoomTitleView *roomTitleView;
@property (strong, nonatomic) MXKAlert *actionMenu;
@@ -56,15 +52,14 @@
- (void)viewDidLoad {
[super viewDidLoad];
// Set room title view
[self setRoomTitleViewClass:MXKRoomTitleViewWithTopic.class];
// Replace the default input toolbar view with the one based on `HPGrowingTextView`.
[self setRoomInputToolbarViewClass:MXKRoomInputToolbarViewWithHPGrowingText.class];
// Set rageShake handler
self.rageShakeManager = [RageShakeManager sharedManager];
// ensure that the titleView will be scaled when it will be required
// during a screen rotation for example.
self.roomTitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
- (void)didReceiveMemoryWarning {
@@ -135,7 +130,7 @@
[AppDelegate theDelegate].masterTabBarController.visibleRoomId = nil;
}
#pragma mark -
#pragma mark - Override MXKRoomViewController
- (void)displayRoom:(MXKRoomDataSource *)dataSource {
@@ -148,12 +143,6 @@
[super displayRoom:dataSource];
}
- (void)dismissKeyboard {
[_roomTitleView dismissKeyboard];
[super dismissKeyboard];
}
- (void)didMatrixSessionStateChange {
[super didMatrixSessionStateChange];
@@ -171,47 +160,18 @@
// Update UI by considering dataSource state
if (self.roomDataSource && self.roomDataSource.state == MXKDataSourceStateReady) {
self.roomTitleView.mxRoom = self.roomDataSource.room;
self.roomTitleView.editable = YES;
self.roomTitleView.hidden = NO;
// Register a listener for events that concern room members
if (!membersListener) {
membersListener = [self.roomDataSource.room listenToEventsOfTypes:@[kMXEventTypeStringRoomMember] onEvent:^(MXEvent *event, MXEventDirection direction, id customObject) {
// Consider only live event
if (direction == MXEventDirectionForwards) {
// Check the room Id (if any)
if (event.roomId && [event.roomId isEqualToString:self.roomDataSource.roomId] == NO) {
// This event does not concern the current room members
return;
}
// Check whether no text field is editing before refreshing title view
if (!self.roomTitleView.isEditing) {
[self.roomTitleView refreshDisplay];
}
// Update navigation bar items
[self updateNavigationBarButtonItems];
}
}];
}
} else {
// Update the title except if the room has just been left
if (!self.leftRoomReasonLabel) {
if (self.roomDataSource && self.roomDataSource.state == MXKDataSourceStatePreparing) {
self.roomTitleView.mxRoom = self.roomDataSource.room;
self.roomTitleView.hidden = (!self.roomTitleView.mxRoom);
}
else {
self.roomTitleView.mxRoom = nil;
self.roomTitleView.hidden = NO;
}
}
self.roomTitleView.editable = NO;
// Remove members listener if any.
if (membersListener) {
[self.roomDataSource.room removeListener:membersListener];
@@ -219,13 +179,38 @@
}
}
// Finalize room title refresh
[self.roomTitleView refreshDisplay];
// Update navigation bar items
[self updateNavigationBarButtonItems];
}
- (BOOL)isIRCStyleCommand:(NSString*)string {
// Override the default behavior for `/join` command in order to open automatically the joined room
if ([string hasPrefix:kCmdJoinRoom]) {
// Join a room
NSString *roomAlias = [string substringFromIndex:kCmdJoinRoom.length + 1];
// Remove white space from both ends
roomAlias = [roomAlias stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// Check
if (roomAlias.length) {
[self.mxSession joinRoom:roomAlias success:^(MXRoom *room) {
// Show the room
[[AppDelegate theDelegate].masterTabBarController showRoom:room.state.roomId];
} failure:^(NSError *error) {
NSLog(@"[Console RoomVC] Join roomAlias (%@) failed: %@", roomAlias, error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
} else {
// Display cmd usage in text input as placeholder
self.inputToolbarView.placeholder = @"Usage: /join <room_alias>";
}
return YES;
}
return [super isIRCStyleCommand:string];
}
- (void)destroy {
if (membersListener) {
[self.roomDataSource.room removeListener:membersListener];
@@ -295,153 +280,6 @@
}
}
#pragma mark - UITextField delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSString *alertMsg = nil;
if (textField == _roomTitleView.displayNameTextField) {
// Check whether the user has enough power to rename the room
MXRoomPowerLevels *powerLevels = [self.roomDataSource.room.state powerLevels];
NSUInteger userPowerLevel = [powerLevels powerLevelOfUserWithUserID:self.mxSession.myUser.userId];
if (userPowerLevel >= [powerLevels minimumPowerLevelForSendingEventAsStateEvent:kMXEventTypeStringRoomName]) {
// Only the room name is edited here, update the text field with the room name
textField.text = self.roomDataSource.room.state.name;
textField.backgroundColor = [UIColor whiteColor];
} else {
alertMsg = @"You are not authorized to edit this room name";
}
// Check whether the user is allowed to change room topic
if (userPowerLevel >= [powerLevels minimumPowerLevelForSendingEventAsStateEvent:kMXEventTypeStringRoomTopic]) {
// Show topic text field even if the current value is nil
_roomTitleView.hiddenTopic = NO;
if (alertMsg) {
// Here the user can only update the room topic, switch on room topic field (without displaying alert)
alertMsg = nil;
[_roomTitleView.topicTextField becomeFirstResponder];
return NO;
}
}
} else if (textField == _roomTitleView.topicTextField) {
// Check whether the user has enough power to edit room topic
MXRoomPowerLevels *powerLevels = [self.roomDataSource.room.state powerLevels];
NSUInteger userPowerLevel = [powerLevels powerLevelOfUserWithUserID:self.mxSession.myUser.userId];
if (userPowerLevel >= [powerLevels minimumPowerLevelForSendingEventAsStateEvent:kMXEventTypeStringRoomTopic]) {
textField.backgroundColor = [UIColor whiteColor];
[self.roomTitleView stopTopicAnimation];
} else {
alertMsg = @"You are not authorized to edit this room topic";
}
}
if (alertMsg) {
// Alert user
__weak typeof(self) weakSelf = self;
if (self.actionMenu) {
[self.actionMenu dismiss:NO];
}
self.actionMenu = [[MXKAlert alloc] initWithTitle:nil message:alertMsg style:MXKAlertStyleAlert];
self.actionMenu.cancelButtonIndex = [self.actionMenu addActionWithTitle:@"Cancel" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) {
weakSelf.actionMenu = nil;
}];
[self.actionMenu showInViewController:self];
return NO;
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == _roomTitleView.displayNameTextField) {
textField.backgroundColor = [UIColor clearColor];
NSString *roomName = textField.text;
if ((roomName.length || self.roomDataSource.room.state.name.length) && [roomName isEqualToString:self.roomDataSource.room.state.name] == NO) {
[self.activityIndicator startAnimating];
__weak typeof(self) weakSelf = self;
[self.roomDataSource.room setName:roomName success:^{
[self stopActivityIndicator];
// Refresh title display
textField.text = weakSelf.roomDataSource.room.state.displayname;
} failure:^(NSError *error) {
[self stopActivityIndicator];
// Revert change
textField.text = weakSelf.roomDataSource.room.state.displayname;
NSLog(@"[Console RoomVC] Rename room failed: %@", error);
// Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
} else {
// No change on room name, restore title with room displayName
textField.text = self.roomDataSource.room.state.displayname;
}
} else if (textField == _roomTitleView.topicTextField) {
textField.backgroundColor = [UIColor clearColor];
NSString *topic = textField.text;
if ((topic.length || self.roomDataSource.room.state.topic.length) && [topic isEqualToString:self.roomDataSource.room.state.topic] == NO) {
[self.activityIndicator startAnimating];
__weak typeof(self) weakSelf = self;
[self.roomDataSource.room setTopic:topic success:^{
[self stopActivityIndicator];
// Hide topic field if empty
weakSelf.roomTitleView.hiddenTopic = !textField.text.length;
} failure:^(NSError *error) {
[self stopActivityIndicator];
// Revert change
textField.text = weakSelf.roomDataSource.room.state.topic;
// Hide topic field if empty
weakSelf.roomTitleView.hiddenTopic = !textField.text.length;
NSLog(@"[RoomVC] Topic room change failed: %@", error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
} else {
// Hide topic field if empty
_roomTitleView.hiddenTopic = !topic.length;
}
}
}
- (BOOL)textFieldShouldReturn:(UITextField*) textField {
if (textField == _roomTitleView.displayNameTextField) {
// "Next" key has been pressed
[_roomTitleView.topicTextField becomeFirstResponder];
} else {
// "Done" key has been pressed
[textField resignFirstResponder];
}
return YES;
}
- (BOOL)isIRCStyleCommand:(NSString*)string {
// Override the default behavior for `/join` command in order to open automatically the joined room
if ([string hasPrefix:kCmdJoinRoom]) {
// Join a room
NSString *roomAlias = [string substringFromIndex:kCmdJoinRoom.length + 1];
// Remove white space from both ends
roomAlias = [roomAlias stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// Check
if (roomAlias.length) {
[self.mxSession joinRoom:roomAlias success:^(MXRoom *room) {
// Show the room
[[AppDelegate theDelegate].masterTabBarController showRoom:room.state.roomId];
} failure:^(NSError *error) {
NSLog(@"[Console RoomVC] Join roomAlias (%@) failed: %@", roomAlias, error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
} else {
// Display cmd usage in text input as placeholder
self.inputToolbarView.placeholder = @"Usage: /join <room_alias>";
}
return YES;
}
return [super isIRCStyleCommand:string];
}
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {