Observe URL preview update notification in RoomViewController.

Update bubbleTableView's content offset when a preview above the bottom most visible cell changes the height of the table's content.
This commit is contained in:
Doug
2021-09-07 11:20:59 +01:00
parent 042fa4197d
commit babbdff05a
2 changed files with 47 additions and 23 deletions
+47
View File
@@ -206,6 +206,9 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
// Observe kThemeServiceDidChangeThemeNotification to handle user interface theme change.
id kThemeServiceDidChangeThemeNotificationObserver;
// Observe URL preview updates to refresh cells.
id URLPreviewDidUpdateNotificationObserver;
// Listener for `m.room.tombstone` event type
id tombstoneEventNotificationsListener;
@@ -438,6 +441,45 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
}];
[self userInterfaceThemeDidChange];
// Observe URL preview updates.
URLPreviewDidUpdateNotificationObserver = [NSNotificationCenter.defaultCenter addObserverForName:URLPreviewDidUpdateNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull notification) {
// Ensure this is the correct room
if (![(NSString*)notification.userInfo[@"roomId"] isEqualToString:self.roomDataSource.roomId])
{
return;
}
// Get the indexPath for the updated cell.
NSString *updatedEventId = notification.userInfo[@"eventId"];
NSInteger updatedEventIndex = [self.roomDataSource indexOfCellDataWithEventId:updatedEventId];
NSIndexPath *updatedIndexPath = [NSIndexPath indexPathForRow:updatedEventIndex inSection:0];
// Store the content size and offset before reloading the cell
CGFloat originalContentSize = self.bubblesTableView.contentSize.height;
CGPoint contentOffset = self.bubblesTableView.contentOffset;
// Only update the content offset if the cell is visible or above the current visible cells.
BOOL shouldUpdateContentOffset = NO;
NSIndexPath *lastVisibleIndexPath = [self.bubblesTableView indexPathsForVisibleRows].lastObject;
if (lastVisibleIndexPath && updatedIndexPath.row < lastVisibleIndexPath.row)
{
shouldUpdateContentOffset = YES;
}
// Note: Despite passing in the index path, this reloads the whole table.
[self dataSource:self.roomDataSource didCellChange:updatedIndexPath];
// Update the content offset to include any changes to the scroll view's height.
if (shouldUpdateContentOffset)
{
CGFloat delta = self.bubblesTableView.contentSize.height - originalContentSize;
contentOffset.y += delta;
self.bubblesTableView.contentOffset = contentOffset;
}
}];
[self setupActions];
}
@@ -1356,6 +1398,11 @@ const NSTimeInterval kResizeComposerAnimationDuration = .05;
[[NSNotificationCenter defaultCenter] removeObserver:mxEventDidDecryptNotificationObserver];
mxEventDidDecryptNotificationObserver = nil;
}
if (URLPreviewDidUpdateNotificationObserver)
{
[NSNotificationCenter.defaultCenter removeObserver:URLPreviewDidUpdateNotificationObserver];
URLPreviewDidUpdateNotificationObserver = nil;
}
[self removeCallNotificationsListeners];
[self removeWidgetNotificationsListeners];