Skip to content

Commit

Permalink
Workaround for the preview controller replacing the info button when …
Browse files Browse the repository at this point in the history
…swiping.
  • Loading branch information
pixlwave committed Jan 30, 2025
1 parent 1451d5f commit 9dcfc85
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private struct TimelineMediaPreviewModifier: ViewModifier {
if let viewModel {
MediaPreviewViewController(viewModel: viewModel,
dismissalPublisher: dismissalPublisher) { self.viewModel = nil }
.id(viewModel.instanceID) // Fixes a bug where opening a second preview too quickly can break presentation.
} else {
// Work around QLPreviewController dismissal issues, see below.
let _ = dismissalPublisher.send(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Foundation
typealias TimelineMediaPreviewViewModelType = StateStoreViewModel<TimelineMediaPreviewViewState, TimelineMediaPreviewViewAction>

class TimelineMediaPreviewViewModel: TimelineMediaPreviewViewModelType {
let instanceID = UUID()

private let timelineViewModel: TimelineViewModelProtocol
private let mediaProvider: MediaProviderProtocol
private let photoLibraryManager: PhotoLibraryManagerProtocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class TimelineMediaPreviewController: QLPreviewController {
private let downloadIndicatorHostingController: UIHostingController<DownloadIndicatorView>
private var detailsHostingController: UIHostingController<TimelineMediaPreviewDetailsView>?

private var barButtonTimer: Timer?

private var cancellables: Set<AnyCancellable> = []

private var navigationBar: UINavigationBar? { view.subviews.first?.subviews.first { $0 is UINavigationBar } as? UINavigationBar }
Expand Down Expand Up @@ -97,6 +99,8 @@ class TimelineMediaPreviewController: QLPreviewController {
fatalError("init(coder:) has not been implemented")
}

// MARK: Layout

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

Expand All @@ -117,11 +121,27 @@ class TimelineMediaPreviewController: QLPreviewController {
navigationBar?.topItem?.titleView = headerHostingController.view

updateBarButtons()

// Ridiculous hack to undo the controller's attempt to replace our info button with the list button.
if barButtonTimer == nil {
barButtonTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [weak self] _ in
self?.updateBarButtons()
}
}
}

override func viewWillDisappear(_ animated: Bool) {
barButtonTimer?.invalidate()
barButtonTimer = nil
}

private func updateBarButtons() {
let button = UIBarButtonItem(customView: detailsButtonHostingController.view)
navigationBar?.topItem?.leftBarButtonItem = button
guard let topItem = navigationBar?.topItem else { return }

if topItem.leftBarButtonItem?.customView == nil {
let button = UIBarButtonItem(customView: detailsButtonHostingController.view)
navigationBar?.topItem?.leftBarButtonItem = button
}
}

// MARK: Item loading
Expand Down

0 comments on commit 9dcfc85

Please sign in to comment.