metatext/View Controllers/ImagePageViewController.swift

98 lines
3.5 KiB
Swift
Raw Normal View History

2020-10-21 08:07:13 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import UIKit
import ViewModels
2020-11-09 06:22:20 +00:00
final class ImagePageViewController: UIPageViewController {
2020-10-21 08:07:13 +00:00
let imageViewControllers: [ImageViewController]
init(initiallyVisible: AttachmentViewModel, statusViewModel: StatusViewModel) {
2020-10-22 22:16:06 +00:00
imageViewControllers = statusViewModel.attachmentViewModels.map { ImageViewController(viewModel: $0) }
2020-10-21 08:07:13 +00:00
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)
}
2020-10-22 22:16:06 +00:00
init(imageURL: URL) {
imageViewControllers = [ImageViewController(imageURL: imageURL)]
super.init(
transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: [.interPageSpacing: CGFloat.defaultSpacing])
setViewControllers(imageViewControllers, direction: .forward, animated: false)
}
2020-10-21 08:07:13 +00:00
@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) })
2021-01-18 06:03:32 +00:00
navigationItem.rightBarButtonItem = .init(
systemItem: .action,
primaryAction: UIAction { [weak self] _ in
(self?.viewControllers?.first as? ImageViewController)?.presentActivityViewController()
})
2020-10-21 08:07:13 +00:00
navigationController?.barHideOnTapGestureRecognizer.addTarget(
self,
action: #selector(toggleDescriptionVisibility))
}
2020-10-29 02:01:24 +00:00
override var prefersStatusBarHidden: Bool { navigationController?.isNavigationBarHidden ?? false }
override var prefersHomeIndicatorAutoHidden: Bool { navigationController?.isNavigationBarHidden ?? false }
2020-10-21 08:07:13 +00: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]
}
}