Add support for tapping an image to hide all the chrome

This commit is contained in:
Jed Fox 2022-11-14 21:52:16 -05:00
parent 5277ec191b
commit 0687ef4f8e
No known key found for this signature in database
GPG Key ID: 0B61D18EA54B47E1
5 changed files with 65 additions and 10 deletions

View File

@ -339,7 +339,7 @@ extension SceneCoordinator {
case .custom(let transitioningDelegate):
viewController.modalPresentationStyle = .custom
viewController.transitioningDelegate = transitioningDelegate
// viewController.modalPresentationCapturesStatusBarAppearance = true
viewController.modalPresentationCapturesStatusBarAppearance = true
(splitViewController ?? presentingViewController)?.present(viewController, animated: true, completion: nil)
case .customPush(let animated):

View File

@ -111,13 +111,19 @@ extension MediaPreviewImageViewController {
}
extension MediaPreviewImageViewController: MediaPreviewPage {
func setShowingChrome(_ showingChrome: Bool) {
if #available(iOS 16.0, *) {
UIView.animate(withDuration: 0.3) {
self.previewImageView.liveTextInteraction.setSupplementaryInterfaceHidden(!showingChrome, animated: true)
}
}
}
}
// MARK: - ImageAnalysisInteractionDelegate
@available(iOS 16.0, *)
extension MediaPreviewImageViewController: ImageAnalysisInteractionDelegate {
func contentView(for interaction: ImageAnalysisInteraction) -> UIView? {
view
}
func presentingViewController(for interaction: ImageAnalysisInteraction) -> UIViewController? {
self
}
@ -128,8 +134,14 @@ extension MediaPreviewImageViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if #available(iOS 16.0, *) {
let location = touch.location(in: previewImageView.imageView)
let hasItem = previewImageView.liveTextInteraction.hasInteractiveItem(at: location)
return !hasItem
// for tap gestures, only items that can be tapped are relevant
if gestureRecognizer is UITapGestureRecognizer {
return !previewImageView.liveTextInteraction.hasSupplementaryInterface(at: location)
&& !previewImageView.liveTextInteraction.hasDataDetector(at: location)
} else {
// for long press, block out everything
return !previewImageView.liveTextInteraction.hasInteractiveItem(at: location)
}
} else {
return true
}

View File

@ -137,6 +137,17 @@ extension MediaPreviewViewController {
}
}
.store(in: &disposeBag)
viewModel.$showingChrome
.receive(on: DispatchQueue.main)
.removeDuplicates()
.sink { [weak self] showingChrome in
UIView.animate(withDuration: 0.3) {
self?.setNeedsStatusBarAppearanceUpdate()
self?.closeButtonBackground.alpha = showingChrome ? 1 : 0
}
}
.store(in: &disposeBag)
// viewModel.$isPoping
// .receive(on: DispatchQueue.main)
@ -153,6 +164,14 @@ extension MediaPreviewViewController {
}
extension MediaPreviewViewController {
override var prefersStatusBarHidden: Bool {
!viewModel.showingChrome
}
}
extension MediaPreviewViewController {
@objc private func closeButtonPressed(_ sender: UIButton) {
@ -239,8 +258,11 @@ extension MediaPreviewViewController: MediaPreviewImageViewControllerDelegate {
let location = tapGestureRecognizer.location(in: viewController.previewImageView.imageView)
let isContainsTap = viewController.previewImageView.imageView.frame.contains(location)
guard !isContainsTap else { return }
dismiss(animated: true, completion: nil)
if isContainsTap {
self.viewModel.showingChrome.toggle()
} else {
dismiss(animated: true, completion: nil)
}
}
func mediaPreviewImageViewController(_ viewController: MediaPreviewImageViewController, longPressGestureRecognizerDidTrigger longPressGestureRecognizer: UILongPressGestureRecognizer) {

View File

@ -12,6 +12,10 @@ import CoreDataStack
import Pageboy
import MastodonCore
protocol MediaPreviewPage: UIViewController {
func setShowingChrome(_ showingChrome: Bool)
}
final class MediaPreviewViewModel: NSObject {
weak var mediaPreviewImageViewControllerDelegate: MediaPreviewImageViewControllerDelegate?
@ -22,9 +26,12 @@ final class MediaPreviewViewModel: NSObject {
let transitionItem: MediaPreviewTransitionItem
@Published var currentPage: Int
@Published var showingChrome = true
// output
let viewControllers: [UIViewController]
private var disposeBag: Set<AnyCancellable> = []
init(
context: AppContext,
@ -34,7 +41,7 @@ final class MediaPreviewViewModel: NSObject {
self.context = context
self.item = item
var currentPage = 0
var viewControllers: [UIViewController] = []
var viewControllers: [MediaPreviewPage] = []
switch item {
case .attachment(let previewContext):
currentPage = previewContext.initialIndex
@ -106,6 +113,14 @@ final class MediaPreviewViewModel: NSObject {
self.currentPage = currentPage
self.transitionItem = transitionItem
super.init()
for viewController in viewControllers {
self.$showingChrome
.sink { [weak viewController] showingChrome in
viewController?.setShowingChrome(showingChrome)
}
.store(in: &disposeBag)
}
}
}

View File

@ -111,6 +111,12 @@ extension MediaPreviewVideoViewController {
// }
//}
extension MediaPreviewVideoViewController: MediaPreviewPage {
func setShowingChrome(_ showingChrome: Bool) {
// TODO: does this do anything?
}
}
// MARK: - AVPlayerViewControllerDelegate
extension MediaPreviewVideoViewController: AVPlayerViewControllerDelegate {