<commit_before>//
//  WYInteractiveTransitions.swift
//  WYInteractiveTransitions
//
//  Created by <NAME> on 6/5/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

public enum WYTransitoinType {
    case Push
    case Zoom
    case Up
    case Swing
}

public class WYInteractiveTransitions: UIPercentDrivenInteractiveTransition, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
    
    
    public func configureTransition(duration duration: NSTimeInterval?=nil, toView toViewController: UIViewController, panEnable handGestureEnable: Bool?=true, type transitionType: WYTransitoinType) {
        if let duration = duration {
            self.durationTransition = duration
        } else { self.durationTransition = 0.5 }
        self.transitionType = transitionType
        self.toViewController = toViewController
        self.toViewController?.transitioningDelegate = self
        self.toViewController?.modalPresentationStyle = .FullScreen
        if handGestureEnable == true {
            let panEdgeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(WYInteractiveTransitions.screenEdgePanGestureHandler(_:)))
            panEdgeGesture.edges = UIRectEdge.Left
            toViewController.view.addGestureRecognizer(panEdgeGesture)
        }
    }
    
    private var presenting = true
    private var gestureEnable = true
    private var handIn = false
    private var transitionType = WYTransitoinType.Up
    private var toViewController: UIViewController?
    var durationTransition = 0.5
    
    func screenEdgePanGestureHandler(gesture: UIScreenEdgePanGestureRecognizer) {
        if let toView = toViewController {
            let location: CGPoint = gesture.translationInView(toView.view)
            let velocity = gesture.velocityInView(toView.view).x
            
            switch gesture.state {
            case .Began:
                self.handIn = true
                toView.modalPresentationStyle = UIModalPresentationStyle.Custom
                toView.dismissViewControllerAnimated(true, completion: nil)
            case .Changed:
                let animationRatio: CGFloat = location.x / toView.view.bounds.width
                self.updateInteractiveTransition(animationRatio)
            case .Ended, .Cancelled, .Failed:
                self.handIn = false
                if velocity > 0 {
                    finishInteractiveTransition()
                } else {
                    cancelInteractiveTransition()
                }
            default: break
            }
            
        }
    }
    
    public func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let container = transitionContext.containerView()!
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        let fromView = fromVC.view
        let toView = toVC.view
        let duration = self.transitionDuration(transitionContext)
        
        let completeTransition: () -> () = {
            let isCancelled = transitionContext.transitionWasCancelled()
            transitionContext.completeTransition(!isCancelled)
        }

        switch transitionType {
        case WYTransitoinType.Push:
            let moveToLeft = CGAffineTransformMakeTranslation(-container.frame.width, 0)
            let moveToRight = CGAffineTransformMakeTranslation(container.frame.width, 0)
            toView.transform = self.presenting ? moveToRight : moveToLeft
            container.addSubview(toView)
            container.addSubview(fromView)
            UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.7, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                fromView.transform = self.presenting ? moveToLeft : moveToRight
                toView.transform = CGAffineTransformIdentity
                }) { (finished) -> Void in
                    completeTransition()
            }
            
        case WYTransitoinType.Up:
            if presenting {
                toView.frame = container.bounds
                toView.transform = CGAffineTransformMakeTranslation(0, container.frame.size.height)
                container.addSubview(fromView)
                container.addSubview(toView)
                UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                    fromView.transform = CGAffineTransformMakeScale(0.8, 0.8)
                    fromView.alpha = 0.5
                    toView.transform = CGAffineTransformIdentity
                    }, completion: { (finished) -> Void in
                        completeTransition()
                })
            } else {
                let transfrom = toView.transform
                toView.transform = CGAffineTransformIdentity
                toView.frame = container.bounds
                toView.transform = transfrom
                
                container.addSubview(toView)
                container.addSubview(fromView)
                UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                    fromView.transform = CGAffineTransformMakeTranslation(0, fromView.frame.size.height)
                    toView.transform = CGAffineTransformIdentity
                    toView.alpha = 1
                    }, completion: { (finished) -> Void in
                        completeTransition()
                })
            }
            
        case WYTransitoinType.Zoom:
            if presenting {
                container.addSubview(fromView)
                container.addSubview(toView)
                toView.alpha = 0
                toView.transform = CGAffineTransformMakeScale(2, 2)
                UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                    fromView.transform = CGAffineTransformMakeScale(0.5, 0.5)
                    fromView.alpha = 0
                    toView.transform = CGAffineTransformIdentity
                    toView.alpha = 1
                    }, completion: { (finished) -> Void in
                        completeTransition()
                })
            } else {
                container.addSubview(toView)
                container.addSubview(fromView)
                UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                    fromView.transform = CGAffineTransformMakeScale(2, 2)
                    fromView.alpha = 0
                    toView.transform = CGAffineTransformMakeScale(1, 1)
                    toView.alpha = 1
                    }, completion: { (finished) -> Void in
                        completeTransition()
                        
                })
            }
            
        case WYTransitoinType.Swing:
            let offScreenRight = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
            let offScreenLeft = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
            
            toView.transform = self.presenting ? offScreenRight : offScreenLeft
            
            toView.layer.anchorPoint = CGPoint(x:0, y:0)
            fromView.layer.anchorPoint = CGPoint(x:0, y:0)
            toView.layer.position = CGPoint(x:0, y:0)
            fromView.layer.position = CGPoint(x:0, y:0)
            container.addSubview(toView)
            container.addSubview(fromView)
            let duration = self.transitionDuration(transitionContext)
            UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.7, options: [], animations: {
                fromView.transform = self.presenting ? offScreenLeft : offScreenRight
                toView.transform = CGAffineTransformIdentity
                }, completion: { finished in
                    completeTransition()
            })
        }
    }
    
    public func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return durationTransition
    }
    
    public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        toViewController = presenting
        self.presenting = true
        return self
    }
    
    public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.presenting = false
        return self
    }
    
    public func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        if handIn == true {
            return self
        } else { return nil }
    }
    
    public func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return nil
    }
}<commit_msg>Added support for Swift 31<commit_after>//
//  WYInteractiveTransitions.swift
//  WYInteractiveTransitions
//
//  Created by <NAME> on 6/5/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

public enum WYTransitoinType {
    case push
    case zoom
    case up
    case swing
}

open class WYInteractiveTransitions: UIPercentDrivenInteractiveTransition, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
    
    
    open func configureTransition(duration: TimeInterval?=nil, toView toViewController: UIViewController, panEnable handGestureEnable: Bool?=true, type transitionType: WYTransitoinType) {
        if let duration = duration {
            self.durationTransition = duration
        } else { self.durationTransition = 0.5 }
        self.transitionType = transitionType
        self.toViewController = toViewController
        self.toViewController?.transitioningDelegate = self
        self.toViewController?.modalPresentationStyle = .fullScreen
        if handGestureEnable == true {
            let panEdgeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(WYInteractiveTransitions.screenEdgePanGestureHandler(_:)))
            panEdgeGesture.edges = UIRectEdge.left
            toViewController.view.addGestureRecognizer(panEdgeGesture)
        }
    }
    
    fileprivate var presenting = true
    fileprivate var gestureEnable = true
    fileprivate var handIn = false
    fileprivate var transitionType = WYTransitoinType.up
    fileprivate var toViewController: UIViewController?
    var durationTransition = 0.5
    
    func screenEdgePanGestureHandler(_ gesture: UIScreenEdgePanGestureRecognizer) {
        if let toView = toViewController {
            let location: CGPoint = gesture.translation(in: toView.view)
            let velocity = gesture.velocity(in: toView.view).x
            
            switch gesture.state {
            case .began:
                self.handIn = true
                toView.modalPresentationStyle = UIModalPresentationStyle.custom
                toView.dismiss(animated: true, completion: nil)
            case .changed:
                let animationRatio: CGFloat = location.x / toView.view.bounds.width
                self.update(animationRatio)
            case .ended, .cancelled, .failed:
                self.handIn = false
                if velocity > 0 {
                    finish()
                } else {
                    cancel()
                }
            default: break
            }
            
        }
    }
    
    open func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let container = transitionContext.containerView
        let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
        let fromView = fromVC.view
        let toView = toVC.view
        let duration = self.transitionDuration(using: transitionContext)
        
        let completeTransition: () -> () = {
            let isCancelled = transitionContext.transitionWasCancelled
            transitionContext.completeTransition(!isCancelled)
        }
        
        switch transitionType {
        case WYTransitoinType.push:
            let moveToLeft = CGAffineTransform(translationX: -container.frame.width, y: 0)
            let moveToRight = CGAffineTransform(translationX: container.frame.width, y: 0)
            toView?.transform = self.presenting ? moveToRight : moveToLeft
            container.addSubview(toView!)
            container.addSubview(fromView!)
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.7, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
                fromView?.transform = self.presenting ? moveToLeft : moveToRight
                toView?.transform = CGAffineTransform.identity
            }) { (finished) -> Void in
                completeTransition()
            }
            
        case WYTransitoinType.up:
            if presenting {
                toView?.frame = container.bounds
                toView?.transform = CGAffineTransform(translationX: 0, y: container.frame.size.height)
                container.addSubview(fromView!)
                container.addSubview(toView!)
                UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: UIViewAnimationOptions(), animations: { () -> Void in
                    fromView?.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
                    fromView?.alpha = 0.5
                    toView?.transform = CGAffineTransform.identity
                }, completion: { (finished) -> Void in
                    completeTransition()
                })
            } else {
                let transfrom = toView?.transform
                toView?.transform = CGAffineTransform.identity
                toView?.frame = container.bounds
                toView?.transform = transfrom!
                
                container.addSubview(toView!)
                container.addSubview(fromView!)
                UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: UIViewAnimationOptions(), animations: { () -> Void in
                    fromView?.transform = CGAffineTransform(translationX: 0, y: (fromView?.frame.size.height)!)
                    toView?.transform = CGAffineTransform.identity
                    toView?.alpha = 1
                }, completion: { (finished) -> Void in
                    completeTransition()
                })
            }
            
        case WYTransitoinType.zoom:
            if presenting {
                container.addSubview(fromView!)
                container.addSubview(toView!)
                toView?.alpha = 0
                toView?.transform = CGAffineTransform(scaleX: 2, y: 2)
                UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: UIViewAnimationOptions(), animations: { () -> Void in
                    fromView?.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
                    fromView?.alpha = 0
                    toView?.transform = CGAffineTransform.identity
                    toView?.alpha = 1
                }, completion: { (finished) -> Void in
                    completeTransition()
                })
            } else {
                container.addSubview(toView!)
                container.addSubview(fromView!)
                UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: UIViewAnimationOptions(), animations: { () -> Void in
                    fromView?.transform = CGAffineTransform(scaleX: 2, y: 2)
                    fromView?.alpha = 0
                    toView?.transform = CGAffineTransform(scaleX: 1, y: 1)
                    toView?.alpha = 1
                }, completion: { (finished) -> Void in
                    completeTransition()
                    
                })
            }
            
        case WYTransitoinType.swing:
            let offScreenRight = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2))
            let offScreenLeft = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
            
            toView?.transform = self.presenting ? offScreenRight : offScreenLeft
            
            toView?.layer.anchorPoint = CGPoint(x:0, y:0)
            fromView?.layer.anchorPoint = CGPoint(x:0, y:0)
            toView?.layer.position = CGPoint(x:0, y:0)
            fromView?.layer.position = CGPoint(x:0, y:0)
            container.addSubview(toView!)
            container.addSubview(fromView!)
            let duration = self.transitionDuration(using: transitionContext)
            UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.7, options: [], animations: {
                fromView?.transform = self.presenting ? offScreenLeft : offScreenRight
                toView?.transform = CGAffineTransform.identity
            }, completion: { finished in
                completeTransition()
            })
        }
    }
    
    open func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return durationTransition
    }
    
    open func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        toViewController = presenting
        self.presenting = true
        return self
    }
    
    open func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.presenting = false
        return self
    }
    
    open func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        if handIn == true {
            return self
        } else { return nil }
    }
    
    open func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return nil
    }
}
{'fraction_non_alphanumeric': 0.07523593609954131, 'fraction_numerical': 0.00822481151473612, 'mean_word_length': 2.132100396301189, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 41, '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': '3094550', 'n_tokens_mistral': 4979, 'n_tokens_neox': 4590, 'n_words': 949}