// GalleryViewController.swift // Tusker // // Created by Shadowfacts on 6/13/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class GalleryViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate { var dismissInteractionController: LargeImageInteractionController? let attachments: [Attachment] let sourcesInfo: [(CGRect, CGFloat)] let startIndex: Int let pages: [AttachmentViewController] var currentIndex: Int { guard let vc = viewControllers?.first as? AttachmentViewController, let index = pages.firstIndex(of: vc) else { fatalError() } return index } override var prefersStatusBarHidden: Bool { return true } override var childForHomeIndicatorAutoHidden: UIViewController? { return viewControllers?.first } init(attachments: [Attachment], sourcesInfo: [(CGRect, CGFloat)], startIndex: Int) { self.attachments = attachments self.sourcesInfo = sourcesInfo self.startIndex = startIndex self.pages = attachments.map(AttachmentViewController.init) super.init(transitionStyle: .scroll, navigationOrientation: .horizontal) setViewControllers([pages[startIndex]], direction: .forward, animated: false) modalPresentationStyle = .fullScreen } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() self.dataSource = self self.delegate = self dismissInteractionController = LargeImageInteractionController(viewController: self) } // MARK: - Page View Controller Data Source func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { guard let attachment = viewController as? AttachmentViewController, let index = pages.firstIndex(of: attachment), index > 0 else { return nil } return pages[index - 1] } func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { guard let attachment = viewController as? AttachmentViewController, let index = pages.firstIndex(of: attachment), index < pages.count - 1 else { return nil } return pages[index + 1] } // MARK: - Page View Controller Delegate func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) { let pending = pendingViewControllers.first as! AttachmentViewController let current = viewControllers!.first as! AttachmentViewController pending.controlsVisible = current.controlsVisible } }