<commit_before>//
//  RippleAnimation.swift
//  RippleAnimationView
//
//  Created by Motoki on 2015/12/22.
//  Copyright (c) 2015 MotokiNarita. All rights reserved.
//

import UIKit

private let DefaultScalingAnimateDuration: NSTimeInterval = 1.0
private let DefaultAlphaAnimateDuration: NSTimeInterval   = 0.2
private let DefaultScale: CGFloat = 100

// MARK: - UIView - Ripple Animation Extension
public extension UIView {

    /**
     Adds a ripple animation to UIView instance

     - parameter color:                The color of ripple view.
     - parameter completionHandler:    A closure to be executed when the animation sequence ends
     */
    public func rippleAnimate(color color: UIColor, completionHandler: (() -> Void)?) {
        let startRect = CGRect(x:0, y:0, width:self.frame.width, height:self.frame.height)
        self.rippleAnimate(color: color, scale: DefaultScale, startRect: startRect, scaleAnimateDuration: DefaultScalingAnimateDuration, fadeAnimateDuration: DefaultAlphaAnimateDuration, completionHandler: completionHandler)
    }


    /**
     Adds a ripple effects to UIView instance with detail parameters

     - parameter color:                The color of ripple view.
     - parameter scale:                ripple view scale fuctor
     - parameter startRect:            The initial frame rectangle of the ripple view to be animated
     - parameter scaleAnimateDuration: The duration of the ripple view scale.
     - parameter fadeAnimateDuration:  The duration of the ripple view fade out.
     - parameter completionHandler:    A closure to be executed when the animation sequence ends
     */
    public func rippleAnimate(color color: UIColor, scale: CGFloat, startRect: CGRect, scaleAnimateDuration: NSTimeInterval, fadeAnimateDuration: NSTimeInterval, completionHandler: (() -> Void)?) {

        let rippleView = RippleView(frame: startRect, backgroundColor: color)
        self.addSubview(rippleView)

        let scaleAnimation = { () -> Void in
            let widthRatio = self.frame.width / rippleView.frame.width
            rippleView.transform = CGAffineTransformMakeScale(widthRatio * scale, widthRatio * scale)
        }

        let fadeAnimation = { rippleView.alpha = 0.0 }

        // start scale animation
        UIView.animateWithDuration(scaleAnimateDuration, animations: scaleAnimation) { (completion) -> Void in
            if completion {

                // start fade animation
                UIView.animateWithDuration(fadeAnimateDuration, animations: fadeAnimation) { (completion) -> Void in
                    if completion {
                        rippleView.removeFromSuperview()

                        if let completionHandler = completionHandler {
                            completionHandler()
                        }

                    }
                }
            }
        }
    }

}

/// Custom UIView for ripple effects
private final class RippleView: UIView {

    init(frame: CGRect, backgroundColor: UIColor) {
        super.init(frame: frame)
        self.backgroundColor = backgroundColor
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func drawRect(rect: CGRect) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: CGRectGetWidth(self.bounds) / 2)
        let maskLayer = CAShapeLayer()
        maskLayer.path = maskPath.CGPath
        self.layer.mask = maskLayer<commit_msg>Add Configuration to setup Animation<commit_after>//
//  RippleAnimation.swift
//  RippleAnimationView
//
//  Created by Motoki on 2015/12/22.
//  Copyright (c) 2015 MotokiNarita. All rights reserved.
//

import UIKit

private let DefaultScalingAnimateDuration: NSTimeInterval = 1.0
private let DefaultAlphaAnimateDuration: NSTimeInterval   = 0.2
private let DefaultScale: CGFloat = 100

// MARK: - UIView - Ripple Animation Extension
public extension UIView {

    private var rippleDefaultStartRect: CGRect {
        return CGRect(x:0, y:0, width:self.frame.width, height:self.frame.height)
    }

    public struct RippleConfiguration {
        var color: UIColor
        var clipsToBounds = false
        var startRect: CGRect? = nil
        var scale: CGFloat = DefaultScale
        var scaleAnimateDuration = DefaultScalingAnimateDuration
        var fadeAnimateDuration = DefaultAlphaAnimateDuration
        var completionHandler: (() -> Void)? = nil

        init(color: UIColor) {
            self.color = color
        }
    }

    public func rippleAnimate(config: UIView.RippleConfiguration, completionHandler: (() -> Void)?) {

        clipsToBounds = config.clipsToBounds
        let startRect = config.startRect ?? rippleDefaultStartRect

        self.rippleAnimate(color: config.color, scale: DefaultScale, startRect: startRect, scaleAnimateDuration: config.scaleAnimateDuration, fadeAnimateDuration: config.fadeAnimateDuration, completionHandler: completionHandler)

    }

    private func rippleAnimate(color color: UIColor, scale: CGFloat, startRect: CGRect, scaleAnimateDuration: NSTimeInterval, fadeAnimateDuration: NSTimeInterval, completionHandler: (() -> Void)?) {

        let rippleView = RippleView(frame: startRect, backgroundColor: color)
        self.addSubview(rippleView)

        let scaleAnimation = {
            let widthRatio = self.frame.width / rippleView.frame.width
            rippleView.transform = CGAffineTransformMakeScale(widthRatio * scale, widthRatio * scale)
        }

        let fadeAnimation = { rippleView.alpha = 0.0 }

        // start scale animation
        UIView.animateWithDuration(scaleAnimateDuration, animations: scaleAnimation) { completion in

            guard completion else { return }

            // start fade animation
            UIView.animateWithDuration(fadeAnimateDuration, animations: fadeAnimation) { completion in
                guard completion else { return }
                rippleView.removeFromSuperview()
                completionHandler?()
            }
        }
    }

}

/// Custom UIView for ripple effects
private final class RippleView: UIView {

    init(frame: CGRect, backgroundColor: UIColor) {
        super.init(frame: frame)
        self.backgroundColor = backgroundColor
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func drawRect(rect: CGRect) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: CGRectGetWidth(self.bounds) / 2)
        let maskLayer = CAShapeLayer()
        maskLayer.path = maskPath.CGPath
        self.layer.mask = maskLayer
{'fraction_non_alphanumeric': 0.0776449220523687, 'fraction_numerical': 0.007265021946420463, 'mean_word_length': 2.7019607843137257, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5145291', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1722, 'n_words': 479}