From 35861aa095227c96398f39a79e67d7ecccbab165 Mon Sep 17 00:00:00 2001 From: Ivan Sein Date: Mon, 25 May 2020 14:57:22 +0200 Subject: [PATCH] Stop/Resume chat owned by rooms manager when start/end a call. Signed-off-by: Ivan Sein --- VideoCalls/NCChatViewController.h | 2 ++ VideoCalls/NCChatViewController.m | 58 +++++++++++++++++++++---------- VideoCalls/NCRoomsManager.m | 7 ++++ 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/VideoCalls/NCChatViewController.h b/VideoCalls/NCChatViewController.h index 57fd6ef3..75a22365 100644 --- a/VideoCalls/NCChatViewController.h +++ b/VideoCalls/NCChatViewController.h @@ -15,6 +15,8 @@ @property (nonatomic, strong) NCRoom *room; - (instancetype)initForRoom:(NCRoom *)room; +- (void)stopChat; +- (void)resumeChat; - (void)leaveChat; @end diff --git a/VideoCalls/NCChatViewController.m b/VideoCalls/NCChatViewController.m index 3651cc01..f595c41d 100644 --- a/VideoCalls/NCChatViewController.m +++ b/VideoCalls/NCChatViewController.m @@ -65,6 +65,7 @@ typedef enum NCChatMessageAction { @property (nonatomic, assign) BOOL leftChatWithVisibleChatVC; @property (nonatomic, assign) BOOL offlineMode; @property (nonatomic, assign) BOOL hasStoredHistory; +@property (nonatomic, assign) BOOL hasStopped; @property (nonatomic, assign) NSInteger lastReadMessage; @property (nonatomic, strong) NCChatMessage *unreadMessagesSeparator; @property (nonatomic, strong) NSIndexPath *unreadMessagesSeparatorIP; @@ -271,6 +272,22 @@ typedef enum NCChatMessageAction { } } +- (void)stopChat +{ + _hasStopped = YES; + [_chatController stopChatController]; + [self cleanChat]; +} + +- (void)resumeChat +{ + _hasStopped = NO; + if (!_hasReceiveInitialHistory && !_hasRequestedInitialHistory) { + _hasRequestedInitialHistory = YES; + [_chatController getInitialChatHistory]; + } +} + - (void)leaveChat { [_lobbyCheckTimer invalidate]; @@ -286,6 +303,24 @@ typedef enum NCChatMessageAction { } } +#pragma mark - App lifecycle notifications + +-(void)appDidBecomeActive:(NSNotification*)notification +{ + [self removeUnreadMessagesSeparator]; + if (!_offlineMode) { + [[NCRoomsManager sharedInstance] joinRoom:_room.token]; + } +} + +-(void)appWillResignActive:(NSNotification*)notification +{ + _hasReceiveNewMessages = NO; + _leftChatWithVisibleChatVC = YES; + [_chatController stopChatController]; + [[NCRoomsManager sharedInstance] leaveChatInRoom:_room.token]; +} + #pragma mark - Configuration - (void)setTitleView @@ -771,24 +806,6 @@ typedef enum NCChatMessageAction { return [super textView:textView shouldChangeTextInRange:range replacementText:text]; } -#pragma mark - App lifecycle notifications - --(void)appDidBecomeActive:(NSNotification*)notification -{ - [self removeUnreadMessagesSeparator]; - if (!_offlineMode) { - [[NCRoomsManager sharedInstance] joinRoom:_room.token]; - } -} - --(void)appWillResignActive:(NSNotification*)notification -{ - _hasReceiveNewMessages = NO; - _leftChatWithVisibleChatVC = YES; - [_chatController stopChatController]; - [[NCRoomsManager sharedInstance] leaveChatInRoom:_room.token]; -} - #pragma mark - Room Manager notifications - (void)didUpdateRoom:(NSNotification *)notification @@ -822,6 +839,10 @@ typedef enum NCChatMessageAction { _hasJoinedRoom = YES; [self checkRoomControlsAvailability]; + if (_hasStopped) { + return; + } + if (_leftChatWithVisibleChatVC && _hasReceiveInitialHistory) { _leftChatWithVisibleChatVC = NO; [_chatController startReceivingNewChatMessages]; @@ -1372,6 +1393,7 @@ typedef enum NCChatMessageAction { _messages = [[NSMutableDictionary alloc] init]; _dateSections = [[NSMutableArray alloc] init]; _hasReceiveInitialHistory = NO; + _hasRequestedInitialHistory = NO; _hasReceiveNewMessages = NO; _unreadMessagesSeparatorIP = nil; [self hideNewMessagesView]; diff --git a/VideoCalls/NCRoomsManager.m b/VideoCalls/NCRoomsManager.m index 36a5714d..6916f18e 100644 --- a/VideoCalls/NCRoomsManager.m +++ b/VideoCalls/NCRoomsManager.m @@ -419,6 +419,9 @@ NSString * const NCRoomsManagerDidReceiveChatMessagesNotification = @"ChatMess extSignalingController.currentRoom = nil; } } + if ([_chatViewController.room.token isEqualToString:room.token]) { + [_chatViewController stopChat]; + } [[NCUserInterfaceController sharedInstance] presentCallViewController:_callViewController]; [self joinRoom:room.token forCall:YES]; } else { @@ -478,6 +481,10 @@ NSString * const NCRoomsManagerDidReceiveChatMessagesNotification = @"ChatMess } [[CallKitManager sharedInstance] endCurrentCall]; [self leaveRoom:room.token]; + + if ([_chatViewController.room.token isEqualToString:room.token]) { + [_chatViewController resumeChat]; + } } #pragma mark - CallViewControllerDelegate