metatext-app-ios-iphone-ipad/View Controllers/ImagePageViewController.swift

81 lines
2.9 KiB
Swift
Raw Normal View History

2020-10-21 10:07:13 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import UIKit
import ViewModels
class ImagePageViewController: UIPageViewController {
let imageViewControllers: [ImageViewController]
init(initiallyVisible: AttachmentViewModel, statusViewModel: StatusViewModel) {
imageViewControllers = statusViewModel.attachmentViewModels.map(ImageViewController.init(viewModel:))
super.init(
transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: [.interPageSpacing: CGFloat.defaultSpacing])
let index = statusViewModel.attachmentViewModels.firstIndex {
$0.attachment.id == initiallyVisible.attachment.id
}
setViewControllers([imageViewControllers[index ?? 0]], direction: .forward, animated: false)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
view.backgroundColor = .secondarySystemBackground
view.subviews.compactMap { $0 as? UIScrollView }.first?.bounces = imageViewControllers.count > 1
navigationItem.leftBarButtonItem = .init(
systemItem: .close,
primaryAction: UIAction { [weak self] _ in self?.presentingViewController?.dismiss(animated: true) })
navigationController?.barHideOnTapGestureRecognizer.addTarget(
self,
action: #selector(toggleDescriptionVisibility))
}
override var prefersStatusBarHidden: Bool { true }
override var prefersHomeIndicatorAutoHidden: Bool { navigationController?.isNavigationBarHidden ?? false }
2020-10-21 10:07:13 +02:00
}
extension ImagePageViewController {
@objc func toggleDescriptionVisibility() {
for controller in imageViewControllers {
controller.toggleDescriptionVisibility()
}
}
}
extension ImagePageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard
let imageViewController = viewController as? ImageViewController,
let index = imageViewControllers.firstIndex(of: imageViewController),
index + 1 < imageViewControllers.count
else { return nil }
return imageViewControllers[index + 1]
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard
let imageViewController = viewController as? ImageViewController,
let index = imageViewControllers.firstIndex(of: imageViewController),
index > 0
else { return nil }
return imageViewControllers[index - 1]
}
}