Merge pull request #4814 from vector-im/steve/4734_room_vc_refacto

RoomVC: Add a delegate, factorize and reduce usage of AppDelegate
This commit is contained in:
SBiOSoftWhare
2021-09-17 15:58:05 +02:00
committed by GitHub
2 changed files with 310 additions and 80 deletions
+195 -67
View File
@@ -266,6 +266,12 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
bundle:[NSBundle bundleForClass:self.class]];
}
+ (instancetype)instantiate
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
return [storyboard instantiateViewControllerWithIdentifier:@"RoomViewControllerStoryboardId"];
}
#pragma mark -
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
@@ -664,7 +670,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
self.keyboardHeight = MAX(self.keyboardHeight, 0);
if (hasJitsiCall &&
![[AppDelegate theDelegate].callPresenter.jitsiVC.widget.roomId isEqualToString:self.roomDataSource.roomId])
!self.isRoomHavingAJitsiCall)
{
// the room had a Jitsi call before, but not now
hasJitsiCall = NO;
@@ -699,9 +705,8 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
[[NSNotificationCenter defaultCenter] removeObserver:mxEventDidDecryptNotificationObserver];
mxEventDidDecryptNotificationObserver = nil;
}
JitsiViewController *jitsiVC = [AppDelegate theDelegate].callPresenter.jitsiVC;
if ([jitsiVC.widget.roomId isEqualToString:self.roomDataSource.roomId])
if (self.isRoomHavingAJitsiCall)
{
hasJitsiCall = YES;
[self reloadBubblesTable:YES];
@@ -1089,7 +1094,14 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
[super leaveRoomOnEvent:event];
[[LegacyAppDelegate theDelegate] restoreInitialDisplay:nil];
if (self.delegate)
{
[self.delegate roomViewControllerDidLeaveRoom:self];
}
else
{
[[AppDelegate theDelegate] restoreInitialDisplay:nil];
}
}
// Set the input toolbar according to the current display
@@ -1204,15 +1216,14 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
{
// TODO: /join command does not support via parameters yet
[self.mainSession joinRoom:roomAlias viaServers:nil success:^(MXRoom *room) {
// Show the room
[[AppDelegate theDelegate] showRoom:room.roomId andEventId:nil withMatrixSession:self.mainSession];
[self showRoomWithId:room.roomId];
} failure:^(NSError *error) {
MXLogDebug(@"[RoomVC] Join roomAlias (%@) failed", roomAlias);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
@@ -1602,8 +1613,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
// video call button for Jitsi call
if (self.isCallActive)
{
JitsiViewController *jitsiVC = [AppDelegate theDelegate].callPresenter.jitsiVC;
if ([jitsiVC.widget.roomId isEqualToString:self.roomDataSource.roomId])
if (self.isRoomHavingAJitsiCall)
{
// show a disabled call button
UIBarButtonItem *item = [self videoCallBarButtonItem];
@@ -1999,7 +2009,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
} failure:^(NSError * _Nonnull error) {
MXLogDebug(@"[RoomVC] Cannot display widget %@", widget);
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
else
@@ -2098,6 +2108,125 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
}
}
- (void)showRoomWithId:(NSString*)roomId
{
if (self.delegate)
{
[self.delegate roomViewController:self showRoomWithId:roomId];
}
else
{
[[AppDelegate theDelegate] showRoom:roomId andEventId:nil withMatrixSession:self.roomDataSource.mxSession];
}
}
- (void)leaveRoom
{
[self startActivityIndicator];
[self.roomDataSource.room leave:^{
[self stopActivityIndicator];
// We remove the current view controller.
if (self.delegate)
{
[self.delegate roomViewControllerDidLeaveRoom:self];
}
else
{
[[AppDelegate theDelegate] restoreInitialDisplay:^{}];
}
} failure:^(NSError *error) {
[self stopActivityIndicator];
MXLogDebug(@"[RoomVC] Failed to reject an invited room (%@) failed", self.roomDataSource.room.roomId);
}];
}
- (void)roomPreviewDidTapCancelAction
{
// Decline this invitation = leave this page
if (self.delegate)
{
[self.delegate roomViewControllerPreviewDidTapCancel:self];
}
else
{
[[AppDelegate theDelegate] restoreInitialDisplay:^{}];
}
}
- (void)startChatWithUserId:(NSString *)userId completion:(void (^)(void))completion
{
if (self.delegate)
{
[self.delegate roomViewController:self startChatWithUserId:userId completion:completion];
}
else
{
[[AppDelegate theDelegate] createDirectChatWithUserId:userId completion:completion];
}
}
- (void)showError:(NSError*)error
{
[[AppDelegate theDelegate] showErrorAsAlert:error];
}
- (UIAlertController*)showAlertWithTitle:(NSString*)title message:(NSString*)message
{
return [[AppDelegate theDelegate] showAlertWithTitle:title message:message];
}
- (BOOL)handleUniversalLinkURL:(NSURL*)universalLinkURL
{
if (self.delegate)
{
return [self.delegate roomViewController:self handleUniversalLinkURL:universalLinkURL];
}
else
{
return [[AppDelegate theDelegate] handleUniversalLinkURL:universalLinkURL];
}
}
- (BOOL)handleUniversalLinkFragment:(NSString*)fragment fromURL:(NSURL*)universalLinkURL
{
if (self.delegate)
{
return [self.delegate roomViewController:self handleUniversalLinkFragment:fragment fromURL:universalLinkURL];
}
else
{
return [[AppDelegate theDelegate] handleUniversalLinkFragment:fragment fromURL:universalLinkURL];
}
}
#pragma mark - Jitsi
- (void)showJitsiCallWithWidget:(Widget*)widget
{
[[AppDelegate theDelegate].callPresenter displayJitsiCallWithWidget:widget];
}
- (void)endActiveJitsiCall
{
[[AppDelegate theDelegate].callPresenter endActiveJitsiCall];
}
- (BOOL)isRoomHavingAJitsiCall
{
return [self isRoomHavingAJitsiCallForWidgetId:self.roomDataSource.roomId];
}
- (BOOL)isRoomHavingAJitsiCallForWidgetId:(NSString*)widgetId
{
return [[AppDelegate theDelegate].callPresenter.jitsiVC.widget.roomId isEqualToString:widgetId];
}
#pragma mark - Dialpad
- (void)openDialpad
@@ -2645,7 +2774,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
if (predecessorRoomId)
{
// Show predecessor room
[[AppDelegate theDelegate] showRoom:predecessorRoomId andEventId:nil withMatrixSession:self.mainSession];
[self showRoomWithId:predecessorRoomId];
}
else
{
@@ -2721,7 +2850,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
[roomDataSource acceptVerificationRequestForEventId:eventId success:^{
} failure:^(NSError *error) {
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
else if ([actionIdentifier isEqualToString:kMXKRoomBubbleCellKeyVerificationIncomingRequestDeclinePressed])
@@ -2733,7 +2862,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
[roomDataSource declineVerificationRequestForEventId:eventId success:^{
} failure:^(NSError *error) {
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
else if ([actionIdentifier isEqualToString:kMXKRoomBubbleCellTapOnAttachmentView])
@@ -2863,7 +2992,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
Widget *jitsiWidget = [self->customizedRoomDataSource jitsiWidget];
if (jitsiWidget)
{
[[AppDelegate theDelegate].callPresenter displayJitsiCallWithWidget:jitsiWidget];
[self showJitsiCallWithWidget:jitsiWidget];
}
}
else
@@ -2879,7 +3008,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
}
else if ([actionIdentifier isEqualToString:RoomGroupCallStatusBubbleCell.leaveAction])
{
[[AppDelegate theDelegate].callPresenter endActiveJitsiCall];
[self endActiveJitsiCall];
[self reloadBubblesTable:YES];
}
else if ([actionIdentifier isEqualToString:RoomGroupCallStatusBubbleCell.declineAction])
@@ -3103,7 +3232,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
[self stopActivityIndicator];
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
@@ -3190,7 +3319,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
} failure:^(NSError *error) {
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
@@ -3264,7 +3393,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
MXLogDebug(@"[RoomVC] Redact event (%@) failed", selectedEvent.eventId);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
@@ -3415,7 +3544,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
MXLogDebug(@"[RoomVC] Ignore user (%@) failed", selectedEvent.sender);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
@@ -3441,7 +3570,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
MXLogDebug(@"[RoomVC] Report event (%@) failed", selectedEvent.eventId);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
@@ -3540,7 +3669,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
{
shouldDoAction = NO;
[[AppDelegate theDelegate] handleUniversalLinkURL:url];
[self handleUniversalLinkURL:url];
}
// Open a detail screen about the clicked user
else if ([MXTools isMatrixUserIdentifier:absoluteURLString])
@@ -3579,7 +3708,8 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
// Open the room or preview it
NSString *fragment = [NSString stringWithFormat:@"/room/%@", [MXTools encodeURIComponent:roomIdOrAlias]];
[[AppDelegate theDelegate] handleUniversalLinkFragment:fragment fromURL:url];
[self handleUniversalLinkFragment:fragment fromURL:url];
}
// Preview the clicked group
else if ([MXTools isMatrixGroupIdentifier:absoluteURLString])
@@ -3588,7 +3718,8 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
// Open the group or preview it
NSString *fragment = [NSString stringWithFormat:@"/group/%@", [MXTools encodeURIComponent:absoluteURLString]];
[[AppDelegate theDelegate] handleUniversalLinkFragment:fragment fromURL:url];
[self handleUniversalLinkFragment:fragment fromURL:url];
}
else if ([absoluteURLString hasPrefix:EventFormatterOnReRequestKeysLinkAction])
{
@@ -3744,8 +3875,8 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
- (void)showUnableToOpenLinkErrorAlert
{
[[AppDelegate theDelegate] showAlertWithTitle:[NSBundle mxk_localizedStringForKey:@"error"]
message:NSLocalizedStringFromTable(@"room_message_unable_open_link_error_message", @"Vector", nil)];
[self showAlertWithTitle:[NSBundle mxk_localizedStringForKey:@"error"]
message:NSLocalizedStringFromTable(@"room_message_unable_open_link_error_message", @"Vector", nil)];
}
- (void)editEventContentWithId:(NSString*)eventId
@@ -3937,7 +4068,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
if (jitsiWidget)
{
// If there is already a Jitsi call, join it
[[AppDelegate theDelegate].callPresenter displayJitsiCallWithWidget:jitsiWidget];
[self showJitsiCallWithWidget:jitsiWidget];
}
else
{
@@ -3964,7 +4095,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
MXStrongifyAndReturnIfNil(self);
[self stopActivityIndicator];
[[AppDelegate theDelegate].callPresenter displayJitsiCallWithWidget:jitsiWidget];
[self showJitsiCallWithWidget:jitsiWidget];
}
failure:^(NSError *error)
{
@@ -4006,9 +4137,9 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
{
[callInRoom hangup];
}
else if ([[AppDelegate theDelegate].callPresenter.jitsiVC.widget.roomId isEqualToString:self.roomDataSource.roomId])
else if (self.isRoomHavingAJitsiCall)
{
[[AppDelegate theDelegate].callPresenter endActiveJitsiCall];
[self endActiveJitsiCall];
[self reloadBubblesTable:YES];
}
@@ -4068,7 +4199,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
- (void)roomMemberDetailsViewController:(MXKRoomMemberDetailsViewController *)roomMemberDetailsViewController startChatWithMemberId:(NSString *)matrixId completion:(void (^)(void))completion
{
[[AppDelegate theDelegate] createDirectChatWithUserId:matrixId completion:completion];
[self startChatWithUserId:matrixId completion:completion];
}
- (void)roomMemberDetailsViewController:(MXKRoomMemberDetailsViewController *)roomMemberDetailsViewController mention:(MXRoomMember*)member
@@ -4363,27 +4494,11 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
// 'Decline' button has been pressed
if (roomPreviewData)
{
// Decline this invitation = leave this page
[[AppDelegate theDelegate] restoreInitialDisplay:^{}];
[self roomPreviewDidTapCancelAction];
}
else
{
[self startActivityIndicator];
[self.roomDataSource.room leave:^{
[self stopActivityIndicator];
// We remove the current view controller.
// Pop to homes view controller
[[AppDelegate theDelegate] restoreInitialDisplay:^{}];
} failure:^(NSError *error) {
[self stopActivityIndicator];
MXLogDebug(@"[RoomVC] Failed to reject an invited room (%@) failed", self.roomDataSource.room.roomId);
}];
[self leaveRoom];
}
}
}
@@ -4626,7 +4741,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
}
// Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}
- (NSUInteger)widgetsCount:(BOOL)includeUserWidgets
@@ -4686,7 +4801,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
if ([self.roomDataSource.mxSession roomWithRoomId:replacementRoomId])
{
// Open the room if it is already joined
[[AppDelegate theDelegate] showRoom:replacementRoomId andEventId:nil withMatrixSession:self.roomDataSource.mxSession];
[self showRoomWithId:replacementRoomId];
}
else
{
@@ -4701,15 +4816,15 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
[self startActivityIndicator];
[self.roomDataSource.mxSession joinRoom:replacementRoomId viaServers:@[viaSenderServer] success:^(MXRoom *room) {
[self stopActivityIndicator];
[[AppDelegate theDelegate] showRoom:replacementRoomId andEventId:nil withMatrixSession:self.roomDataSource.mxSession];
[self showRoomWithId:replacementRoomId];
} failure:^(NSError *error) {
[self stopActivityIndicator];
MXLogDebug(@"[RoomVC] Failed to join an upgraded room. Error: %@",
error);
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
}
@@ -5398,7 +5513,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
MXLogDebug(@"[RoomVC] Invite %@ failed", participantId);
// Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
@@ -5433,11 +5548,11 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
&& error.code == MXRestClientErrorMissingIdentityServer)
{
NSString *message = [NSBundle mxk_localizedStringForKey:@"error_invite_3pid_with_no_identity_server"];
[[AppDelegate theDelegate] showAlertWithTitle:message message:nil];
[self showAlertWithTitle:message message:nil];
}
else
{
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}
}];
}
@@ -5452,7 +5567,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
MXLogDebug(@"[RoomVC] Invite %@ failed", participantId);
// Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
}
@@ -5550,10 +5665,16 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
- (void)showSettingsSecurityScreen
{
[[AppDelegate theDelegate] presentCompleteSecurityForSession: self.mainSession];
if (self.delegate)
{
[self.delegate roomViewController:self showCompleteSecurityForSession:self.mainSession];
}
else
{
[[AppDelegate theDelegate] presentCompleteSecurityForSession: self.mainSession];
}
}
#pragma mark Tombstone event
- (void)listenTombstoneEventNotifications
@@ -5914,7 +6035,7 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
[self stopActivityIndicator];
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self showError:error];
}];
// Start animation in case of download during attachment preparing
@@ -6094,8 +6215,8 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
{
MXLogDebug(@"[MXKRoomViewController] File upload using MIME type %@ is not supported.", mimeType);
[[AppDelegate theDelegate] showAlertWithTitle:NSLocalizedStringFromTable(@"file_upload_error_title", @"Vector", nil)
message:NSLocalizedStringFromTable(@"file_upload_error_unsupported_file_type_message", @"Vector", nil)];
[self showAlertWithTitle:NSLocalizedStringFromTable(@"file_upload_error_title", @"Vector", nil)
message:NSLocalizedStringFromTable(@"file_upload_error_unsupported_file_type_message", @"Vector", nil)];
}
}
@@ -6250,7 +6371,14 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
- (void)roomInfoCoordinatorBridgePresenterDelegateDidLeaveRoom:(RoomInfoCoordinatorBridgePresenter *)coordinatorBridgePresenter
{
[[LegacyAppDelegate theDelegate] restoreInitialDisplay:nil];
if (self.delegate)
{
[self.delegate roomViewControllerDidLeaveRoom:self];
}
else
{
[[AppDelegate theDelegate] restoreInitialDisplay:nil];
}
}
#pragma mark - RemoveJitsiWidgetViewDelegate
@@ -6275,9 +6403,9 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
self.removeJitsiWidgetView.delegate = nil;
// end active call if exists
if ([[AppDelegate theDelegate].callPresenter.jitsiVC.widget.widgetId isEqualToString:jitsiWidget.widgetId])
if ([self isRoomHavingAJitsiCallForWidgetId:jitsiWidget.widgetId])
{
[[AppDelegate theDelegate].callPresenter endActiveJitsiCall];
[self endActiveJitsiCall];
}
} failure:^(NSError *error) {
MXStrongifyAndReturnIfNil(self);