diff --git a/ElementX/Sources/Screens/FilePreviewScreen/TimelineMediaPreviewDataSource.swift b/ElementX/Sources/Screens/FilePreviewScreen/TimelineMediaPreviewDataSource.swift index a96c04d48..4fd3160df 100644 --- a/ElementX/Sources/Screens/FilePreviewScreen/TimelineMediaPreviewDataSource.swift +++ b/ElementX/Sources/Screens/FilePreviewScreen/TimelineMediaPreviewDataSource.swift @@ -33,7 +33,14 @@ class TimelineMediaPreviewDataSource: NSObject, QLPreviewControllerDataSource { private var backwardPadding: Int private var forwardPadding: Int - var paginationState: TimelinePaginationState + /// Reaching the end of the timeline changes which placeholder an index shows, so a change + /// needs to be published too, not only the arrival of new items. + var paginationState: TimelinePaginationState { + didSet { + guard paginationState != oldValue else { return } + previewItemsPaginationPublisher.send() + } + } /// The gallery attachments to include, so that a gallery only contributes the media being browsed. private let allowedGalleryItemTypes: [TimelineAllowedGalleryItemType]? diff --git a/ElementX/Sources/Screens/FilePreviewScreen/View/TimelineMediaPreviewController.swift b/ElementX/Sources/Screens/FilePreviewScreen/View/TimelineMediaPreviewController.swift index 364cf61b9..e9240438b 100644 --- a/ElementX/Sources/Screens/FilePreviewScreen/View/TimelineMediaPreviewController.swift +++ b/ElementX/Sources/Screens/FilePreviewScreen/View/TimelineMediaPreviewController.swift @@ -243,11 +243,13 @@ class TimelineMediaPreviewController: QLPreviewController { } private func handleUpdatedItems() { - if currentPreviewItem is TimelineMediaPreviewItem.Loading { - let dataSource = context.viewState.dataSource - if dataSource.previewController(self, previewItemAt: currentPreviewItemIndex) is TimelineMediaPreviewItem.Media { - refreshCurrentPreviewItem() // This will trigger loadCurrentItem automatically. - } + guard let displayedItem = currentPreviewItem as? TimelineMediaPreviewItem.Loading else { return } + + // The index may now hold a media, or a different placeholder having reached the end of + // the timeline, in which case what's on display is stale. + let dataSource = context.viewState.dataSource + if dataSource.previewController(self, previewItemAt: currentPreviewItemIndex) as AnyObject !== displayedItem { + refreshCurrentPreviewItem() // This will trigger loadCurrentItem automatically. } }