diff --git a/Riot/Modules/Common/Recents/RecentsViewController.m b/Riot/Modules/Common/Recents/RecentsViewController.m index 98fe7dd63..fca8a1300 100644 --- a/Riot/Modules/Common/Recents/RecentsViewController.m +++ b/Riot/Modules/Common/Recents/RecentsViewController.m @@ -1144,8 +1144,10 @@ NSString *const RecentsViewControllerDataReadyNotification = @"RecentsViewContro if (weakSelf) { typeof(self) self = weakSelf; - [self presentViewController:isLastOwnerPrompt animated:YES completion:nil]; - self->currentAlert = isLastOwnerPrompt; + dispatch_async(dispatch_get_main_queue(), ^{ + [self presentViewController:isLastOwnerPrompt animated:YES completion:nil]; + self->currentAlert = isLastOwnerPrompt; + }); } } else @@ -1243,8 +1245,10 @@ NSString *const RecentsViewControllerDataReadyNotification = @"RecentsViewContro if (weakSelf) { typeof(self) self = weakSelf; - [self presentViewController:leavePrompt animated:YES completion:nil]; - self->currentAlert = leavePrompt; + dispatch_async(dispatch_get_main_queue(), ^{ + [self presentViewController:leavePrompt animated:YES completion:nil]; + self->currentAlert = leavePrompt; + }); } } }]; diff --git a/Riot/Modules/MatrixKit/Controllers/MXKRoomMemberDetailsViewController.m b/Riot/Modules/MatrixKit/Controllers/MXKRoomMemberDetailsViewController.m index 39bb1854c..908a3e72c 100644 --- a/Riot/Modules/MatrixKit/Controllers/MXKRoomMemberDetailsViewController.m +++ b/Riot/Modules/MatrixKit/Controllers/MXKRoomMemberDetailsViewController.m @@ -220,20 +220,55 @@ Please see LICENSE in the repository root for full details. } case MXKRoomMemberDetailsActionLeave: { - [self addPendingActionMask]; - [self.mxRoom leave:^{ - - [self removePendingActionMask]; - [self withdrawViewControllerAnimated:YES completion:nil]; - - } failure:^(NSError *error) { - - [self removePendingActionMask]; - MXLogDebug(@"[MXKRoomMemberDetailsVC] Leave room %@ failed", self->mxRoom.roomId); - // Notify MatrixKit user - NSString *myUserId = self.mainSession.myUser.userId; - [[NSNotificationCenter defaultCenter] postNotificationName:kMXKErrorNotification object:error userInfo:myUserId ? @{kMXKErrorUserIdKey: myUserId} : nil]; - + __weak typeof(self) weakSelf = self; + [self.mxRoom isLastOwnerWithCompletionHandler:^(BOOL isLastOwner, NSError* error){ + if (isLastOwner) + { + UIAlertController *isLastOwnerPrompt = [UIAlertController alertControllerWithTitle:[VectorL10n error] + message:[VectorL10n roomParticipantsLeaveNotAllowedForLastOwnerMsg] + preferredStyle:UIAlertControllerStyleAlert]; + + [isLastOwnerPrompt addAction:[UIAlertAction actionWithTitle:[VectorL10n ok] + style:UIAlertActionStyleCancel + handler:^(UIAlertAction * action) { + if (weakSelf) + { + typeof(self) self = weakSelf; + self->currentAlert = nil; + } + }]]; + + if (weakSelf) + { + typeof(self) self = weakSelf; + dispatch_async(dispatch_get_main_queue(), ^{ + [self presentViewController:isLastOwnerPrompt animated:YES completion:nil]; + self->currentAlert = isLastOwnerPrompt; + }); + } + } + else + { + if (weakSelf) + { + typeof(self) self = weakSelf; + [self addPendingActionMask]; + [self.mxRoom leave:^{ + + [self removePendingActionMask]; + [self withdrawViewControllerAnimated:YES completion:nil]; + + } failure:^(NSError *error) { + + [self removePendingActionMask]; + MXLogDebug(@"[MXKRoomMemberDetailsVC] Leave room %@ failed", self->mxRoom.roomId); + // Notify MatrixKit user + NSString *myUserId = self.mainSession.myUser.userId; + [[NSNotificationCenter defaultCenter] postNotificationName:kMXKErrorNotification object:error userInfo:myUserId ? @{kMXKErrorUserIdKey: myUserId} : nil]; + + }]; + } + } }]; break; } diff --git a/Riot/Modules/Room/Members/RoomParticipantsViewController.m b/Riot/Modules/Room/Members/RoomParticipantsViewController.m index 6ec3b8e7c..392211da1 100644 --- a/Riot/Modules/Room/Members/RoomParticipantsViewController.m +++ b/Riot/Modules/Room/Members/RoomParticipantsViewController.m @@ -1231,62 +1231,7 @@ Please see LICENSE in the repository root for full details. if (section == participantsSection && userParticipant && (0 == row) && !currentSearchText.length) { - // Leave ? - MXWeakify(self); - - NSString *title, *message; - if (self.mxRoom.isDirect) - { - title = [VectorL10n roomParticipantsLeavePromptTitleForDm]; - message = [VectorL10n roomParticipantsLeavePromptMsgForDm]; - } - else - { - title = [VectorL10n roomParticipantsLeavePromptTitle]; - message = [VectorL10n roomParticipantsLeavePromptMsg]; - } - - currentAlert = [UIAlertController alertControllerWithTitle:title - message:message - preferredStyle:UIAlertControllerStyleAlert]; - - [currentAlert addAction:[UIAlertAction actionWithTitle:[VectorL10n cancel] - style:UIAlertActionStyleCancel - handler:^(UIAlertAction * action) { - - MXStrongifyAndReturnIfNil(self); - self->currentAlert = nil; - - }]]; - - [currentAlert addAction:[UIAlertAction actionWithTitle:[VectorL10n leave] - style:UIAlertActionStyleDefault - handler:^(UIAlertAction * action) { - - MXStrongifyAndReturnIfNil(self); - self->currentAlert = nil; - - [self addPendingActionMask]; - MXWeakify(self); - [self.mxRoom leave:^{ - - MXStrongifyAndReturnIfNil(self); - [self withdrawViewControllerAnimated:YES completion:nil]; - - } failure:^(NSError *error) { - - MXStrongifyAndReturnIfNil(self); - [self removePendingActionMask]; - MXLogDebug(@"[RoomParticipantsVC] Leave room %@ failed", self.mxRoom.roomId); - // Alert user - [[AppDelegate theDelegate] showErrorAsAlert:error]; - - }]; - - }]]; - - [currentAlert mxk_setAccessibilityIdentifier:@"RoomParticipantsVCLeaveAlert"]; - [self presentViewController:currentAlert animated:YES completion:nil]; + [self leaveRoom]; } else { @@ -1433,6 +1378,89 @@ Please see LICENSE in the repository root for full details. } } +- (void)leaveRoom { + MXWeakify(self); + + [self.mxRoom isLastOwnerWithCompletionHandler:^(BOOL isLastOnwer, NSError* error) { + if (isLastOnwer) + { + MXStrongifyAndReturnIfNil(self); + self->currentAlert = [UIAlertController alertControllerWithTitle:[VectorL10n error] + message:[VectorL10n roomParticipantsLeaveNotAllowedForLastOwnerMsg] + preferredStyle:UIAlertControllerStyleAlert]; + + [self->currentAlert addAction:[UIAlertAction actionWithTitle:[VectorL10n cancel] + style:UIAlertActionStyleCancel + handler:^(UIAlertAction * action) { + + MXStrongifyAndReturnIfNil(self); + self->currentAlert = nil; + }]]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self presentViewController:self->currentAlert animated:YES completion:nil]; + }); + } + else + { + // Leave ? + MXStrongifyAndReturnIfNil(self); + NSString *title, *message; + if (self.mxRoom.isDirect) + { + title = [VectorL10n roomParticipantsLeavePromptTitleForDm]; + message = [VectorL10n roomParticipantsLeavePromptMsgForDm]; + } + else + { + title = [VectorL10n roomParticipantsLeavePromptTitle]; + message = [VectorL10n roomParticipantsLeavePromptMsg]; + } + + self->currentAlert = [UIAlertController alertControllerWithTitle:title + message:message + preferredStyle:UIAlertControllerStyleAlert]; + + [self->currentAlert addAction:[UIAlertAction actionWithTitle:[VectorL10n cancel] + style:UIAlertActionStyleCancel + handler:^(UIAlertAction * action) { + + MXStrongifyAndReturnIfNil(self); + self->currentAlert = nil; + + }]]; + + [self->currentAlert addAction:[UIAlertAction actionWithTitle:[VectorL10n leave] + style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) { + + MXStrongifyAndReturnIfNil(self); + self->currentAlert = nil; + + [self addPendingActionMask]; + MXWeakify(self); + [self.mxRoom leave:^{ + + MXStrongifyAndReturnIfNil(self); + [self withdrawViewControllerAnimated:YES completion:nil]; + + } failure:^(NSError *error) { + + MXStrongifyAndReturnIfNil(self); + [self removePendingActionMask]; + MXLogDebug(@"[RoomParticipantsVC] Leave room %@ failed", self.mxRoom.roomId); + // Alert user + [[AppDelegate theDelegate] showErrorAsAlert:error]; + + }]; + + }]]; + + [self->currentAlert mxk_setAccessibilityIdentifier:@"RoomParticipantsVCLeaveAlert"]; + [self presentViewController:self->currentAlert animated:YES completion:nil]; + } + }]; +} + - (void)onCancel:(id)sender { [self withdrawViewControllerAnimated:YES completion:nil];