<commit_before>//
//  PhotosViewController.swift
//  Yep
//
//  Created by NIX on 16/6/17.
//  Copyright © 2016年 Catch Inc. All rights reserved.
//

import UIKit

public class PhotosViewController: UIViewController {

    private weak var delegate: PhotosViewControllerDelegate?

    private let dataSource: PhotosViewControllerDataSource

    private lazy var transitionController = PhotoTransitonController()

    private lazy var pageViewController: UIPageViewController = {

        let vc = UIPageViewController(
            transitionStyle: .Scroll,
            navigationOrientation: .Horizontal,
            options: [UIPageViewControllerOptionInterPageSpacingKey: 16])

        vc.dataSource = self
        vc.delegate = self

        vc.view.backgroundColor = UIColor.clearColor()

        vc.view.addGestureRecognizer(self.panGestureRecognizer)
        vc.view.addGestureRecognizer(self.singleTapGestureRecognizer)

        return vc
    }()

    private var currentPhotoViewController: PhotoViewController? {
        return pageViewController.viewControllers?.first as? PhotoViewController
    }
    private var currentlyDisplayedPhoto: Photo? {
        return currentPhotoViewController?.photo
    }
    private var referenceViewForCurrentPhoto: UIView? {
        guard let photo = currentlyDisplayedPhoto else {
            return nil
        }
        
        return delegate?.photosViewController(self, referenceViewForPhoto: photo)
    }

    private lazy var panGestureRecognizer: UIPanGestureRecognizer = {

        let pan = UIPanGestureRecognizer()
        pan.addTarget(self, action: #selector(didPan(_:)))
        return pan
    }()

    private lazy var singleTapGestureRecognizer: UITapGestureRecognizer = {

        let tap = UITapGestureRecognizer()
        tap.addTarget(self, action: #selector(didSingleTap(_:)))
        return tap
    }()

    deinit {
        pageViewController.dataSource = nil
        pageViewController.delegate = nil
    }

    // MARK: Init

    public init(photos: [Photo], initialPhoto: Photo, delegate: PhotosViewControllerDelegate? = nil) {

        self.dataSource = PhotosDataSource(photos: photos)
        self.delegate = delegate

        super.init(nibName: nil, bundle: nil)

        self.modalPresentationStyle = .Custom
        self.transitioningDelegate = transitionController
        self.modalPresentationCapturesStatusBarAppearance = true

        //overlayView...        

        let initialPhotoViewController: PhotoViewController
        if dataSource.containsPhoto(initialPhoto) {
            initialPhotoViewController = newPhotoViewControllerForPhoto(initialPhoto)
        } else {
            guard let firstPhoto = dataSource.photoAtIndex(0) else {
                fatalError("Empty dataSource")
            }
            initialPhotoViewController = newPhotoViewControllerForPhoto(firstPhoto)
        }
        setCurrentlyDisplayedViewController(initialPhotoViewController, animated: false)
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func newPhotoViewControllerForPhoto(photo: Photo) -> PhotoViewController {

        let photoViewController = PhotoViewController(photo: photo)

        //photoViewController.delegate = self

        singleTapGestureRecognizer.requireGestureRecognizerToFail(photoViewController.doubleTapGestureRecognizer)

        // ...

        return photoViewController
    }

    private func setCurrentlyDisplayedViewController(vc: PhotoViewController, animated: Bool) {

        pageViewController.setViewControllers([vc], direction: .Forward, animated: animated, completion: nil)
    }

    // MARK: Life Circle

    override public func viewDidLoad() {
        super.viewDidLoad()

        view.tintColor = UIColor.whiteColor()
        view.backgroundColor = UIColor.blackColor()

        addChildViewController(pageViewController)
        view.addSubview(pageViewController.view)
        pageViewController.didMoveToParentViewController(self)

        // TODO: add overlay

        transitionController.setStartingView(referenceViewForCurrentPhoto)

        if currentlyDisplayedPhoto?.imageType.image != nil {
            transitionController.setEndingView(currentPhotoViewController?.scalingImageView.imageView)
        }
    }

    // MARK: Selectors

    @objc private func didPan(sender: UIPanGestureRecognizer) {

        transitionController.forcesNonInteractiveDismissal = false

        // TODO: didPan
    }

    @objc private func didSingleTap(sender: UITapGestureRecognizer) {

        // TODO: didSingleTap
    }
}

extension PhotosViewController: UIPageViewControllerDataSource {
<commit_msg>try dismissViewControllerAnimated<commit_after>//
//  PhotosViewController.swift
//  Yep
//
//  Created by NIX on 16/6/17.
//  Copyright © 2016年 Catch Inc. All rights reserved.
//

import UIKit

public class PhotosViewController: UIViewController {

    private weak var delegate: PhotosViewControllerDelegate?

    private let dataSource: PhotosViewControllerDataSource

    private lazy var transitionController = PhotoTransitonController()

    private lazy var pageViewController: UIPageViewController = {

        let vc = UIPageViewController(
            transitionStyle: .Scroll,
            navigationOrientation: .Horizontal,
            options: [UIPageViewControllerOptionInterPageSpacingKey: 16])

        vc.dataSource = self
        vc.delegate = self

        vc.view.backgroundColor = UIColor.clearColor()

        vc.view.addGestureRecognizer(self.panGestureRecognizer)
        vc.view.addGestureRecognizer(self.singleTapGestureRecognizer)

        return vc
    }()

    private var currentPhotoViewController: PhotoViewController? {
        return pageViewController.viewControllers?.first as? PhotoViewController
    }
    private var currentlyDisplayedPhoto: Photo? {
        return currentPhotoViewController?.photo
    }
    private var referenceViewForCurrentPhoto: UIView? {
        guard let photo = currentlyDisplayedPhoto else {
            return nil
        }
        
        return delegate?.photosViewController(self, referenceViewForPhoto: photo)
    }

    private lazy var panGestureRecognizer: UIPanGestureRecognizer = {

        let pan = UIPanGestureRecognizer()
        pan.addTarget(self, action: #selector(PhotosViewController.didPan(_:)))
        return pan
    }()

    private lazy var singleTapGestureRecognizer: UITapGestureRecognizer = {

        let tap = UITapGestureRecognizer()
        tap.addTarget(self, action: #selector(PhotosViewController.didSingleTap(_:)))
        return tap
    }()

    deinit {
        pageViewController.dataSource = nil
        pageViewController.delegate = nil
    }

    // MARK: Init

    public init(photos: [Photo], initialPhoto: Photo, delegate: PhotosViewControllerDelegate? = nil) {

        self.dataSource = PhotosDataSource(photos: photos)
        self.delegate = delegate

        super.init(nibName: nil, bundle: nil)

        self.modalPresentationStyle = .Custom
        self.transitioningDelegate = transitionController
        self.modalPresentationCapturesStatusBarAppearance = true

        //overlayView...        

        let initialPhotoViewController: PhotoViewController
        if dataSource.containsPhoto(initialPhoto) {
            initialPhotoViewController = newPhotoViewControllerForPhoto(initialPhoto)
        } else {
            guard let firstPhoto = dataSource.photoAtIndex(0) else {
                fatalError("Empty dataSource")
            }
            initialPhotoViewController = newPhotoViewControllerForPhoto(firstPhoto)
        }
        setCurrentlyDisplayedViewController(initialPhotoViewController, animated: false)
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func newPhotoViewControllerForPhoto(photo: Photo) -> PhotoViewController {

        let photoViewController = PhotoViewController(photo: photo)

        //photoViewController.delegate = self

        singleTapGestureRecognizer.requireGestureRecognizerToFail(photoViewController.doubleTapGestureRecognizer)

        // ...

        return photoViewController
    }

    private func setCurrentlyDisplayedViewController(vc: PhotoViewController, animated: Bool) {

        pageViewController.setViewControllers([vc], direction: .Forward, animated: animated, completion: nil)
    }

    // MARK: Life Circle

    override public func viewDidLoad() {
        super.viewDidLoad()

        view.tintColor = UIColor.whiteColor()
        view.backgroundColor = UIColor.blackColor()

        addChildViewController(pageViewController)
        view.addSubview(pageViewController.view)
        pageViewController.didMoveToParentViewController(self)

        // TODO: add overlay

        transitionController.setStartingView(referenceViewForCurrentPhoto)

        if currentlyDisplayedPhoto?.imageType.image != nil {
            transitionController.setEndingView(currentPhotoViewController?.scalingImageView.imageView)
        }
    }

    // MARK: Selectors

    @objc private func didPan(sender: UIPanGestureRecognizer) {

        transitionController.forcesNonInteractiveDismissal = false

        // TODO: didPan
    }

    @objc private func didSingleTap(sender: UITapGestureRecognizer) {

        // TODO: didSingleTap
        dismissViewControllerAnimated(true, userInitiated: true)
    }

    // MARK: Dismissal

    private func dismissViewControllerAnimated(animated: Bool, userInitiated: Bool, completion: (() -> Void)? = nil) {

        if presentedViewController != nil {
            super.dismissViewControllerAnimated(animated, completion: completion)
        }

        let startingView = currentPhotoViewController?.scalingImageView.imageView
        transitionController.setStartingView(startingView)
        transitionController.setEndingView(referenceViewForCurrentPhoto)

        // TODO
        super.dismissViewControllerAnimated(animated) {
            completion?()
        }
    }
}

extension PhotosViewController: UIPageViewControllerDataSource {

{'fraction_non_alphanumeric': 0.0728756884343037, 'fraction_numerical': 0.0023603461841070024, 'mean_word_length': 3.036919412465264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4135884', 'n_tokens_mistral': 2775, 'n_tokens_neox': 2437, 'n_words': 525}