From 2a56c5a8c7b4f36b8b787a075aef517432a7daa1 Mon Sep 17 00:00:00 2001 From: ismailgulek Date: Thu, 19 Aug 2021 19:33:43 +0300 Subject: [PATCH 1/2] Adapt to new async methods --- Riot/Categories/MXRoom+Riot.h | 3 +- Riot/Categories/MXRoom+Riot.m | 38 +- .../Recents/DataSources/RecentsDataSource.m | 314 ++++++++------- .../Recents/Views/RecentTableViewCell.m | 4 +- .../Home/Views/RoomCollectionViewCell.m | 53 +-- Riot/Modules/Room/RoomViewController.m | 373 +++++++++--------- 6 files changed, 414 insertions(+), 371 deletions(-) diff --git a/Riot/Categories/MXRoom+Riot.h b/Riot/Categories/MXRoom+Riot.h index dcf4860a39..fe291c934b 100644 --- a/Riot/Categories/MXRoom+Riot.h +++ b/Riot/Categories/MXRoom+Riot.h @@ -52,7 +52,8 @@ typedef NS_ENUM(NSUInteger, RoomSentStatus) @property (nonatomic) id notificationCenterDidUpdateObserver; /// Check if all messages have been sent. -@property (nonatomic, readonly) RoomSentStatus sentStatus; +/// @param completion Completion block +- (void)sentStatusWithCompletion:(void (^)(RoomSentStatus))completion; /** Update the room tag. diff --git a/Riot/Categories/MXRoom+Riot.m b/Riot/Categories/MXRoom+Riot.m index 273f86a827..d7b23dfc83 100644 --- a/Riot/Categories/MXRoom+Riot.m +++ b/Riot/Categories/MXRoom+Riot.m @@ -658,28 +658,32 @@ - (id)notificationCenterDidUpdateObserver #pragma mark - Unread messages -- (RoomSentStatus)sentStatus +- (void)sentStatusWithCompletion:(void (^)(RoomSentStatus))completion { - RoomSentStatus status = RoomSentStatusOk; - NSArray *outgoingMsgs = self.outgoingMessages; - - for (MXEvent *event in outgoingMsgs) - { - if (event.sentState == MXEventSentStateFailed) + [self outgoingMessagesWithCompletion:^(NSArray * _Nullable outgoingMsgs) { + RoomSentStatus status = RoomSentStatusOk; + + for (MXEvent *event in outgoingMsgs) { - status = RoomSentStatusSentFailed; - - // Check if the error is due to unknown devices - if ([event.sentError.domain isEqualToString:MXEncryptingErrorDomain] - && event.sentError.code == MXEncryptingErrorUnknownDeviceCode) + if (event.sentState == MXEventSentStateFailed) { - status = RoomSentStatusSentFailedDueToUnknownDevices; - break; + status = RoomSentStatusSentFailed; + + // Check if the error is due to unknown devices + if ([event.sentError.domain isEqualToString:MXEncryptingErrorDomain] + && event.sentError.code == MXEncryptingErrorUnknownDeviceCode) + { + status = RoomSentStatusSentFailedDueToUnknownDevices; + break; + } } } - } - - return status; + + if (completion) + { + completion(status); + } + }]; } @end diff --git a/Riot/Modules/Common/Recents/DataSources/RecentsDataSource.m b/Riot/Modules/Common/Recents/DataSources/RecentsDataSource.m index 123d9da690..109b97e45c 100644 --- a/Riot/Modules/Common/Recents/DataSources/RecentsDataSource.m +++ b/Riot/Modules/Common/Recents/DataSources/RecentsDataSource.m @@ -1160,16 +1160,21 @@ - (void)computeStateAsyncWithCells:(NSArray> *)cell onComplete:(void (^)(RecentsDataSourceState *newState))onComplete { dispatch_async(processingQueue, ^{ - RecentsDataSourceState *newState = [RecentsDataSource computeStateWithCells:cells recentsDataSourceMode:recentsDataSourceMode matrixSession:mxSession]; - dispatch_async(dispatch_get_main_queue(), ^{ - onComplete(newState); - }); + [RecentsDataSource computeStateWithCells:cells + recentsDataSourceMode:recentsDataSourceMode + matrixSession:mxSession + completion:^(RecentsDataSourceState * newState) { + dispatch_async(dispatch_get_main_queue(), ^{ + onComplete(newState); + }); + }]; }); } -+ (RecentsDataSourceState *)computeStateWithCells:(NSArray> *)cells - recentsDataSourceMode:(RecentsDataSourceMode)recentsDataSourceMode - matrixSession:(MXSession*)mxSession ++ (void)computeStateWithCells:(NSArray> *)cells + recentsDataSourceMode:(RecentsDataSourceMode)recentsDataSourceMode + matrixSession:(MXSession*)mxSession + completion:(void (^)(RecentsDataSourceState *))completion { NSDate *startDate = [NSDate date]; @@ -1183,8 +1188,13 @@ + (RecentsDataSourceState *)computeStateWithCells:(NSArray sentStatus map + __block NSMutableDictionary *sentStatusesMap = [NSMutableDictionary dictionaryWithCapacity:cells.count]; for (id recentCellDataStoring in cells) { @@ -1324,74 +1334,123 @@ + (RecentsDataSourceState *)computeStateWithCells:(NSArray recentCellData1, id recentCellData2) { - - if (recentCellData1.roomSummary.room.sentStatus != RoomSentStatusOk - && recentCellData2.roomSummary.room.sentStatus == RoomSentStatusOk) + if (room.isDirect) { - return NSOrderedAscending; + unsentMessagesDirectDiscussionsCount ++; } - - if (recentCellData2.roomSummary.room.sentStatus != RoomSentStatusOk - && recentCellData1.roomSummary.room.sentStatus == RoomSentStatusOk) + else { - return NSOrderedDescending; + unsentMessagesGroupDiscussionsCount ++; } - - if (recentCellData1.highlightCount) - { - if (recentCellData2.highlightCount) + } + sentStatusesMap[room.roomId] = @(sentStatus); + dispatch_group_leave(dispatchGroup); + }]; + } + + dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{ + if (recentsDataSourceMode == RecentsDataSourceModeHome) + { + BOOL pinMissedNotif = RiotSettings.shared.pinRoomsWithMissedNotificationsOnHome; + BOOL pinUnread = RiotSettings.shared.pinRoomsWithUnreadMessagesOnHome; + NSComparator comparator = nil; + + if (pinMissedNotif) + { + // Sort each rooms collection by considering first the rooms with some missed notifs, the rooms with unread, then the others. + comparator = ^NSComparisonResult(id recentCellData1, id recentCellData2) { + + RoomSentStatus sentStatus1 = (RoomSentStatus)[sentStatusesMap[recentCellData1.roomSummary.roomId] integerValue]; + RoomSentStatus sentStatus2 = (RoomSentStatus)[sentStatusesMap[recentCellData2.roomSummary.roomId] integerValue]; + + if (sentStatus1 != RoomSentStatusOk + && sentStatus2 == RoomSentStatusOk) { - return NSOrderedSame; + return NSOrderedAscending; } - else + + if (sentStatus2 != RoomSentStatusOk + && sentStatus1 == RoomSentStatusOk) { - return NSOrderedAscending; + return NSOrderedDescending; } - } - else if (recentCellData2.highlightCount) - { - return NSOrderedDescending; - } - else if (recentCellData1.notificationCount) - { - if (recentCellData2.notificationCount) + + if (recentCellData1.highlightCount) { - return NSOrderedSame; + if (recentCellData2.highlightCount) + { + return NSOrderedSame; + } + else + { + return NSOrderedAscending; + } } - else + else if (recentCellData2.highlightCount) + { + return NSOrderedDescending; + } + else if (recentCellData1.notificationCount) + { + if (recentCellData2.notificationCount) + { + return NSOrderedSame; + } + else + { + return NSOrderedAscending; + } + } + else if (recentCellData2.notificationCount) + { + return NSOrderedDescending; + } + else if (pinUnread) + { + if (recentCellData1.hasUnread) + { + if (recentCellData2.hasUnread) + { + return NSOrderedSame; + } + else + { + return NSOrderedAscending; + } + } + else if (recentCellData2.hasUnread) + { + return NSOrderedDescending; + } + } + + return NSOrderedSame; + }; + } + else if (pinUnread) + { + // Sort each rooms collection by considering first the rooms with some unread messages then the others. + comparator = ^NSComparisonResult(id recentCellData1, id recentCellData2) { + + RoomSentStatus sentStatus1 = (RoomSentStatus)[sentStatusesMap[recentCellData1.roomSummary.roomId] integerValue]; + RoomSentStatus sentStatus2 = (RoomSentStatus)[sentStatusesMap[recentCellData2.roomSummary.roomId] integerValue]; + + if (sentStatus1 != RoomSentStatusOk + && sentStatus2 == RoomSentStatusOk) { return NSOrderedAscending; } - } - else if (recentCellData2.notificationCount) - { - return NSOrderedDescending; - } - else if (pinUnread) - { + + if (sentStatus2 != RoomSentStatusOk + && sentStatus1 == RoomSentStatusOk) + { + return NSOrderedDescending; + } + if (recentCellData1.hasUnread) { if (recentCellData2.hasUnread) @@ -1407,101 +1466,68 @@ + (RecentsDataSourceState *)computeStateWithCells:(NSArray 0 && recentsDataSourceMode == RecentsDataSourceModeFavourites) + { + // Sort them according to their tag order + [favoriteCellDataArray sortUsingComparator:^NSComparisonResult(id recentCellData1, id recentCellData2) { + + return [mxSession compareRoomsByTag:kMXRoomTagFavourite room1:recentCellData1.roomSummary.room room2:recentCellData2.roomSummary.room]; - return NSOrderedSame; - }; + }]; } - else if (pinUnread) + else if (conversationCellDataArray.count > 0 && (recentsDataSourceMode == RecentsDataSourceModeRooms || recentsDataSourceMode == RecentsDataSourceModePeople)) { - // Sort each rooms collection by considering first the rooms with some unread messages then the others. - comparator = ^NSComparisonResult(id recentCellData1, id recentCellData2) { + [conversationCellDataArray sortUsingComparator:^NSComparisonResult(id recentCellData1, id recentCellData2) { - if (recentCellData1.roomSummary.room.sentStatus != RoomSentStatusOk - && recentCellData2.roomSummary.room.sentStatus == RoomSentStatusOk) - { - return NSOrderedAscending; - } + RoomSentStatus sentStatus1 = (RoomSentStatus)[sentStatusesMap[recentCellData1.roomSummary.roomId] integerValue]; + RoomSentStatus sentStatus2 = (RoomSentStatus)[sentStatusesMap[recentCellData2.roomSummary.roomId] integerValue]; - if (recentCellData2.roomSummary.room.sentStatus != RoomSentStatusOk - && recentCellData1.roomSummary.room.sentStatus == RoomSentStatusOk) + if (sentStatus1 != RoomSentStatusOk + && sentStatus2 == RoomSentStatusOk) { - return NSOrderedDescending; + return NSOrderedAscending; } - if (recentCellData1.hasUnread) - { - if (recentCellData2.hasUnread) - { - return NSOrderedSame; - } - else - { - return NSOrderedAscending; - } - } - else if (recentCellData2.hasUnread) + if (sentStatus2 != RoomSentStatusOk + && sentStatus1 == RoomSentStatusOk) { return NSOrderedDescending; } - return NSOrderedSame; - }; + return NSOrderedAscending; + }]; } - if (comparator) - { - // Sort the rooms collections - [favoriteCellDataArray sortUsingComparator:comparator]; - [peopleCellDataArray sortUsingComparator:comparator]; - [conversationCellDataArray sortUsingComparator:comparator]; - [lowPriorityCellDataArray sortUsingComparator:comparator]; - [serverNoticeCellDataArray sortUsingComparator:comparator]; - } - } - else if (favoriteCellDataArray.count > 0 && recentsDataSourceMode == RecentsDataSourceModeFavourites) - { - // Sort them according to their tag order - [favoriteCellDataArray sortUsingComparator:^NSComparisonResult(id recentCellData1, id recentCellData2) { - - return [mxSession compareRoomsByTag:kMXRoomTagFavourite room1:recentCellData1.roomSummary.room room2:recentCellData2.roomSummary.room]; - - }]; - } - else if (conversationCellDataArray.count > 0 && (recentsDataSourceMode == RecentsDataSourceModeRooms || recentsDataSourceMode == RecentsDataSourceModePeople)) - { - [conversationCellDataArray sortUsingComparator:^NSComparisonResult(id recentCellData1, id recentCellData2) { - - if (recentCellData1.roomSummary.room.sentStatus != RoomSentStatusOk - && recentCellData2.roomSummary.room.sentStatus == RoomSentStatusOk) - { - return NSOrderedAscending; - } - - if (recentCellData2.roomSummary.room.sentStatus != RoomSentStatusOk - && recentCellData1.roomSummary.room.sentStatus == RoomSentStatusOk) - { - return NSOrderedDescending; - } - - return NSOrderedAscending; - }]; - } - - MXLogDebug(@"[RecentsDataSource] refreshRoomsSections: Done in %.0fms", [[NSDate date] timeIntervalSinceDate:startDate] * 1000); - - return [[RecentsDataSourceState alloc] - initWithInvitesCellDataArray:invitesCellDataArray - favoriteCellDataArray:favoriteCellDataArray - peopleCellDataArray:peopleCellDataArray - conversationCellDataArray:conversationCellDataArray - lowPriorityCellDataArray:lowPriorityCellDataArray - serverNoticeCellDataArray:serverNoticeCellDataArray - favouriteMissedDiscussionsCount:favouriteMissedDiscussionsCount - directMissedDiscussionsCount:directMissedDiscussionsCount - groupMissedDiscussionsCount:groupMissedDiscussionsCount - unsentMessagesDirectDiscussionsCount:unsentMessagesDirectDiscussionsCount - unsentMessagesGroupDiscussionsCount:unsentMessagesGroupDiscussionsCount]; + MXLogDebug(@"[RecentsDataSource] refreshRoomsSections: Done in %.0fms", [[NSDate date] timeIntervalSinceDate:startDate] * 1000); + + completion([[RecentsDataSourceState alloc] + initWithInvitesCellDataArray:invitesCellDataArray + favoriteCellDataArray:favoriteCellDataArray + peopleCellDataArray:peopleCellDataArray + conversationCellDataArray:conversationCellDataArray + lowPriorityCellDataArray:lowPriorityCellDataArray + serverNoticeCellDataArray:serverNoticeCellDataArray + favouriteMissedDiscussionsCount:favouriteMissedDiscussionsCount + directMissedDiscussionsCount:directMissedDiscussionsCount + groupMissedDiscussionsCount:groupMissedDiscussionsCount + unsentMessagesDirectDiscussionsCount:unsentMessagesDirectDiscussionsCount + unsentMessagesGroupDiscussionsCount:unsentMessagesGroupDiscussionsCount]); + }); } - (void)dataSource:(MXKDataSource*)dataSource didCellChange:(id)changes diff --git a/Riot/Modules/Common/Recents/Views/RecentTableViewCell.m b/Riot/Modules/Common/Recents/Views/RecentTableViewCell.m index 337c39cbac..9955b70837 100644 --- a/Riot/Modules/Common/Recents/Views/RecentTableViewCell.m +++ b/Riot/Modules/Common/Recents/Views/RecentTableViewCell.m @@ -88,7 +88,9 @@ - (void)render:(MXKCellData *)cellData self.lastEventDescription.text = roomCellData.lastEventTextMessage; } - self.unsentImageView.hidden = roomCellData.roomSummary.room.sentStatus == RoomSentStatusOk; + [roomCellData.roomSummary.room sentStatusWithCompletion:^(RoomSentStatus sentStatus) { + self.unsentImageView.hidden = sentStatus == RoomSentStatusOk; + }]; self.lastEventDecriptionLabelTrailingConstraint.constant = self.unsentImageView.hidden ? 10 : 30; // Notify unreads and bing diff --git a/Riot/Modules/Home/Views/RoomCollectionViewCell.m b/Riot/Modules/Home/Views/RoomCollectionViewCell.m index 33b263a806..a6de9319bd 100644 --- a/Riot/Modules/Home/Views/RoomCollectionViewCell.m +++ b/Riot/Modules/Home/Views/RoomCollectionViewCell.m @@ -100,35 +100,36 @@ - (void)render:(MXKCellData *)cellData } } - // Notify unreads and bing - if (roomCellData.roomSummary.room.summary.membership == MXMembershipInvite - || roomCellData.roomSummary.room.sentStatus != RoomSentStatusOk) - { - self.badgeLabel.hidden = NO; - self.badgeLabel.badgeColor = ThemeService.shared.theme.noticeColor; - self.badgeLabel.text = @"!"; - - // Use bold font for the room title - self.roomTitle.font = self.roomTitle1.font = self.roomTitle2.font = [UIFont systemFontOfSize:13 weight:UIFontWeightBold]; - } - else if (roomCellData.hasUnread) - { - if (0 < roomCellData.notificationCount) + [roomCellData.roomSummary.room sentStatusWithCompletion:^(RoomSentStatus sentStatus) { + // Notify unreads and bing + if (self->roomCellData.roomSummary.room.summary.membership == MXMembershipInvite + || sentStatus != RoomSentStatusOk) { self.badgeLabel.hidden = NO; - self.badgeLabel.badgeColor = roomCellData.highlightCount ? ThemeService.shared.theme.noticeColor : ThemeService.shared.theme.noticeSecondaryColor; - self.badgeLabel.text = roomCellData.notificationCountStringValue; + self.badgeLabel.badgeColor = ThemeService.shared.theme.noticeColor; + self.badgeLabel.text = @"!"; + + // Use bold font for the room title + self.roomTitle.font = self.roomTitle1.font = self.roomTitle2.font = [UIFont systemFontOfSize:13 weight:UIFontWeightBold]; } - - // Use bold font for the room title - self.roomTitle.font = self.roomTitle1.font = self.roomTitle2.font = [UIFont systemFontOfSize:13 weight:UIFontWeightBold]; - } - else - { - // The room title is not bold anymore - self.roomTitle.font = self.roomTitle1.font = self.roomTitle2.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]; - - } + else if (self->roomCellData.hasUnread) + { + if (0 < self->roomCellData.notificationCount) + { + self.badgeLabel.hidden = NO; + self.badgeLabel.badgeColor = self->roomCellData.highlightCount ? ThemeService.shared.theme.noticeColor : ThemeService.shared.theme.noticeSecondaryColor; + self.badgeLabel.text = self->roomCellData.notificationCountStringValue; + } + + // Use bold font for the room title + self.roomTitle.font = self.roomTitle1.font = self.roomTitle2.font = [UIFont systemFontOfSize:13 weight:UIFontWeightBold]; + } + else + { + // The room title is not bold anymore + self.roomTitle.font = self.roomTitle1.font = self.roomTitle2.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]; + } + }]; [roomCellData.roomSummary setRoomAvatarImageIn:self.roomAvatar]; } diff --git a/Riot/Modules/Room/RoomViewController.m b/Riot/Modules/Room/RoomViewController.m index faff892292..d9199b13d4 100644 --- a/Riot/Modules/Room/RoomViewController.m +++ b/Riot/Modules/Room/RoomViewController.m @@ -4621,47 +4621,52 @@ - (void)refreshActivitiesViewDisplay } }]; } - else if ([self checkUnsentMessages] == NO) + else { - // Show "scroll to bottom" icon when the most recent message is not visible, - // or when the timelime is not live (this icon is used to go back to live). - // Note: we check if `currentEventIdAtTableBottom` is set to know whether the table has been rendered at least once. - if (!self.roomDataSource.isLive || (currentEventIdAtTableBottom && [self isBubblesTableScrollViewAtTheBottom] == NO)) - { - if (self.roomDataSource.room) + [self checkUnsentMessagesWithCompletion:^(BOOL result) { + if (result == NO) { - // Retrieve the unread messages count - NSUInteger unreadCount = self.roomDataSource.room.summary.localUnreadEventCount; - - self.scrollToBottomBadgeLabel.text = unreadCount ? [NSString stringWithFormat:@"%lu", unreadCount] : nil; - self.scrollToBottomHidden = NO; - } - else - { - // will be here for left rooms - self.scrollToBottomBadgeLabel.text = nil; - self.scrollToBottomHidden = YES; - } - } - else if (serverNotices.usageLimit && serverNotices.usageLimit.isServerNoticeUsageLimit) - { - self.scrollToBottomHidden = YES; - self.activitiesViewExpanded = YES; - [roomActivitiesView showResourceUsageLimitNotice:serverNotices.usageLimit onAdminContactTapped:^(NSURL *adminContactURL) { - [[UIApplication sharedApplication] vc_open:adminContactURL completionHandler:^(BOOL success) { - if (!success) + // Show "scroll to bottom" icon when the most recent message is not visible, + // or when the timelime is not live (this icon is used to go back to live). + // Note: we check if `currentEventIdAtTableBottom` is set to know whether the table has been rendered at least once. + if (!self.roomDataSource.isLive || (self->currentEventIdAtTableBottom && [self isBubblesTableScrollViewAtTheBottom] == NO)) + { + if (self.roomDataSource.room) { - MXLogDebug(@"[RoomVC] refreshActivitiesViewDisplay: adminContact(%@) cannot be opened", adminContactURL); + // Retrieve the unread messages count + NSUInteger unreadCount = self.roomDataSource.room.summary.localUnreadEventCount; + + self.scrollToBottomBadgeLabel.text = unreadCount ? [NSString stringWithFormat:@"%lu", unreadCount] : nil; + self.scrollToBottomHidden = NO; } - }]; - }]; - } - else - { - self.scrollToBottomHidden = YES; - self.activitiesViewExpanded = NO; - [self refreshTypingNotification]; - } + else + { + // will be here for left rooms + self.scrollToBottomBadgeLabel.text = nil; + self.scrollToBottomHidden = YES; + } + } + else if (self->serverNotices.usageLimit && self->serverNotices.usageLimit.isServerNoticeUsageLimit) + { + self.scrollToBottomHidden = YES; + self.activitiesViewExpanded = YES; + [roomActivitiesView showResourceUsageLimitNotice:self->serverNotices.usageLimit onAdminContactTapped:^(NSURL *adminContactURL) { + [[UIApplication sharedApplication] vc_open:adminContactURL completionHandler:^(BOOL success) { + if (!success) + { + MXLogDebug(@"[RoomVC] refreshActivitiesViewDisplay: adminContact(%@) cannot be opened", adminContactURL); + } + }]; + }]; + } + else + { + self.scrollToBottomHidden = YES; + self.activitiesViewExpanded = NO; + [self refreshTypingNotification]; + } + } + }]; } // Recognize swipe downward to dismiss keyboard if any @@ -4782,87 +4787,92 @@ - (void)refreshMissedDiscussionsCount:(BOOL)force #pragma mark - Unsent Messages Handling --(BOOL)checkUnsentMessages +-(void)checkUnsentMessagesWithCompletion:(void (^)(BOOL))completion; { - RoomSentStatus sentStatus = RoomSentStatusOk; if ([self.activitiesView isKindOfClass:RoomActivitiesView.class]) { - sentStatus = self.roomDataSource.room.sentStatus; - - if (sentStatus != RoomSentStatusOk) - { - NSString *notification = sentStatus == RoomSentStatusSentFailedDueToUnknownDevices ? - NSLocalizedStringFromTable(@"room_unsent_messages_unknown_devices_notification", @"Vector", nil) : - NSLocalizedStringFromTable(@"room_unsent_messages_notification", @"Vector", nil); - - RoomActivitiesView *roomActivitiesView = (RoomActivitiesView*) self.activitiesView; - self.activitiesViewExpanded = YES; - [roomActivitiesView displayUnsentMessagesNotification:notification withResendLink:^{ - - [self resendAllUnsentMessages]; - - } andCancelLink:^{ - - [self cancelAllUnsentMessages]; - - } andIconTapGesture:^{ - - if (currentAlert) - { - [currentAlert dismissViewControllerAnimated:NO completion:nil]; - } - - __weak __typeof(self) weakSelf = self; - currentAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; + [self.roomDataSource.room sentStatusWithCompletion:^(RoomSentStatus sentStatus) { + if (sentStatus != RoomSentStatusOk) + { + NSString *notification = sentStatus == RoomSentStatusSentFailedDueToUnknownDevices ? + NSLocalizedStringFromTable(@"room_unsent_messages_unknown_devices_notification", @"Vector", nil) : + NSLocalizedStringFromTable(@"room_unsent_messages_notification", @"Vector", nil); - [currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"room_resend_unsent_messages", @"Vector", nil) - style:UIAlertActionStyleDefault - handler:^(UIAlertAction * action) { + RoomActivitiesView *roomActivitiesView = (RoomActivitiesView*) self.activitiesView; + self.activitiesViewExpanded = YES; + [roomActivitiesView displayUnsentMessagesNotification:notification withResendLink:^{ - if (weakSelf) - { - typeof(self) self = weakSelf; - [self resendAllUnsentMessages]; - self->currentAlert = nil; - } + [self resendAllUnsentMessages]; - }]]; - - [currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"room_delete_unsent_messages", @"Vector", nil) - style:UIAlertActionStyleDefault - handler:^(UIAlertAction * action) { + } andCancelLink:^{ - if (weakSelf) - { - typeof(self) self = weakSelf; - [self cancelAllUnsentMessages]; - self->currentAlert = nil; - } + [self cancelAllUnsentMessages]; - }]]; - - [currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"cancel", @"Vector", nil) - style:UIAlertActionStyleCancel - handler:^(UIAlertAction * action) { + } andIconTapGesture:^{ - if (weakSelf) + if (self->currentAlert) { - typeof(self) self = weakSelf; - self->currentAlert = nil; + [self->currentAlert dismissViewControllerAnimated:NO completion:nil]; } - }]]; - - [currentAlert mxk_setAccessibilityIdentifier:@"RoomVCUnsentMessagesMenuAlert"]; - [currentAlert popoverPresentationController].sourceView = roomActivitiesView; - [currentAlert popoverPresentationController].sourceRect = roomActivitiesView.bounds; - [self presentViewController:currentAlert animated:YES completion:nil]; - - }]; - } + __weak __typeof(self) weakSelf = self; + self->currentAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; + + [self->currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"room_resend_unsent_messages", @"Vector", nil) + style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) { + + if (weakSelf) + { + typeof(self) self = weakSelf; + [self resendAllUnsentMessages]; + self->currentAlert = nil; + } + + }]]; + + [self->currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"room_delete_unsent_messages", @"Vector", nil) + style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) { + + if (weakSelf) + { + typeof(self) self = weakSelf; + [self cancelAllUnsentMessages]; + self->currentAlert = nil; + } + + }]]; + + [self->currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"cancel", @"Vector", nil) + style:UIAlertActionStyleCancel + handler:^(UIAlertAction * action) { + + if (weakSelf) + { + typeof(self) self = weakSelf; + self->currentAlert = nil; + } + + }]]; + + [self->currentAlert mxk_setAccessibilityIdentifier:@"RoomVCUnsentMessagesMenuAlert"]; + [self->currentAlert popoverPresentationController].sourceView = roomActivitiesView; + [self->currentAlert popoverPresentationController].sourceRect = roomActivitiesView.bounds; + [self presentViewController:self->currentAlert animated:YES completion:nil]; + + }]; + } + + completion(sentStatus != RoomSentStatusOk); + }]; + } + else + { + dispatch_async(dispatch_get_main_queue(), ^{ + completion(NO); + }); } - - return sentStatus != RoomSentStatusOk; } - (void)eventDidChangeSentState:(NSNotification *)notif @@ -4883,62 +4893,63 @@ - (void)eventDidChangeSentState:(NSNotification *)notif // List all unknown devices unknownDevices = [[MXUsersDevicesMap alloc] init]; - NSArray *outgoingMsgs = self.roomDataSource.room.outgoingMessages; - for (MXEvent *event in outgoingMsgs) - { - if (event.sentState == MXEventSentStateFailed - && [event.sentError.domain isEqualToString:MXEncryptingErrorDomain] - && event.sentError.code == MXEncryptingErrorUnknownDeviceCode) + [self.roomDataSource.room outgoingMessagesWithCompletion:^(NSArray * _Nullable outgoingMsgs) { + for (MXEvent *event in outgoingMsgs) { - MXUsersDevicesMap *eventUnknownDevices = event.sentError.userInfo[MXEncryptingErrorUnknownDeviceDevicesKey]; - - [unknownDevices addEntriesFromMap:eventUnknownDevices]; - } - } - - currentAlert = [UIAlertController alertControllerWithTitle:[NSBundle mxk_localizedStringForKey:@"unknown_devices_alert_title"] - message:[NSBundle mxk_localizedStringForKey:@"unknown_devices_alert"] - preferredStyle:UIAlertControllerStyleAlert]; - - [currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"unknown_devices_verify"] - style:UIAlertActionStyleDefault - handler:^(UIAlertAction * action) { - - if (weakSelf) - { - typeof(self) self = weakSelf; - self->currentAlert = nil; - - [self performSegueWithIdentifier:@"showUnknownDevices" sender:self]; + if (event.sentState == MXEventSentStateFailed + && [event.sentError.domain isEqualToString:MXEncryptingErrorDomain] + && event.sentError.code == MXEncryptingErrorUnknownDeviceCode) + { + MXUsersDevicesMap *eventUnknownDevices = event.sentError.userInfo[MXEncryptingErrorUnknownDeviceDevicesKey]; + + [self->unknownDevices addEntriesFromMap:eventUnknownDevices]; + } } - }]]; - - [currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"unknown_devices_send_anyway"] - style:UIAlertActionStyleDefault - handler:^(UIAlertAction * action) { + self->currentAlert = [UIAlertController alertControllerWithTitle:[NSBundle mxk_localizedStringForKey:@"unknown_devices_alert_title"] + message:[NSBundle mxk_localizedStringForKey:@"unknown_devices_alert"] + preferredStyle:UIAlertControllerStyleAlert]; - if (weakSelf) - { - typeof(self) self = weakSelf; - self->currentAlert = nil; + [self->currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"unknown_devices_verify"] + style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) { - // Acknowledge the existence of all devices - [self startActivityIndicator]; - [self.mainSession.crypto setDevicesKnown:self->unknownDevices complete:^{ + if (weakSelf) + { + typeof(self) self = weakSelf; + self->currentAlert = nil; - self->unknownDevices = nil; - [self stopActivityIndicator]; + [self performSegueWithIdentifier:@"showUnknownDevices" sender:self]; + } + + }]]; + + [self->currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"unknown_devices_send_anyway"] + style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) { + + if (weakSelf) + { + typeof(self) self = weakSelf; + self->currentAlert = nil; - // And resend pending messages - [self resendAllUnsentMessages]; - }]; - } + // Acknowledge the existence of all devices + [self startActivityIndicator]; + [self.mainSession.crypto setDevicesKnown:self->unknownDevices complete:^{ + + self->unknownDevices = nil; + [self stopActivityIndicator]; + + // And resend pending messages + [self resendAllUnsentMessages]; + }]; + } + + }]]; - }]]; - - [currentAlert mxk_setAccessibilityIdentifier:@"RoomVCUnknownDevicesAlert"]; - [self presentViewController:currentAlert animated:YES completion:nil]; + [self->currentAlert mxk_setAccessibilityIdentifier:@"RoomVCUnknownDevicesAlert"]; + [self presentViewController:self->currentAlert animated:YES completion:nil]; + }]; } } @@ -4958,19 +4969,20 @@ - (void)eventDidChangeIdentifier:(NSNotification *)notif - (void)resendAllUnsentMessages { // List unsent event ids - NSArray *outgoingMsgs = self.roomDataSource.room.outgoingMessages; - NSMutableArray *failedEventIds = [NSMutableArray arrayWithCapacity:outgoingMsgs.count]; - - for (MXEvent *event in outgoingMsgs) - { - if (event.sentState == MXEventSentStateFailed) + [self.roomDataSource.room outgoingMessagesWithCompletion:^(NSArray * _Nullable outgoingMsgs) { + NSMutableArray *failedEventIds = [NSMutableArray arrayWithCapacity:outgoingMsgs.count]; + + for (MXEvent *event in outgoingMsgs) { - [failedEventIds addObject:event.eventId]; + if (event.sentState == MXEventSentStateFailed) + { + [failedEventIds addObject:event.eventId]; + } } - } - - // Launch iterative operation - [self resendFailedEvent:0 inArray:failedEventIds]; + + // Launch iterative operation + [self resendFailedEvent:0 inArray:failedEventIds]; + }]; } - (void)resendFailedEvent:(NSUInteger)index inArray:(NSArray*)failedEventIds @@ -5011,20 +5023,17 @@ - (void)cancelAllUnsentMessages [currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"delete"] style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { MXStrongifyAndReturnIfNil(self); // Remove unsent event ids - for (NSUInteger index = 0; index < self.roomDataSource.room.outgoingMessages.count;) - { - MXEvent *event = self.roomDataSource.room.outgoingMessages[index]; - if (event.sentState == MXEventSentStateFailed) + [self.roomDataSource.room outgoingMessagesWithCompletion:^(NSArray * _Nullable outgoingMessages) { + for (MXEvent *event in outgoingMessages) { - [self.roomDataSource removeEventWithEventId:event.eventId]; - } - else - { - index ++; + if (event.sentState == MXEventSentStateFailed) + { + [self.roomDataSource removeEventWithEventId:event.eventId]; + } } - } - - [self refreshActivitiesViewDisplay]; + + [self refreshActivitiesViewDisplay]; + }]; }]]; [self presentViewController:currentAlert animated:YES completion:nil]; @@ -5143,19 +5152,19 @@ - (void)animateReadMarkerView [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseIn animations:^{ - readMarkerTableViewCell.readMarkerViewLeadingConstraint.constant = readMarkerTableViewCell.readMarkerViewTrailingConstraint.constant = readMarkerTableViewCell.bubbleOverlayContainer.frame.size.width / 2; - readMarkerTableViewCell.readMarkerView.alpha = 0; + self->readMarkerTableViewCell.readMarkerViewLeadingConstraint.constant = self->readMarkerTableViewCell.readMarkerViewTrailingConstraint.constant = self->readMarkerTableViewCell.bubbleOverlayContainer.frame.size.width / 2; + self->readMarkerTableViewCell.readMarkerView.alpha = 0; // Force to render the view - [readMarkerTableViewCell.bubbleOverlayContainer layoutIfNeeded]; + [self->readMarkerTableViewCell.bubbleOverlayContainer layoutIfNeeded]; } completion:^(BOOL finished){ - readMarkerTableViewCell.readMarkerView.hidden = YES; - readMarkerTableViewCell.readMarkerView.alpha = 1; + self->readMarkerTableViewCell.readMarkerView.hidden = YES; + self->readMarkerTableViewCell.readMarkerView.alpha = 1; - readMarkerTableViewCell = nil; + self->readMarkerTableViewCell = nil; }]; }); From 21c99ba1403b5118462a6c68ca820c5dc01238b3 Mon Sep 17 00:00:00 2001 From: ismailgulek Date: Fri, 20 Aug 2021 17:29:36 +0300 Subject: [PATCH 2/2] Add changelog --- changelog.d/4382.change | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/4382.change diff --git a/changelog.d/4382.change b/changelog.d/4382.change new file mode 100644 index 0000000000..299ce6df2f --- /dev/null +++ b/changelog.d/4382.change @@ -0,0 +1 @@ +Recents & Room: Adapt to new lazy load room messages methods.