diff --git a/matrixConsole/AppDelegate.m b/matrixConsole/AppDelegate.m index bbbe99f00..2057f23bc 100644 --- a/matrixConsole/AppDelegate.m +++ b/matrixConsole/AppDelegate.m @@ -318,7 +318,28 @@ // Look for the room id NSString* roomId = [userInfo objectForKey:@"room_id"]; if (roomId.length) { - [self.masterTabBarController showRoom:roomId]; + // TODO retrieve the right matrix session + + //************** + // Patch consider the first session which knows the room id + MXSession *mxSession; + NSArray *mxAccounts = [MXKAccountManager sharedManager].accounts; + + if (mxAccounts.count == 1) { + MXKAccount *account = mxAccounts.firstObject; + mxSession = account.mxSession; + } else { + for (MXKAccount *account in mxAccounts) { + if ([account.mxSession roomWithRoomId:roomId]) { + mxSession = account.mxSession; + break; + } + } + } + //************** + + + [self.masterTabBarController showRoom:roomId withMatrixSession:mxSession]; } } } @@ -565,7 +586,7 @@ handler:^(MXKAlert *alert) { weakSelf.mxInAppNotification = nil; // Show the room - [weakSelf.masterTabBarController showRoom:event.roomId]; + [weakSelf.masterTabBarController showRoom:event.roomId withMatrixSession:account.mxSession]; }]; [self.mxInAppNotification showInViewController:[self.masterTabBarController selectedViewController]]; @@ -673,7 +694,7 @@ // if the room exists if (mxRoom) { // open it - [self.masterTabBarController showRoom:mxRoom.state.roomId]; + [self.masterTabBarController showRoom:mxRoom.state.roomId withMatrixSession:mxSession]; } else { // create a new room [mxSession createRoom:nil @@ -693,7 +714,7 @@ } // Open created room - [self.masterTabBarController showRoom:room.state.roomId]; + [self.masterTabBarController showRoom:room.state.roomId withMatrixSession:mxSession]; } failure:^(NSError *error) { NSLog(@"[AppDelegate] Create room failed: %@", error); diff --git a/matrixConsole/ViewController/HomeViewController.m b/matrixConsole/ViewController/HomeViewController.m index 7a1367c45..0615f67a7 100644 --- a/matrixConsole/ViewController/HomeViewController.m +++ b/matrixConsole/ViewController/HomeViewController.m @@ -37,7 +37,6 @@ NSMutableArray *homeServerSuffixArray; MXKAlert *mxAccountSelectionAlert; - MXSession *selectedSession; } @property (weak, nonatomic) IBOutlet UITableView *publicRoomsTable; @@ -317,13 +316,12 @@ return participants; } -- (void)selectMatrixSession:(void (^)())onSelection { +- (void)selectMatrixSession:(void (^)(MXSession *selectedSession))onSelection { NSArray *mxSessions = self.mxSessions; if (mxSessions.count == 1) { - selectedSession = self.mainSession; if (onSelection) { - onSelection(); + onSelection(self.mainSession); } } else if (mxSessions.count > 1) { if (mxAccountSelectionAlert) { @@ -337,10 +335,8 @@ [mxAccountSelectionAlert addActionWithTitle:mxSession.myUser.userId style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { __strong __typeof(weakSelf)strongSelf = weakSelf; strongSelf->mxAccountSelectionAlert = nil; - strongSelf->selectedSession = mxSession; - if (onSelection) { - onSelection(); + onSelection(mxSession); } }]; } @@ -494,81 +490,74 @@ [self dismissKeyboard]; - // Check whether a session is selected - if (!selectedSession) { - [self selectMatrixSession:^{ - [self onButtonPressed:sender]; - }]; - return; - } - - if (sender == _createRoomBtn) { - // Disable button to prevent multiple request - _createRoomBtn.enabled = NO; - - NSString *roomName = _roomNameTextField.text; - if (! roomName.length) { - roomName = nil; - } - - // Create new room - [selectedSession createRoom:roomName - visibility:(_roomVisibilityControl.selectedSegmentIndex == 0) ? kMXRoomVisibilityPublic : kMXRoomVisibilityPrivate - roomAlias:self.alias - topic:nil - success:^(MXRoom *room) { - // Check whether some users must be invited - NSArray *invitedUsers = self.participantsList; - for (NSString *userId in invitedUsers) { - [room inviteUser:userId success:^{ - NSLog(@"[HomeVC] %@ has been invited (roomId: %@)", userId, room.state.roomId); - } failure:^(NSError *error) { - NSLog(@"[HomeVC] %@ invitation failed (roomId: %@): %@", userId, room.state.roomId, error); - //Alert user - [[AppDelegate theDelegate] showErrorAsAlert:error]; - }]; - } - - // Reset text fields - _roomNameTextField.text = nil; - _roomAliasTextField.text = nil; - _participantsTextField.text = nil; - // Open created room - [[AppDelegate theDelegate].masterTabBarController showRoom:room.state.roomId]; - } failure:^(NSError *error) { - _createRoomBtn.enabled = YES; - NSLog(@"[HomeVC] Create room (%@ %@ (%@)) failed: %@", _roomNameTextField.text, self.alias, (_roomVisibilityControl.selectedSegmentIndex == 0) ? @"Public":@"Private", error); - //Alert user - [[AppDelegate theDelegate] showErrorAsAlert:error]; - }]; - } else if (sender == _joinRoomBtn) { - // Disable button to prevent multiple request - _joinRoomBtn.enabled = NO; - - NSString *roomAlias = _joinRoomAliasTextField.text; - // Remove white space from both ends - roomAlias = [roomAlias stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; - - // Check - if (roomAlias.length) { - [selectedSession joinRoom:roomAlias success:^(MXRoom *room) { + // Handle multi-sessions here + [self selectMatrixSession:^(MXSession *selectedSession) { + if (sender == _createRoomBtn) { + // Disable button to prevent multiple request + _createRoomBtn.enabled = NO; + + NSString *roomName = _roomNameTextField.text; + if (! roomName.length) { + roomName = nil; + } + + // Create new room + [selectedSession createRoom:roomName + visibility:(_roomVisibilityControl.selectedSegmentIndex == 0) ? kMXRoomVisibilityPublic : kMXRoomVisibilityPrivate + roomAlias:self.alias + topic:nil + success:^(MXRoom *room) { + // Check whether some users must be invited + NSArray *invitedUsers = self.participantsList; + for (NSString *userId in invitedUsers) { + [room inviteUser:userId success:^{ + NSLog(@"[HomeVC] %@ has been invited (roomId: %@)", userId, room.state.roomId); + } failure:^(NSError *error) { + NSLog(@"[HomeVC] %@ invitation failed (roomId: %@): %@", userId, room.state.roomId, error); + //Alert user + [[AppDelegate theDelegate] showErrorAsAlert:error]; + }]; + } + + // Reset text fields + _roomNameTextField.text = nil; + _roomAliasTextField.text = nil; + _participantsTextField.text = nil; + // Open created room + [[AppDelegate theDelegate].masterTabBarController showRoom:room.state.roomId withMatrixSession:selectedSession]; + } failure:^(NSError *error) { + _createRoomBtn.enabled = YES; + NSLog(@"[HomeVC] Create room (%@ %@ (%@)) failed: %@", _roomNameTextField.text, self.alias, (_roomVisibilityControl.selectedSegmentIndex == 0) ? @"Public":@"Private", error); + //Alert user + [[AppDelegate theDelegate] showErrorAsAlert:error]; + }]; + } else if (sender == _joinRoomBtn) { + // Disable button to prevent multiple request + _joinRoomBtn.enabled = NO; + + NSString *roomAlias = _joinRoomAliasTextField.text; + // Remove white space from both ends + roomAlias = [roomAlias stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + + // Check + if (roomAlias.length) { + [selectedSession joinRoom:roomAlias success:^(MXRoom *room) { + // Reset text fields + _joinRoomAliasTextField.text = nil; + // Show the room + [[AppDelegate theDelegate].masterTabBarController showRoom:room.state.roomId withMatrixSession:selectedSession]; + } failure:^(NSError *error) { + _joinRoomBtn.enabled = YES; + NSLog(@"[HomeVC] Failed to join room alias (%@): %@", roomAlias, error); + //Alert user + [[AppDelegate theDelegate] showErrorAsAlert:error]; + }]; + } else { // Reset text fields _joinRoomAliasTextField.text = nil; - // Show the room - [[AppDelegate theDelegate].masterTabBarController showRoom:room.state.roomId]; - } failure:^(NSError *error) { - _joinRoomBtn.enabled = YES; - NSLog(@"[HomeVC] Failed to join room alias (%@): %@", roomAlias, error); - //Alert user - [[AppDelegate theDelegate] showErrorAsAlert:error]; - }]; - } else { - // Reset text fields - _joinRoomAliasTextField.text = nil; + } } - } - - selectedSession = nil; + }]; } #pragma mark - Table view data source @@ -703,52 +692,47 @@ #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - // Check whether a session is selected - if (!selectedSession) { - [self selectMatrixSession:^{ - [self tableView:tableView didSelectRowAtIndexPath:indexPath]; - }]; - return; - } - - MXPublicRoom *publicRoom; - if (filteredPublicRooms && indexPath.row < filteredPublicRooms.count) { - publicRoom = [filteredPublicRooms objectAtIndex:indexPath.row]; - } else if (indexPath.row < publicRooms.count){ - publicRoom = [publicRooms objectAtIndex:indexPath.row]; - } - - if (publicRooms) { - // Check whether the user has already joined the selected public room - if ([selectedSession roomWithRoomId:publicRoom.roomId]) { - // Open selected room - [[AppDelegate theDelegate].masterTabBarController showRoom:publicRoom.roomId]; - } else { - // Join the selected room - UIActivityIndicatorView *loadingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; - UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; - if (selectedCell) { - CGPoint center = CGPointMake(selectedCell.frame.size.width / 2, selectedCell.frame.size.height / 2); - loadingWheel.center = center; - [selectedCell addSubview:loadingWheel]; - } - [loadingWheel startAnimating]; - [selectedSession joinRoom:publicRoom.roomId success:^(MXRoom *room) { - // Show joined room - [loadingWheel stopAnimating]; - [loadingWheel removeFromSuperview]; - [[AppDelegate theDelegate].masterTabBarController showRoom:publicRoom.roomId]; - } failure:^(NSError *error) { - NSLog(@"[HomeVC] Failed to join public room (%@): %@", publicRoom.displayname, error); - //Alert user - [loadingWheel stopAnimating]; - [loadingWheel removeFromSuperview]; - [[AppDelegate theDelegate] showErrorAsAlert:error]; - }]; + // Handle multi-sessions here + [self selectMatrixSession:^(MXSession *selectedSession) { + MXPublicRoom *publicRoom; + if (filteredPublicRooms && indexPath.row < filteredPublicRooms.count) { + publicRoom = [filteredPublicRooms objectAtIndex:indexPath.row]; + } else if (indexPath.row < publicRooms.count){ + publicRoom = [publicRooms objectAtIndex:indexPath.row]; } - [tableView deselectRowAtIndexPath:indexPath animated:YES]; - } + if (publicRooms) { + // Check whether the user has already joined the selected public room + if ([selectedSession roomWithRoomId:publicRoom.roomId]) { + // Open selected room + [[AppDelegate theDelegate].masterTabBarController showRoom:publicRoom.roomId withMatrixSession:selectedSession]; + } else { + // Join the selected room + UIActivityIndicatorView *loadingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; + UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; + if (selectedCell) { + CGPoint center = CGPointMake(selectedCell.frame.size.width / 2, selectedCell.frame.size.height / 2); + loadingWheel.center = center; + [selectedCell addSubview:loadingWheel]; + } + [loadingWheel startAnimating]; + [selectedSession joinRoom:publicRoom.roomId success:^(MXRoom *room) { + // Show joined room + [loadingWheel stopAnimating]; + [loadingWheel removeFromSuperview]; + [[AppDelegate theDelegate].masterTabBarController showRoom:publicRoom.roomId withMatrixSession:selectedSession]; + } failure:^(NSError *error) { + NSLog(@"[HomeVC] Failed to join public room (%@): %@", publicRoom.displayname, error); + //Alert user + [loadingWheel stopAnimating]; + [loadingWheel removeFromSuperview]; + [[AppDelegate theDelegate] showErrorAsAlert:error]; + }]; + } + + [tableView deselectRowAtIndexPath:indexPath animated:YES]; + } + }]; } #pragma mark - UISearchBarDelegate diff --git a/matrixConsole/ViewController/MasterTabBarController.h b/matrixConsole/ViewController/MasterTabBarController.h index 2dec4eaa3..48c5e9477 100644 --- a/matrixConsole/ViewController/MasterTabBarController.h +++ b/matrixConsole/ViewController/MasterTabBarController.h @@ -39,7 +39,7 @@ - (void)showAuthenticationScreen; - (void)showRoomCreationForm; -- (void)showRoom:(NSString*)roomId; +- (void)showRoom:(NSString*)roomId withMatrixSession:(MXSession*)mxSession; - (void)popRoomViewControllerAnimated:(BOOL)animated; diff --git a/matrixConsole/ViewController/MasterTabBarController.m b/matrixConsole/ViewController/MasterTabBarController.m index 8d0c58177..f09810641 100644 --- a/matrixConsole/ViewController/MasterTabBarController.m +++ b/matrixConsole/ViewController/MasterTabBarController.m @@ -219,17 +219,15 @@ [self setSelectedIndex:TABBAR_HOME_INDEX]; } -- (void)showRoom:(NSString*)roomId { +- (void)showRoom:(NSString*)roomId withMatrixSession:(MXSession*)mxSession { [self restoreInitialDisplay]; // Switch on Recents Tab [self setSelectedIndex:TABBAR_RECENTS_INDEX]; - // TODO GFO handle multi-session - // Select room to display its details (dispatch this action in order to let TabBarController end its refresh) dispatch_async(dispatch_get_main_queue(), ^{ - [recentsViewController selectRoomWithId:roomId inMatrixSession:nil]; // TODO a matrix session is required here + [recentsViewController selectRoomWithId:roomId inMatrixSession:mxSession]; }); } diff --git a/matrixConsole/ViewController/RecentsViewController.m b/matrixConsole/ViewController/RecentsViewController.m index 8e6ad49c1..3a12c841d 100644 --- a/matrixConsole/ViewController/RecentsViewController.m +++ b/matrixConsole/ViewController/RecentsViewController.m @@ -178,23 +178,6 @@ } } -//- (void)onRecentRoomUpdatedByBackPagination:(NSNotification *)notif{ -// [self refreshRecentsDisplay]; -// [self updateTitleView]; -// -// if ([notif.object isKindOfClass:[NSString class]]) { -// NSString* roomId = notif.object; -// // Check whether this room is currently displayed in RoomViewController -// if ([[AppDelegate theDelegate].masterTabBarController.visibleRoomId isEqualToString:roomId]) { -// // For sanity reason, we have to force a full refresh in order to restore back state of the room -// dispatch_async(dispatch_get_main_queue(), ^{ -// MXKRoomDataSource *roomDataSrc = currentRoomViewController.dataSource; -// [currentRoomViewController displayRoom:roomDataSrc]; -// }); -// } -// } -//} - - (void)updateTitleView { NSString *title = @"Recents"; diff --git a/matrixConsole/ViewController/RoomViewController.m b/matrixConsole/ViewController/RoomViewController.m index f7b7df661..62b06422f 100644 --- a/matrixConsole/ViewController/RoomViewController.m +++ b/matrixConsole/ViewController/RoomViewController.m @@ -189,7 +189,7 @@ if (roomAlias.length) { [self.mainSession joinRoom:roomAlias success:^(MXRoom *room) { // Show the room - [[AppDelegate theDelegate].masterTabBarController showRoom:room.state.roomId]; + [[AppDelegate theDelegate].masterTabBarController showRoom:room.state.roomId withMatrixSession:self.mainSession]; } failure:^(NSError *error) { NSLog(@"[Console RoomVC] Join roomAlias (%@) failed: %@", roomAlias, error); //Alert user