Skip to content

Commit

Permalink
Fixes #3243 - Bring back default controls for QuickLook based media v…
Browse files Browse the repository at this point in the history
…iewers.
  • Loading branch information
stefanceriu committed Sep 10, 2024
1 parent 4ec6098 commit 862eb95
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ import SwiftUI
extension View {
/// Preview a media file using a QuickLook Preview Controller. The preview is interactive with
/// the dismiss gesture working as expected if it was presented from UIKit.
func interactiveQuickLook(item: Binding<MediaPreviewItem?>, shouldHideControls: Bool = false) -> some View {
modifier(InteractiveQuickLookModifier(item: item, shouldHideControls: shouldHideControls))
func interactiveQuickLook(item: Binding<MediaPreviewItem?>) -> some View {
modifier(InteractiveQuickLookModifier(item: item))
}
}

private struct InteractiveQuickLookModifier: ViewModifier {
@Binding var item: MediaPreviewItem?
let shouldHideControls: Bool

@State private var dismissalPublisher = PassthroughSubject<Void, Never>()

func body(content: Content) -> some View {
content.background {
if let item {
MediaPreviewViewController(previewItem: item,
shouldHideControls: shouldHideControls,
dismissalPublisher: dismissalPublisher) { self.item = nil }
} else {
// Work around QLPreviewController dismissal issues, see below.
Expand All @@ -39,13 +36,11 @@ private struct InteractiveQuickLookModifier: ViewModifier {

private struct MediaPreviewViewController: UIViewControllerRepresentable {
let previewItem: MediaPreviewItem
let shouldHideControls: Bool
let dismissalPublisher: PassthroughSubject<Void, Never>
let onDismiss: () -> Void

func makeUIViewController(context: Context) -> PreviewHostingController {
PreviewHostingController(previewItem: previewItem,
shouldHideControls: shouldHideControls,
dismissalPublisher: dismissalPublisher,
onDismiss: onDismiss)
}
Expand All @@ -58,19 +53,16 @@ private struct MediaPreviewViewController: UIViewControllerRepresentable {
/// animations and interactions which don't work if you represent it directly to SwiftUI 🤷‍♂️
class PreviewHostingController: UIViewController, QLPreviewControllerDataSource, QLPreviewControllerDelegate {
let previewItem: MediaPreviewItem
let shouldHideControls: Bool
let onDismiss: () -> Void

private var dismissalObserver: AnyCancellable?

var previewController: QLPreviewController?

init(previewItem: MediaPreviewItem,
shouldHideControls: Bool,
dismissalPublisher: PassthroughSubject<Void, Never>,
onDismiss: @escaping () -> Void) {
self.previewItem = previewItem
self.shouldHideControls = shouldHideControls
self.onDismiss = onDismiss

super.init(nibName: nil, bundle: nil)
Expand Down Expand Up @@ -100,7 +92,7 @@ private struct MediaPreviewViewController: UIViewControllerRepresentable {

guard self.previewController == nil else { return }

let previewController = (shouldHideControls ? NoControlsPreviewController() : QLPreviewController())
let previewController = QLPreviewController()
previewController.dataSource = self
previewController.delegate = self
present(previewController, animated: true)
Expand Down Expand Up @@ -139,28 +131,6 @@ class MediaPreviewItem: NSObject, QLPreviewItem {
}
}

private class NoControlsPreviewController: QLPreviewController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

guard let navigationController = children.first as? UINavigationController else {
return
}

// Remove top file details bar
navigationController.navigationBar.isHidden = true

// Remove the toolbars and their buttons
navigationController.view.subviews.compactMap { $0 as? UIToolbar }.forEach { toolbar in
toolbar.subviews.forEach { item in
item.isHidden = true
}

toolbar.isHidden = true
}
}
}

// MARK: - Previews

struct PreviewView_Previews: PreviewProvider {
Expand All @@ -170,7 +140,6 @@ struct PreviewView_Previews: PreviewProvider {

static var previews: some View {
MediaPreviewViewController(previewItem: previewItem,
shouldHideControls: false,
dismissalPublisher: .init()) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct RoomDetailsScreen: View {
.navigationTitle(L10n.screenRoomDetailsTitle)
.navigationBarTitleDisplayMode(.inline)
.track(screen: .RoomDetails)
.interactiveQuickLook(item: $context.mediaPreviewItem, shouldHideControls: true)
.interactiveQuickLook(item: $context.mediaPreviewItem)
}

// MARK: - Private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct RoomMemberDetailsScreen: View {
.alert(item: $context.ignoreUserAlert, actions: blockUserAlertActions, message: blockUserAlertMessage)
.alert(item: $context.alertInfo)
.track(screen: .User)
.interactiveQuickLook(item: $context.mediaPreviewItem, shouldHideControls: true)
.interactiveQuickLook(item: $context.mediaPreviewItem)
}

// MARK: - Private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct UserProfileScreen: View {
.toolbar { toolbar }
.alert(item: $context.alertInfo)
.track(screen: .User)
.interactiveQuickLook(item: $context.mediaPreviewItem, shouldHideControls: true)
.interactiveQuickLook(item: $context.mediaPreviewItem)
}

// MARK: - Private
Expand Down

0 comments on commit 862eb95

Please sign in to comment.